Unit test with mocha + chai always passed

倖福魔咒の 提交于 2019-12-14 03:02:45

问题


I try to use 'mocha' and 'chai' for my unit test but I have a problem with the test result, it always passed. Please take a look.

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){

        }
    })
})


回答1:


Remove the try catch block to actually run your expect function.

If your try block returns an error the JavaScript interpreter moves on to the catch block and so the former is never run.




回答2:


When you use ASYNCHRONOUS CODE, you need done in the callback

example

You need to declare done as an argument to it.

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


来源:https://stackoverflow.com/questions/47111489/unit-test-with-mocha-chai-always-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!