mocha + chai 的单元测试总是通过

Unit test with mocha + chai always passed

我尝试使用 'mocha' 和 'chai' 进行单元测试,但我对测试结果有疑问,它总是通过。 请看一下。

UnitTest.spec.ts

import PostgresService from "../src/Services/PostgresService"
import {expect} from "chai"
import 'mocha'

describe('Postgres Override Test function', () => {
    it('should return any number but not zero', async () => {
        let client = new PostgresService();
        let result = await client.getLatestCMD("Servo");   
        try{
            console.log("Type : " + typeof(result));
            console.log("Value : " + result.rows[0].id);
            expect(result.rows[0].id).to.equal(0)        
        }catch(error){

        }
    })
})

删除 try catch 块以实际 运行 您的 expect 函数。

如果您的 try 块 returns 出现错误,JavaScript 解释器将移至 catch 块,因此前者永远不会 运行。

当你使用ASYNCHRONOUS CODE时,你需要在回调中完成

例子

您需要将完成声明为 it 的参数。

it('description', (done) => {
    expect((err, result) => {
    if (err) done(err);
    else done();
 })
})