我有一个使用Jest和Babel的NodeJS项目。 运行测试时,我遇到以下错误:
FAIL ./user.test.js
● Test suite failed to run.
SyntaxError: C:\Users\...\__test__\mocks\registerUser.json: Unexpected token, expected ";" (2:8)
当我将registerUser.json更改为javascript文件时,我可以轻松获取内容,但是当我尝试使用JSON文件时,无法正常工作。
但是我似乎找不到使用JSON文件时错误的原因。
我的当前结构类似于:
|-- src
|-- __test__
|-- mocks
|-- registerUser.json
|-- user.test.js
|-- tmp
.babelrc
.eslintignore
.eslintrc
.gitignore
.prettierrc
jest.config.js
package.json
user.test.js
const userRegisterMock = require('./mocks/registerUser.json');
describe('User', () => {
it('[SUCCESS] should be able to register', async () => {
// console.log(userRegisterMock)
expect(true).toBe(true);
});
});
jest.config.js
module.exports = {
bail: 1,
testEnvironment: 'node',
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: [
'src/**',
'!src/helpers/**',
'!src/app.js',
'!src/server.js',
'!src/database/**',
'!src/config/**'
],
coverageDirectory: '__tests__/coverage',
coverageReporters: ['text', 'lcov'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100
}
},
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
resetModules: false,
testMatch: ['**/__tests__/**/*.test.js'],
transform: {
'.(js|jsx|ts|tsx)': 'babel-jest'
}
};
.babelrc
{
"presets": [
["@babel/preset-env", {
"useBuiltIns": "usage", // or "entry"
"corejs": 3,
"targets": {
"node": "current"
}
}]
],
"plugins": [
["@babel/plugin-transform-modules-commonjs"],
["@babel/plugin-transform-runtime", {
"regenerator": true
}]
]
}
registerUser.json
{
"name": "Test Case",
"country": "Brazil",
"birthdate": "25/03/1994",
"sex": "Masculino",
"nationality": "Brasileiro"
}
mocks
,而是mock
,对吗? - trmaphijson
转换 https://github.com/facebook/jest/issues/8426 - trmaphi