我正在使用Jest来测试我的React-App。 我已经拥有了一些测试套件,但是我无法运行其中的两个。
一个错误示例:
FAIL react/utils/__tests__/ChartsUtils-tests.js (2.662s)
● ChartsUtils › it get labels for a date period
- Error: Failed to get mock metadata: /Users/nessie/Sites/blackriver-analytics-project/node_modules/core-js/modules/$.global.js
at Loader._generateMock (node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:251:15)
at Object.eval (node_modules/core-js/modules/$.export.js:1:137)
at Object.eval (node_modules/core-js/modules/es5.js:3:25)
1 test failed, 0 tests passed (1 total in 1 test suite, run time 3.68s)
npm ERR! Test failed. See above for more details.
我的测试看起来像这样:
/* global expect jest it beforeEach describe */
'use strict';
jest.dontMock('../ChartsUtils');
import ChartsUtils from '../ChartsUtils';
describe('ChartsUtils', function () {
it('get labels for a date period', function () {
// let labels = ChartsUtils.getLabels('2015-07-15', '2015-07-20');
// console.log(labels)
});
});
我的JS文件是这样的:
'use strict'
import Period from 'date-period'
import Datetime from 'react-datetime'
module.exports = {
getLabels: function (s, e) {
let start = '',
end = '',
duration = 'P1D',
period,
days = []
if (typeof s === 'number' && typeof e === 'number') {
start = Datetime.moment.unix(s)
end = Datetime.moment.unix(e)
} else {
start = Datetime.moment(s)
end = Datetime.moment(e)
}
period = Period({start, duration, end})
for (let date of period) {
days.push(Datetime.moment(date).format('MMM, Do'))
}
return days
}
};
由于我的代码被删除,我认为问题在于
import Period from 'date-period'
但是我已经将模块添加到我的package.json中的 unmockedModulePathPatterns ,它看起来如下:
"jest": {
"rootDir": ".",
"verbose": false,
"noStackTrace": false,
"collectCoverage": false,
"moduleFileExtensions": [
"js",
"jsx"
],
"scriptPreprocessor": "<rootDir>/react/jest/preprocessor.js",
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/react-dom",
"<rootDir>/node_modules/react-router",
"<rootDir>/node_modules/react-addons-test-utils",
"<rootDir>/node_modules/react-bootstrap",
"<rootDir>/node_modules/react-datetime",
"<rootDir>/node_modules/fbjs",
"<rootDir>/node_modules/events",
"<rootDir>/node_modules/object-assign",
"<rootDir>/node_modules/date-period",
"<rootDir>/node_modules/moment",
"<rootDir>/node_modules/keymirror"
],
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/"
]
},
我一直在阅读其他错误报告,有人建议将路径从/ node_modules / date-period更改为“date-period”,但它并没有改变我的任何内容。
我的第二个文件具有完全相同的错误,但使用不同的模块“react-bootstrap”