具有模块分辨率的打字稿的 Jest 设置

Tec*_*tle 4 typescript webpack jestjs

该项目使用 ReactJS、Typescript、Webpack 和 Jest。\n为了实现模块解析(以最小化导入),我们进行了以下更改:

\n\n

TSConfig.js:

\n\n

"compilerOptions": { "baseUrl": "src",}

\n\n

Webpack.config.js

\n\n
alias: {\n  Common: path.resolve(__dirname, \'src/Common/\'),\n},\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码工作正常,但是 Jest 测试开始失败并出现错误:

\n\n

Cannot find module \'Common/js/utils\' from \'MyContact.ts\'

\n\n

这是因为,Jest 还不知道如何将 Common 解析为 Src/common,而不是在 node_module 中查找。为了解决这个问题,进行了以下更改:

\n\n

jestConfig.js

\n\n

moduleNameMapper: {"Common": "<rootDir>/src/Common"}

\n\n

这似乎已经解决了,但我还有另一个问题[这就是我在这里的原因:)]

\n\n

Jest 遇到意外令牌

\n\n
This 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)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是完整的 jest.config.js 文件:

\n\n

\r\n
\r\n
alias: {\n  Common: path.resolve(__dirname, \'src/Common/\'),\n},\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

tsconfig.js:

\n\n

\r\n
\r\n
This 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)\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

Tec*_*tle 6

好的,所以我可以通过将此行添加到 jest.config.js 来修复模块解析

moduleDirectories: ["node_modules", "src"],