问题描述
使用节点配置Jest之后,我可以无错误地运行测试,但是我会得到这样的输出
Test Suites: 0 of 2 total
Tests: 0 total
Snapshots: 0 total
Time: 0.508s
Ran all test suites.
即使我有一个测试文件,但它不会使它失败或通过
const sum = require('./sum');
test('adds 1 + 2 to equal 3',() => {
expect(sum(1,2)).toBe(3);
});
配置文件
{
"name": "my-project","testMatch": [ "**/__tests__/**/*.[jt]s?(x)" ],"testPathIgnorePatterns": ["\\node_modules\\"]
}
脚本
"test": "jest --config ./jest.config.json",
命令
npm run test
Jest Install命令
npm install jest@22.2.2
如何开始获得正确的结果?
解决方法
您可以尝试此解决方案,将配置文件扩展为.js。
jest.config.js
module.exports = {
roots: ['./__tests__'],// provide path with reference to this modue,I assumed both are in root
};
脚本
"test": "jest --config='./jest.config.js'"
sum.js
function sum(num,num1){
return num+num1
}
exports.sum=sum
__测试__ / sum.test.js
const sum = require('../sum');
test('adds 1 + 2 to equal 3',() => {
expect(sum.sum(1,2)).toBe(3);
});