Support transforming resolves (to allow for .ts-based snapshotResolver)
See original GitHub issue🚀 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
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Configuring Jest Snapshot Resolvers - Bruno Scheufler
js , returning a transformed file path for the snapshot created by the tests the file contains. On to the next function! //...
Read more >Configuring Jest
Here is how to enable it globally (additional options are not supported): ... While code transformation is applied to the linked setup-file, Jest...
Read more >ESM Support | ts-jest - GitHub Pages
To use ts-jest with ESM support: Check ESM Jest documentation. Enable useESM true for ts-jest config. Include .ts in extensionsToTreatAsEsm Jest ...
Read more >How to configure jest snapshot locations - Stack Overflow
In package.json (or if you use jest.config.js ) you need to add the path for the snapshotResolver file: "jest": { "snapshotResolver": ".
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ll fix that
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