我正在实现 jest 来测试我的 React 应用程序,并对我拥有的实用程序函数设置了一个简单的测试,但我收到了错误:
Error: Your test suite must contain at least one test.
检查了我的实现,我认为一切都是正确的 - 有人可以帮我看看吗?
测试的文件结构和函数如下
- __tests__
-- sumObjectValues-test.js
- utils
-- sumObjectValues.js
sumObjectValues.js
如下:
const sumObjectValues = (obj, identifier) => {
return obj
.map((el) => { return el[identifier]; })
.reduce((prev, next) => { return prev += next; }, 0);
}
export default sumObjectValues;
和sumObjectValues-test.js
:
const obj = [
{
"id": 0,
"item": "Tesla Model S",
"amount": 85000
},
{
"id": 1,
"item": "iPhone 6S",
"amount": 600
},
{
"id": 2,
"item": "MacBook Pro",
"amount": 1700
}
];
const identifier = "amount";
jest.unmock('../client/utils/sumObjectValues'); // unmock to use the actual implementation of `sumObjectValues`
describe('sumObjectValues', () => {
if('takes an array of objects, each with amount values, & sums the values', () => {
const sumObjectValues = require('../client/utils/sumObjectValues');
expect(sumObjectValues(obj, identifier)).toBe(87300);
});
});
然后我的 package.json 脚本中有 "test": "jest"
,但出现以下错误:
FAIL __tests__/sumObjectValues-test.js (0s) ● Runtime Error - Error: Your test suite must contain at least one test. at onTestResult (node_modules/jest-cli/build/TestRunner.js:143:18) 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 13.17s) npm ERR! Test failed. See above for more details.
谢谢大家:)
N.B.:在 it
中是一个拼写错误,但修复后我收到了一个新错误:
FAIL __tests__/sumObjectValues-test.js (321.763s) ● sumObjectValues › it takes an array of objects, each with amount values, & sums the values - TypeError: sumObjectValues is not a function at Object. (__tests__/sumObjectValues-test.js:27:10) 1 test failed, 0 tests passed (1 total in 1 test suite, run time 344.008s) npm ERR! Test failed. See above for more details.
最佳答案
if('takes an array of objects, each with amount values, & sums the values', () => {
应该是
it('takes an array of objects, each with amount values, & sums the values', () => {
关于javascript - Jest : Error: Your test suite must contain at least one test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39131521/