【发布时间】:2025-11-24 00:30:03
【问题描述】:
class TestObject {
constructor(value) {
if (value === null || value === undefined) {
throw new Error('Expect a value!');
}
}
}
describe('test the constructor', () => {
test('it works', () => {
expect(() => {
new TestObject();
}).toThrow();
});
test('not work', () => {
expect(new TestObject()).toThrow();
});
});
这里有 2 个测试用例,一个有效,另一个无效。
not work 的失败消息如下:
● 测试构造函数 › 不工作
期待一个值!
at new TestObject (tests/client/utils/aaa.test.js:4:11) at Object.<anonymous> (tests/client/utils/aaa.test.js:17:12) at Promise (<anonymous>) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7)
为什么我需要在函数调用中包装该调用,当函数只返回一个普通值,甚至是一个承诺时,我们不需要包装,我们可以使用async/await 来检查expect()而不是在expect() 中创建一个函数。
这里发生了什么?
【问题讨论】:
标签: javascript ecmascript-6 jestjs es6-class