kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.98k stars 455 forks source link

Babel option `ignore` breaking for RegExp #902

Closed artola closed 4 years ago

artola commented 5 years ago

Issue :

With the following jest.config.js:

...
  globals: {
    'ts-jest': {
      babelConfig: true,
      tsConfig: true,
      enableInternalCache: true,
      diagnostics: false,
      disableSourceMapSupport: false,
      ignoreCoverageForDecorators: false,
      ignoreCoverageForAllDecorators: true,
      useExperimentalLanguageServer: false,
    },
  },
...

It fails when I add to babel.config.js:

...
    ignore: [/core-js/, /@babel(?:\/|\\{1,2})runtime/, /localforage/],
...

Note: Babel compile without problem with webpack.

It seems that /node_modules/@babel/core/lib/config/config-chain.js () is not receiving the pathToTest as I debugged it is also not in the context (undefined):

function matchPattern(pattern, dirname, pathToTest, context) {
  if (typeof pattern === "function") {
    return !!pattern(pathToTest, {
      dirname,
      envName: context.envName,
      caller: context.caller
    });
  }

  if (typeof pathToTest !== "string") {
    throw new Error(`Configuration contains string/RegExp pattern, but no filename was passed to Babel`);
  }

  if (typeof pattern === "string") {
    pattern = (0, _patternToRegex.default)(pattern, dirname);
  }

  return pattern.test(pathToTest);
}

Expected behavior :

pathToTest should be passed into the context to handle it in the Babel config, as stated in the Babel configuration, when it is Babel MatchPatterns for string | RegExp.

Importantly, if either of these are used, Babel requires that the filename option be present, and will consider it an error otherwise.

Workaround using the Function parameter:

(filename: string | void, context: { callee: { name: string } | void, envName: string }) => boolean is a general callback that should return a boolean to indicate whether it is a match or not. The function is passed the filename or undefined if one was not given to Babel. It is also passed the current envName and callee options that were specified by the top-level call to Babel.

    ignore: [(filename, context) => {
      if (filename) {
        // test these RegExp [/core-js/, /@babel(?:\/|\\{1,2})runtime/, /localforage/]
      }
      return false;
    }],

Debug log:

log file content
# content of ts-jest.log :

{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"23.10.5"},"message":"creating Importer singleton","sequence":1,"time":"2018-12-07T10:23:13.874Z"}
{"context":{"allowJs":false,"logLevel":20,"namespace":"jest-preset","package":"ts-jest","version":"23.10.5"},"message":"creating jest presets not handling JavaScript files","sequence":2,"time":"2018-12-07T10:23:13.883Z"}
{"context":{"actualVersion":"23.6.0","expectedVersion":">=22 <24","logLevel":20,"namespace":"versions","package":"ts-jest","version":"23.10.5"},"message":"checking version of jest: OK","sequence":3,"time":"2018-12-07T10:23:13.887Z"}
{"context":{"baseOptions":{},"logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"23.10.5"},"message":"created new transformer","sequence":4,"time":"2018-12-07T10:23:13.887Z"}
{"context":{"fileName":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/core/button/arrowButton.test.tsx","logLevel":20,"namespace":"jest-transformer","package":"ts-jest","transformOptions":{"instrument":false,"rootDir":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib"},"transformerId":1,"version":"23.10.5"},"message":"computing cache key for /Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/core/button/arrowButton.test.tsx","sequence":5,"time":"2018-12-07T10:23:13.889Z"}
{"context":{"logLevel":30,"namespace":"jest-transformer","package":"ts-jest","transformerId":1,"version":"23.10.5"},"message":"no matching config-set found, creating a new one","sequence":6,"time":"2018-12-07T10:23:13.889Z"}
{"context":{"config":{"automock":false,"browser":false,"cache":false,"cacheDirectory":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/.cache/jest","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/","/__.+__/","^.+(.generated.).+$","^.+(.graphql.).+$","^.+(.mock.).+$","^.+(.d.ts)$"],"cwd":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"filter":null,"forceCoverageMatch":[],"globals":{"ts-jest":{"babelConfig":true,"diagnostics":false,"disableSourceMapSupport":false,"enableInternalCache":true,"ignoreCoverageForAllDecorators":true,"ignoreCoverageForDecorators":false,"tsConfig":true,"useExperimentalLanguageServer":false}},"haste":{"providesModuleNodeModules":[]},"moduleDirectories":["node_modules"],"moduleFileExtensions":["web.ts","ts","web.tsx","tsx","web.js","js","web.jsx","jsx","mjs","node","json"],"moduleNameMapper":[["^.+(.module.)(css|sass|scss)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/identityObjProxy.js"]],"modulePathIgnorePatterns":[],"name":"72cc9437578eebc89d8c29099e47c129","prettierPath":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/prettier/index.js","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib","roots":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib"],"runner":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-runner/build/index.js","setupFiles":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/enzymeSetup.js","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/promiseSetup.js","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/polyfills.js"],"skipFilter":false,"snapshotSerializers":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/enzyme-to-json/serializer.js"],"testEnvironment":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-environment-jsdom/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/**/__tests__/**/*.{ts|tsx|mjs|js|jsx}","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/**/?(*.)(spec|test).{ts|tsx|mjs|js|jsx}"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":"","testRunner":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost/","timers":"real","transform":[["^.+\\.(ts|tsx)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/typescriptTransform.js"],["^.+\\.(mjs|js|jsx|node)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/babelTransform.js"],["^.+\\.(graphql|gql)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/graphqlTransform.js"],["^.+\\.(css|scss|sass)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/cssTransform.js"],["^(?!.*\\.(ts|tsx|mjs|js|jsx|node|json|graphql|gql|css|scss|sass)$)","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/fileTransform.js"]],"transformIgnorePatterns":["node_modules/(?!(@swisslife)/)"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"backports","package":"ts-jest","transformerId":1,"version":"23.10.5"},"message":"backporting config","sequence":7,"time":"2018-12-07T10:23:13.889Z"}
{"context":{"jestConfig":{"automock":false,"browser":false,"cache":false,"cacheDirectory":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/.cache/jest","clearMocks":false,"coveragePathIgnorePatterns":["/node_modules/","/__.+__/","^.+(.generated.).+$","^.+(.graphql.).+$","^.+(.mock.).+$","^.+(.d.ts)$"],"cwd":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"filter":null,"forceCoverageMatch":[],"globals":{"ts-jest":{"babelConfig":true,"diagnostics":false,"disableSourceMapSupport":false,"enableInternalCache":true,"ignoreCoverageForAllDecorators":true,"ignoreCoverageForDecorators":false,"tsConfig":true,"useExperimentalLanguageServer":false}},"haste":{"providesModuleNodeModules":[]},"moduleDirectories":["node_modules"],"moduleFileExtensions":["web.ts","ts","web.tsx","tsx","web.js","js","web.jsx","jsx","mjs","node","json"],"moduleNameMapper":[["^.+(.module.)(css|sass|scss)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/identityObjProxy.js"]],"modulePathIgnorePatterns":[],"name":"72cc9437578eebc89d8c29099e47c129","prettierPath":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/prettier/index.js","resetMocks":false,"resetModules":false,"resolver":null,"restoreMocks":false,"rootDir":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib","roots":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib"],"runner":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-runner/build/index.js","setupFiles":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/enzymeSetup.js","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/promiseSetup.js","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/polyfills.js"],"skipFilter":false,"snapshotSerializers":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/enzyme-to-json/serializer.js"],"testEnvironment":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-environment-jsdom/build/index.js","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":["/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/**/__tests__/**/*.{ts|tsx|mjs|js|jsx}","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/src/**/?(*.)(spec|test).{ts|tsx|mjs|js|jsx}"],"testPathIgnorePatterns":["/node_modules/"],"testRegex":"","testRunner":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/jest-jasmine2/build/index.js","testURL":"http://localhost/","timers":"real","transform":[["^.+\\.(ts|tsx)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/typescriptTransform.js"],["^.+\\.(mjs|js|jsx|node)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/babelTransform.js"],["^.+\\.(graphql|gql)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/graphqlTransform.js"],["^.+\\.(css|scss|sass)$","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/cssTransform.js"],["^(?!.*\\.(ts|tsx|mjs|js|jsx|node|json|graphql|gql|css|scss|sass)$)","/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/@sl/rdk.jest-preset-proactive/utils/fileTransform.js"]],"transformIgnorePatterns":["node_modules/(?!(@swisslife)/)"],"watchPathIgnorePatterns":[]},"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"version":"23.10.5"},"message":"normalized jest config","sequence":8,"time":"2018-12-07T10:23:13.890Z"}
{"context":{"logLevel":20,"namespace":"config","package":"ts-jest","transformerId":1,"tsJestConfig":{"babelConfig":{"kind":"file"},"compiler":"typescript","diagnostics":{"ignoreCodes":[6059,18002,18003],"pathRegex":"a^","pretty":true,"throws":false},"isolatedModules":false,"transformers":[],"tsConfig":{"kind":"file"}},"version":"23.10.5"},"message":"normalized ts-jest config","sequence":9,"time":"2018-12-07T10:23:13.890Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","requireResult":{"exists":true,"given":"babel-core","path":"/Users/martin/Vault/SL/Fusion-UI/packages/common-ui-lib/node_modules/babel-core/index.js"},"version":"23.10.5"},"message":"loaded module babel-core","sequence":10,"time":"2018-12-07T10:23:13.936Z"}
{"context":{"logLevel":20,"namespace":"Importer","package":"ts-jest","version":"23.10.5"},"message":"patching babel-core","sequence":11,"time":"2018-12-07T10:23:13.936Z"}
{"context":{"actualVersion":"7.0.0-bridge.0","expectedVersion":">=6 <7 || 7.0.0-bridge.0","logLevel":20,"namespace":"versions","package":"ts-jest","version":"23.10.5"},"message":"checking version of babel-core: OK","sequence":12,"time":"2018-12-07T10:23:13.937Z"}

Minimal repo :

artola commented 5 years ago

@kulshekhar do you have some hint for this issue?

davydkov commented 5 years ago

@artola Have you solved the problem? I encountered the same issue

artola commented 5 years ago

@davydkov It is breaking as described above with v23.6 ... no workaround.

ahnpnl commented 4 years ago

i will close this issue. If you have any issues like this with latest ts-jest, please create another issue.