0

我正在使用 nodeJS 并使用 sinon、chai 以及代码覆盖率 nyc 创建了单元测试。但 nyc 对语句、行、函数返回 0% -

======================== 覆盖总结 ======================== =======

语句:0% (0/34) 分支:100% (0/0) 函数:0% (0/4) 行:0% (0/34)

app.js -

 exports.get  = async (event, context) => {
      try {
           const data = await getResult(13, 'table', 'inbox', false, 2);
           return success(data);
        } catch (err) {
       return failure({ status: false });
     }
   };

app.test.unit.js

const sandbox = sinon.createSandbox();
const expect = chai.expect;
let event, context;

describe('test get', () => {
    afterEach(() => {
        sandbox.restore();
    });

    it('get should return success', async () => {
        // Mock
        const result = { };
        sandbox.stub(lambda, 'getResult').returns(result);

        // Act
        let response = await get(event, context);
        expect(response.statusCode).equal(200);
    });

    it('get should return failure', async () => {
        sandbox.stub(lambda, 'getResult').throws(new Error('whatever'));

        // Act
        let response = await get(event, context);
        console.log(response.body);

        //Expect
        expect(response.statusCode).equal(500);

    });
});
4

0 回答 0