【发布时间】:2017-01-01 01:46:05
【问题描述】:
我正在实施 jest 来测试我的 React 应用程序,并已对我拥有的实用程序函数设置了一个简单的测试,但我收到了错误:
错误:您的测试套件必须至少包含一个测试。
检查了我的实现,我认为一切都是正确的 - 有人可以看看我吗?
测试用的文件结构及功能如下
- __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",但出现以下错误:
谢谢大家:)
注意:it 中有错字,但在修复后我收到了一个新错误:
【问题讨论】:
-
我很确定Node.js doesn't support ES2015 modules(还)。尝试将
export default sumObjectValues更改为module.exports = sumObjectValues。 -
啊,当然,我可以添加 babel jest 模块来尝试排序,试试吧!谢谢你:)
-
啊伙计,这很痛苦.. 我想我需要睡觉了。它现在已经运行了 我已经安装了 babel-jest,但是我收到了一个新错误:FAIL __tests__/sumObjectValues-test.js (321.763s) ● sumObjectValues › 它需要一个对象数组,每个对象都有数量值,并对这些值求和- TypeError: sumObjectValues is not a function at Object.
(__tests__/sumObjectValues-test.js:27:10) 1 个测试失败,0 个测试通过(1 个测试套件中总共 1 个,运行时间 344.008 秒)npm ERR!测试失败。有关更多详细信息,请参见上文。 -
我只是吐口水,因为我还没有在 Node 中使用过 ES2015 模块,但是你有没有尝试过使用
importsyntax 而不是require? -
是的!你美女!谢谢迈克,就是这样,感谢您的帮助:)
标签: javascript unit-testing testing jestjs