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: ''
};
md5-25d9d86f3b7fccc5364d15f047394042
● 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
This also applies for "resolver" resolvers.
This should be pretty doable 🙂 We recently refactored the logic for transpilation of this in #8756, so the diff should be pretty small. See #8751 for how test envs can be transpiled. Copying that approach for snapshot resolvers (and resolvers if you want) should be pretty straight forward
Awesome, thanks for the links - I'll check them out & see what I can hack together :D
Looking over the config options, I've come up w/ this list:
are transformed:
snapshotSerializers
setupFiles
setupFilesAfterEnv
not transformed:
runner
resolver
snapshotResolver
dependencyExtractor
testEnvironment
testResultsProcessor
testRunner
testSequencer
I don't know how easy it would be to transform all of those, so I won't get caught up in trying, but I'll see how far I can take it, and get as many transforming as I can (each in their own branch/PR of course).
@SimenB sadly I don't think it'll be as small of a diff as you're expecting, as it means making buildSnapshotResolver async 😬
I'll fix that
@SimenB @G-Rath I can pick up some of the modules to transform. I'll mention the list in an issue so it will be easy if anyone else wants to pick it up
Most helpful comment
I'll fix that