问题
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