Tec*_*tle 4 typescript webpack jestjs
该项目使用 ReactJS、Typescript、Webpack 和 Jest。\n为了实现模块解析(以最小化导入),我们进行了以下更改:
\n\nTSConfig.js:
"compilerOptions": { "baseUrl": "src",}
Webpack.config.js
alias: {\n Common: path.resolve(__dirname, \'src/Common/\'),\n},\nRun Code Online (Sandbox Code Playgroud)\n\n代码工作正常,但是 Jest 测试开始失败并出现错误:
\n\nCannot find module \'Common/js/utils\' from \'MyContact.ts\'
这是因为,Jest 还不知道如何将 Common 解析为 Src/common,而不是在 node_module 中查找。为了解决这个问题,进行了以下更改:
\n\njestConfig.js
moduleNameMapper: {"Common": "<rootDir>/src/Common"}
这似乎已经解决了,但我还有另一个问题[这就是我在这里的原因:)]
\n\nJest 遇到意外令牌
\n\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it\'s not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere\'s what you can do:\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\nYou\'ll find more details and examples of these config options in the docs:\nhttps://jestjs.io/docs/en/configuration.html\n\nDetails:\n\nC:\\Users\\ua\\Project\\develop\\src\\Common\\index.ts:1\n({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from \'./Acc\'\n ^^^^^^\n\nSyntaxError: Unexpected token export\n\n at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)\n at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)\nRun Code Online (Sandbox Code Playgroud)\n\n这是完整的 jest.config.js 文件:
\n\nalias: {\n Common: path.resolve(__dirname, \'src/Common/\'),\n},\nRun Code Online (Sandbox Code Playgroud)\r\ntsconfig.js:
\n\nThis usually means that you are trying to import a file which Jest cannot parse, e.g. it\'s not plain JavaScript.\n\nBy default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".\n\nHere\'s what you can do:\n \xe2\x80\xa2 To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.\n \xe2\x80\xa2 If you need a custom transformation specify a "transform" option in your config.\n \xe2\x80\xa2 If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.\n\nYou\'ll find more details and examples of these config options in the docs:\nhttps://jestjs.io/docs/en/configuration.html\n\nDetails:\n\nC:\\Users\\ua\\Project\\develop\\src\\Common\\index.ts:1\n({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from \'./Acc\'\n ^^^^^^\n\nSyntaxError: Unexpected token export\n\n at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)\n at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)\nRun Code Online (Sandbox Code Playgroud)\r\n| 归档时间: |
|
| 查看次数: |
7683 次 |
| 最近记录: |