-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Closed
Labels
Description
π Feature Proposal
Currently resolver files used for properties such as snapshotResolver
don't get transformed, meaning that you can only use language features that can be transformed by Babel.
For example, TypeScript resolvers won't work:
package.json
:
...
"jest": {
"snapshotResolver": "./test/snapshotResolver.ts",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json",
"jsx",
"node"
],
"moduleNameMapper": {
"^@src/(.*)$": "<rootDir>/src/$1",
"^@test/(.*)$": "<rootDir>/test/$1"
},
"transform": {
".(ts|tsx)": "ts-jest"
}
},
...
// snapshotResolver.ts
module.exports = {
/** resolves from test to snapshot path */
resolveSnapshotPath: function (testPath: string, snapshotExtension: string) {
return testPath.replace('src/', '__snapshots__/') + snapshotExtension;
},
/** resolves from snapshot to test path */
resolveTestPath: function (snapshotFilePath: string, snapshotExtension: string) {
return snapshotFilePath
.replace('__snapshots__/', 'src/')
.slice(0, -snapshotExtension.length);
},
testPathForConsistencyCheck: ''
};
Output:
β Test suite failed to run
Jest encountered an unexpected token
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\G-Rath\workspace\projects-personal\strongly-typed-event-emitter\test\snapshotResolver.ts:3
resolveSnapshotPath: (testPath: string, snapshotExtension: string) => {
^
SyntaxError: Unexpected token :
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
Based off discussion with @SimenB