你可以使用.throw([errorLike], [errMsgMatcher], [msg])方法。
例如
index.ts:
export class MyClass {
name: string;
constructor(name: string) {
if (typeof name !== 'string') {
throw new TypeError('expect "string" type for "name" argument');
}
this.name = name;
}
}
index.test.ts:
import { MyClass } from './';
import { expect } from 'chai';
describe('63958704', () => {
it('should throw error if no parameter passed in the constructor', () => {
expect(() => new MyClass(1 as any)).to.throw('expect "string" type for "name" argument');
});
});
单元测试结果:
63958704
✓ should throw error if no parameter passed in the constructor
1 passing (43ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 75 | 50 | 100 | 75 |
index.ts | 75 | 50 | 100 | 75 | 7
----------|---------|----------|---------|---------|-------------------