Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transforming resolves (to allow for .ts-based snapshotResolver) #8330

Closed
G-Rath opened this issue Apr 14, 2019 · 7 comments · Fixed by #8829
Closed

Support transforming resolves (to allow for .ts-based snapshotResolver) #8330

G-Rath opened this issue Apr 14, 2019 · 7 comments · Fixed by #8829

Comments

@G-Rath
Copy link
Contributor

G-Rath commented Apr 14, 2019

🚀 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

@G-Rath
Copy link
Contributor Author

G-Rath commented May 26, 2019

This also applies for "resolver" resolvers.

@SimenB
Copy link
Member

SimenB commented Aug 10, 2019

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

@G-Rath
Copy link
Contributor Author

G-Rath commented Aug 10, 2019

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).

@G-Rath
Copy link
Contributor Author

G-Rath commented Aug 11, 2019

@SimenB sadly I don't think it'll be as small of a diff as you're expecting, as it means making buildSnapshotResolver async 😬

@SimenB
Copy link
Member

SimenB commented Aug 11, 2019

I'll fix that

@Mark1626
Copy link
Contributor

@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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants