在尝试运行测试时,将React Native与Jest和TypeScript结合使用:
Test suite failed to run
[BABEL] unknown: Preset /* your preset */ requires a filename to be set when babel is called directly,
babel.transform(code, { filename: 'file.ts', presets: [/* your preset */] });
See https://babeljs.io/docs/en/options#filename for more information.
任何想法会导致这种情况吗?
这是我在jest
上的package.json
部分:
"jest": {
"preset": "react-native",
"roots": [
"<rootDir>/__tests__"
],
"setupFiles": [
"<rootDir>/scripts/jest/setup.js",
"<rootDir>/node_modules/appcenter/test/AppCenterMock.js",
"<rootDir>/node_modules/appcenter-analytics/test/AppCenterAnalyticsMock.js",
"<rootDir>/node_modules/appcenter-crashes/test/AppCenterCrashesMock.js",
"<rootDir>/node_modules/appcenter-push/test/AppCenterPushMock.js",
"<rootDir>/node_modules/react-native-localization.js",
"./config/jest/mock.fetch.js",
"./config/jest/react-native-localization.js"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"collectCoverage": true,
"coverageDirectory": "coverage",
"coverageReporters": [
"text-summary",
"html"
],
"testPathIgnorePatterns": [
"\\.snap$",
"<rootDir>/node_modules/",
"<rootDir>/lib/"
],
"collectCoverageFrom": [
"**/*.{js,jsx}",
"artifacts/**/*.js",
"!artifacts/**/*.spec.js",
"!artifacts/**/*.index.js",
"!<rootDir>/node_modules/"
]
},
这是我的babel.config.js
文件:
module.exports = {
presets: [
'module:metro-react-native-babel-preset'
],
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', { legacy: true }]
],
};