Javascript 预计投手不会得到报道

Javascript 预计投手不会得到报道,javascript,node.js,unit-testing,jestjs,Javascript,Node.js,Unit Testing,Jestjs,如果未提供字符串,则会在我的反转函数上抛出一个验证错误(自定义): const invert=(str)=>{ 如果(str=='')抛出新的ValidationError('字符串不能为空'); 返回str; }; 在使用Jest时,我的断言没有完全覆盖这行代码: expect(()=>invert(“”)).toThrow(ValidationError); 有什么方法可以覆盖这一行吗?您至少应该有两个测试用例。一个测试抛出错误,一个测试正常。例如 index.ts: 导出类Valida

如果未提供
字符串
,则会在我的
反转
函数上抛出一个
验证错误
(自定义):

const invert=(str)=>{
如果(str=='')抛出新的ValidationError('字符串不能为空');
返回str;
};
在使用Jest时,我的断言没有完全覆盖这行代码:

expect(()=>invert(“”)).toThrow(ValidationError);

有什么方法可以覆盖这一行吗?

您至少应该有两个测试用例。一个测试抛出错误,一个测试正常。例如

index.ts

导出类ValidationError扩展错误{
构造函数(消息){
超级(信息);
}
}
导出常量反转=(str)=>{
如果(str=='')抛出新的ValidationError('字符串不能为空');
返回str;
};
index.test.ts

从“/”导入{invert,ValidationError};
描述('64271662',()=>{
它('如果字符串为空,则应引发验证错误',()=>{
expect(()=>invert(“”)).toThrow(ValidationError);
});
它('应该返回字符串',()=>{
expect(倒置('teresa teng')).toBe('teresa teng');
});
});
100%覆盖率的单元测试结果:

 PASS  src/stackoverflow/64271662/index.test.ts (9.867s)
  64271662
    ✓ should throw validation error if string is empty (4ms)
    ✓ should return string (1ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        11.042s

我提供了错误的if!编辑