Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@G-Rath

Description

@G-Rath

πŸš€ 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions