【发布时间】:2019-08-24 10:14:31
【问题描述】:
我想测试一个函数的无效输入并期望该函数抛出 在那个输入上。但是测试没有通过,但函数仍然抛出错误。我是个开玩笑的初学者,所以我不知道为什么会这样。
我的函数如下所示:
export class MyClass{
static theFunction(tokens){
let result = [];
if (typeof tokens[0] === "string") {
return tokens;
} else {
try {
for(let token of tokens){
result.push(token.text);
}
return result;
} catch (e) {
throw new Error(e); //also tried throw e; and no try/catch aswell
}
}
}
}}
Test.js:
import {MyClass} from './MyClass'
describe('Test the MyClass:', () => {
test('invalid inputs for thefunction()', () => {
expect(MyClass.theFunction(0)).toThrow(/*'TypeError: tokens is not iterable'*/);
//Tried with and without the Error Message
});
});
我错过了什么?
【问题讨论】:
标签: javascript jestjs