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

babel-jest createTransformer removed from Typescript? #11444

Closed
broksonic21 opened this issue May 25, 2021 · 15 comments · Fixed by #12399
Closed

babel-jest createTransformer removed from Typescript? #11444

broksonic21 opened this issue May 25, 2021 · 15 comments · Fixed by #12399

Comments

@broksonic21
Copy link

broksonic21 commented May 25, 2021

🐛 Bug Report

In babel-jest 26.6.3, the index.d.ts is this (containing createTransformer):

import { TransformOptions } from '@babel/core';
import type { Transformer } from '@jest/transform';
interface BabelJestTransformer extends Transformer {
    canInstrument: true;
}
declare const transformer: BabelJestTransformer & {
    createTransformer: (options?: TransformOptions) => BabelJestTransformer;
};
export = transformer;

But in 27.0.1

it is

import { TransformOptions } from '@babel/core';
import type { SyncTransformer } from '@jest/transform';
declare const transformer: SyncTransformer<TransformOptions>;
export default transformer;

so createTransformer is no longer exposed. That usage is documented on lots of sites as the way to use babel-jest - i.e. https://babeljs.io/docs/en/config-files for example, which has you call like this, which no longer works:

module.exports = require("babel-jest").createTransformer({
  rootMode: "upward",
});

Is this intentional to change the typescript to drop createTransformer? If so, can the doc be updated, and what's the alternative?

To Reproduce

Steps to reproduce the behavior:

  • Have babel-jest 26.6.3
  • Upgrade it to 27.0.1 (in my case, I also was updating Jest for the same versions too)

Expected behavior

  • Either typescript still exposes createTransformer
  • or doc is updated with new steps

envinfo

26.6.3:

  System:
    OS: macOS 11.3.1
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  Binaries:
    Node: 15.12.0 - /usr/local/bin/node
    npm: 7.6.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.6.3 => 26.6.3

27.0.1:

  System:
    OS: macOS 11.3.1
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
  Binaries:
    Node: 15.12.0 - /usr/local/bin/node
    npm: 7.6.0 - /usr/local/bin/npm
  npmPackages:
    jest: ^27.0.1 => 27.0.1
@sibelius
Copy link

I can confirm that createTransformer is not working anymore for us

TypeError: createTransformer is not a function
packages/main/test/babel-transformer.js

@sibelius
Copy link

I think you need to do like this

const { configWeb } = require('babelconfig.js');

const transformer = require('babel-jest');

module.exports = transformer.createTransformer({
  ...configWeb,
});

@sibelius
Copy link

this does not work as well

@SimenB
Copy link
Member

SimenB commented May 25, 2021

using require requires (hah) you to do require('babel-jest').default.createTransformer. Somewhat cleaner with fake ESM (like TypeScript or Babel), but unfortunately just as ugly with real ESM (double default) 😞 We'll probably migrate to emit proper ESM this summer when we drop node 10.

We might wanna export createTransformer in addition to sticking it on the default export? I didn't do it since Jest itself picks up createTransformer calls and calls it, but only on default - not as a named export.

@thymikee @jeysal thoughts?

@sibelius
Copy link

require('babel-jest').default.createTransformer is also breaking for us

@SimenB
Copy link
Member

SimenB commented May 25, 2021

really? that works fine for me.

$ node -p "require('babel-jest').default.createTransformer"
[Function: createTransformer]

@broksonic21
Copy link
Author

once resolved, can the docs be updated ASAP with the supported solution? https://babeljs.io/docs/en/config-files

@SimenB
Copy link
Member

SimenB commented May 25, 2021

Babel docs are not kept in this repo

@SimenB
Copy link
Member

SimenB commented May 25, 2021

(/cc @nicolo-ribaudo FYI)

@sibelius
Copy link

it is working now, maybe it was can cache issue

@broksonic21
Copy link
Author

the .default also worked for me.

DanailMinchev added a commit to DanailMinchev/gatsby-starter-testing that referenced this issue May 25, 2021
darekkay added a commit to darekkay/darekkay-scripts that referenced this issue May 29, 2021
@nwalters512
Copy link
Contributor

@SimenB although require('babel-jest').default.createTransformer works at runtime, as @broksonic21 pointed out in the original issue message, attempting to access createTransformer in a TypeScript file breaks because the type for the default export no longer reflects the existence of a createTransformer function:

import { TransformOptions } from '@babel/core';
-import type { Transformer } from '@jest/transform';
-interface BabelJestTransformer extends Transformer {
-    canInstrument: true;
-}
-declare const transformer: BabelJestTransformer & {
-    createTransformer: (options?: TransformOptions) => BabelJestTransformer;
-};
-export = transformer;
+import type { SyncTransformer } from '@jest/transform';
+declare const transformer: SyncTransformer<TransformOptions>;
+export default transformer;

I believe your suggestion to "export createTransformer in addition to sticking it on the default export" would resolve this; I'd be happy to open a PR with this change.

@dcwarwick
Copy link

dcwarwick commented Jun 7, 2021

Just to note that we've also hit this issue, but not using the typescript at all: just a regular JS transform using the conventional pattern as currently given in the docs.

Incidentally, here's a link to the docs in question, which give code that no longer works:
https://babeljs.io/docs/en/config-files#jest

Note: code something like the following provides a workaround which runs both on previous and newer versions, in case that's needed by anyone:

const babelJestMd = require('babel-jest');
const babelJest = babelJestMd.__esModule ? babelJestMd.default : babelJestMd;

module.exports = babelJest.createTransformer({
  // babel options
});

I note that Jest itself uses jest-utils interopRequireDefault to ensure everything it loads has a default field whichever pattern it was using.

drwpow pushed a commit to FredKSchott/snowpack that referenced this issue Oct 19, 2021
* Upgrade babel-jest version

This is to match with the current version of Jest. Also Jest is added
as a peer dependency to reflect this.

* Change the babel jest transform script

This is done to reflect the new API changes in babel-jest.
The second line is done so that it can stay backwards compatible with
Jest 26 if needed.

References:
#3398 (comment)
jestjs/jest#11444 (comment)

* Use jsdom as a test environment to run jest

This is done so the react testing-library can render the DOM elements
in testing.
fanck0605 added a commit to fanck0605/craco that referenced this issue Dec 19, 2021
@nwalters512
Copy link
Contributor

I opened a PR to add a named export for createTransformer: #12399.

@SimenB
Copy link
Member

SimenB commented Feb 15, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants