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

Undefined createTransformer function in jest-preprocess.js when following Gatsby unit testing instructions #31618

Closed
amcgregor101 opened this issue May 27, 2021 · 2 comments · Fixed by #31649
Labels
good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. type: documentation An issue or pull request for improving or updating Gatsby's documentation

Comments

@amcgregor101
Copy link

Description

When following the Gatsby unit testing instructions, all tests fail to run because the createTransformer function in the object returned by require("babel-jest") is undefined.

Steps to reproduce

gatsby new my-gatsby-project https://github.com/gatsbyjs/gatsby-starter-default
cd my-gatsby-project/
npm install --save-dev jest babel-jest react-test-renderer babel-preset-gatsby identity-obj-proxy
[copy various files listed in unit testing instructions]
[add a basic test case to src/__tests__/header.js (see below)]
npm test

I used the following test case in my reproduction:

test("test", () => {
	expect(true).toBe(true)
})

Link to a reproduction: https://github.com/amcgregor101/gatsby-testing-bug

Expected result

The test passes.

Actual result

I get the following error:

TypeError: require(...).createTransformer is not a function
    at Object.<anonymous> (/home/***/my-gatsby-project/jest-preprocess.js:4:40)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at requireOrImportModule (/home/***/my-gatsby-project/node_modules/jest-util/build/requireOrImportModule.js:51:28)
    at /home/***/my-gatsby-project/node_modules/@jest/transform/build/ScriptTransformer.js:381:73
    at Array.map (<anonymous>)

Environment

$ gatsby info --clipboard

  System:
    OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (8) x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 12.13.0 - /usr/local/bin/node
    Yarn: 1.22.10 - ~/.npm-global/bin/yarn
    npm: 7.14.0 - ~/.npm-global/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 91.0.4472.77
    Firefox: 88.0.1
  npmPackages:
    gatsby: ^3.4.1 => 3.4.1
    gatsby-plugin-gatsby-cloud: ^2.4.1 => 2.4.1
    gatsby-plugin-image: ^1.4.0 => 1.4.0
    gatsby-plugin-manifest: ^3.4.0 => 3.4.0
    gatsby-plugin-offline: ^4.4.0 => 4.4.0
    gatsby-plugin-react-helmet: ^4.4.0 => 4.4.0
    gatsby-plugin-sharp: ^3.4.1 => 3.4.1
    gatsby-source-filesystem: ^3.4.0 => 3.4.0
    gatsby-transformer-sharp: ^3.4.0 => 3.4.0
  npmGlobalPackages:
    gatsby-cli: 3.6.0

@amcgregor101 amcgregor101 added the type: bug An issue or pull request relating to a bug in Gatsby label May 27, 2021
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label May 27, 2021
@LekoArts LekoArts added type: documentation An issue or pull request for improving or updating Gatsby's documentation good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer type: bug An issue or pull request relating to a bug in Gatsby labels May 28, 2021
@LekoArts
Copy link
Contributor

LekoArts commented May 28, 2021

The documentation was written with jest/babel-jest v26 in mind, and just three days ago jest/babel-jest v27 was released. I'm pretty sure that's the reason why it's not working anymore as in repositories of mine with this exact setup (but v26) it still works.

If anyone can comb through the breaking changes https://github.com/facebook/jest/blob/master/CHANGELOG.md#2700 and see what needs to be changed: Would be highly appreciated!

@sj-rai
Copy link
Contributor

sj-rai commented May 29, 2021

@LekoArts I went through the changelog for jest v.27.0.0 and found that one of the changes included the function to be exported as default, so the code snippet in this documentation would throw an error.

I have created a PR to update the docs, #31649 . But since createTransformer is used in other places with the project, they may need to be updated if Jest is updated to v27.0.0 or above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that doesn't require previous experience with Gatsby help wanted Issue with a clear description that the community can help with. type: documentation An issue or pull request for improving or updating Gatsby's documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants