webpack - Jest-konfiguration för TypeScript med modulupplösning
Projektet använder ReactJS, Typescript, Webpack och Jest. För att uppnå modulupplösning (för att minimera import), gjorde vi följande ändringar:
TSConfig.js:"compilerOptions": { "baseUrl": "src",}Webpack.config.jsalias: {
Common: path.resolve(__dirname, 'src/Common/'),
},
Koden fungerar fint, men Jest-tester började misslyckas med fel:
Cannot find module 'Common/js/utils' from 'MyContact.ts'Detta beror på att Jest ännu inte vet hur man löser Common till Src/common och inte ska leta i node_module. För att åtgärda detta gjordes följande ändringar:
jestConfig.jsmoduleNameMapper: {"Common": "<rootDir>/src/Common"}Det verkar fixat, men sedan har jag ett annat problem [vilket är anledningen till att jag är här :]
Jest stötte på ett oväntat tecken
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
C:\Users\ua\Project\develop\src\Common\index.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './Acc'
^^^^^^
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
Här är hela jest.config.js-filen:
module.exports = {
verbose: false,
roots: ["<rootDir>/src"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
globals: {
'ts-jest': {
diagnostics: false
}
},
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/__mocks__/fileMock.js",
"\\.(css|less|pcss)$": "<rootDir>/__mocks__/styleMock.js",
"Common": "<rootDir>/src/Common",
},
snapshotSerializers: ["enzyme-to-json/serializer"],
setupFilesAfterEnv: ["<rootDir>/setupEnzyme.ts"],
testMatch: [ "**/__tests__/**/*.test.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
collectCoverageFrom: ["**/*.{ts,tsx}", "!**/node_modules/**", "!**/vendor/**"]
};tsconfig.js:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "src",
"typeRoots": [
"./node_modules/@types",
"./custom_typings"
],
"outDir": "build",
"module": "commonjs",
"skipLibCheck": true,
"moduleResolution": "node",
"removeComments": true,
"target": "es5",
"sourceMap": true,
"lib": [ "dom", "es2015", "es2017", "esnext" ],
"noEmit": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": false,
"allowUnusedLabels": false,
"jsx": "react",
"allowJs": true,
"isolatedModules": false,
"strictNullChecks": false,
"noEmitHelpers": false,
"allowSyntheticDefaultImports": true
},
"typeRoots": [
"./node_modules/@types",
"./custom_typings"
],
"exclude":[
"./node_modules"
],
"include": [
"src/*",
"custom_typings"
]
}Svar #1
Okej, så jag kunde fixa modulupplösningen genom att lägga till den här raden i jest.config.js
moduleDirectories: ["node_modules", "src"],Svar #2
Det ser ut som att Babel som standard inte kan tolka en TypeScript-testfil, så installera npm install ts-jest babel-jest --save-dev. Lägg sedan till följande konfiguration i filen jest.config.js.
transform: {
'^.+\\.jsx?$': require.resolve('babel-jest'),
'^.+\\.tsx?$': 'ts-jest',
}
