0
我在寫一個打字稿庫。它揭示了以下功能:如何使用chai捕捉事件錯誤
export declare function setDictionary(_dictionary: object): void;
export declare function getMessage(error: string): any;
export declare function setLoggerLevel(level: string): void;
export declare function on(event: string, handler: IListener): void;
現在我想用Mocha和Chai測試函數getMessage()。我通過一個錯誤的輸入,並使其拋出(它發出一個「錯誤」事件加上新的錯誤)。這是我的代碼:
// Imports and Globals
import * as responseGiver from '../index.js';
import { expect } from 'chai';
import 'mocha';
describe('Public functions',() => {
describe('getMessage(error : string) : any',() => {
it('should throw when the dictionary is not set',() => {
expect(function() { responseGiver.getMessage("A") }).to.throw(new Error("The dictionary is not set"));
});
});
});
但是,它不起作用。
Public functions
getMessage(error : string) : any
error: The dictionary is not set
1) should throw when the dictionary is not set
0 passing (14ms)
1 failing
1) Public functions getMessage(error : string) : any should throw when the dictionary is not set:
AssertionError: expected [Function] to throw 'Error: The dictionary is not set'
at Context.<anonymous> (js-src/test/test.js:17:83)
npm ERR! Test failed. See above for more details.
我讀過關於堆棧溢出類似的線程,但我仍然沒有得到如何使它工作,在這種情況下是如何工作的柴。也許this可能會有幫助,但我不知道如何將這個示例應用於我的案例。