gatsby: 4.15: Gatsby Script causing existing Jest tests to fail

Preliminary Checks

Description

Running on working version of 4.14.1, then updating to Gatsby 4.15.0 (and updating all other gatsby-* versions likewise) causes a working Gatsby install which includes Jest to fail for some tests due to gatsby-script, even if there is no direct use of the <Script> tag.

Error returns is:

 ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/MYNAME/site/node_modules/gatsby-script/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from "./gatsby-script";
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1773:14)
      at Object.require (node_modules/gatsby/cache-dir/commonjs/gatsby-browser-entry.js:45:21)

Reproduction Link

none yet

Steps to Reproduce

Have a working 4.14.1 Gatsby site up and working, including jest tests Upgrade to 4.15.0

Expected Result

4.15.0 is a dropin replacement, or there is documentation on what needs to change

Actual Result

Errors on most, but not all tests.

Environment

Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 8.10.0 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Safari: 15.5
  npmPackages:
    gatsby: ^4.15.0 => 4.15.0
    gatsby-image: ^3.11.0 => 3.11.0
    gatsby-link: ^4.15.0 => 4.15.0
    gatsby-plugin-canonical-urls: ^4.15.0 => 4.15.0
    gatsby-plugin-google-fonts: ^1.0.1 => 1.0.1
    gatsby-plugin-google-gtag: ^4.15.0 => 4.15.0
    gatsby-plugin-html-attributes: ^1.0.5 => 1.0.5
    gatsby-plugin-manifest: ^4.15.0 => 4.15.0
    gatsby-plugin-netlify: ^4.4.0 => 4.4.0
    gatsby-plugin-offline: ^5.15.0 => 5.15.0
    gatsby-plugin-purgecss: ^6.1.2 => 6.1.2
    gatsby-plugin-robots-txt: ^1.7.1 => 1.7.1
    gatsby-plugin-sharp: ^4.15.0 => 4.15.0
    gatsby-plugin-sitemap: ^5.15.0 => 5.15.0
    gatsby-plugin-styled-components: ^5.15.0 => 5.15.0
    gatsby-source-filesystem: ^4.15.0 => 4.15.0
    gatsby-transformer-sharp: ^4.15.0 => 4.15.0

Config Flags

no

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 18 (8 by maintainers)

Commits related to this issue

Most upvoted comments

This workaround works for me: just add a new empty file gatsby-script.js in your __mocks__ folder.

I’ve got a complex gatsby-node.js setup that performs many additional tasks and verifications.

I’m running into this issue too when performing the build (everything worked fine until gatsby-script was introduced in v4.15). I guess others might run into this issue too.

Also having the same issue:

gatsby-script/dist/index.js:1 export * from “./gatsby-script”; ^^^^^^ SyntaxError: Unexpected token ‘export’

I met the same error after upgrading gatsby from 4.14.1 to 4.15.0.

In my environment, changing transformIgnorePatterns in jest.config.js fixed the error though I’m not sure this is the correct fix.

jest.config.js:

-  transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
+  transformIgnorePatterns: [`node_modules/(?!(gatsby|gatsby-script)/)`],

Edit:

My environment:


  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: 102.0.5005.61
    Firefox: 98.0.2
    Safari: 15.5
  npmPackages:
    gatsby: ^4.0.1 => 4.15.0 
    gatsby-plugin-canonical-urls: ^4.0.0 => 4.15.0 
    gatsby-plugin-catch-links: ^4.0.0 => 4.15.0 
    gatsby-plugin-feed: ^4.0.0 => 4.15.0 
    gatsby-plugin-gatsby-cloud: ^4.0.0 => 4.15.0 
    gatsby-plugin-google-gtag: ^4.0.0 => 4.15.0 
    gatsby-plugin-image: ^2.0.0 => 2.15.0 
    gatsby-plugin-manifest: ^4.0.0 => 4.15.0 
    gatsby-plugin-nprogress: ^4.0.0 => 4.15.0 
    gatsby-plugin-offline: ^5.0.0 => 5.15.0 
    gatsby-plugin-react-helmet: ^5.0.0 => 5.15.0 
    gatsby-plugin-sharp: ^4.0.0 => 4.15.0 
    gatsby-plugin-sitemap: ^5.0.0 => 5.15.0 
    gatsby-remark-autolink-headers: ^5.0.0 => 5.15.0 
    gatsby-remark-copy-linked-files: ^5.0.0 => 5.15.0 
    gatsby-remark-images: ^6.0.0 => 6.15.0 
    gatsby-remark-prismjs: ^6.0.0 => 6.15.0 
    gatsby-source-filesystem: ^4.0.0 => 4.15.0 
    gatsby-transformer-remark: ^5.0.0 => 5.15.0 
    gatsby-transformer-sharp: ^4.0.0 => 4.15.0 

Related docs pages:

I can’t look in depth until the weekend, but I do use const { graphql } = require("gatsby");, despite apparently not needing it, and that will probably recreate the bug.

Hi @manusa @g-pex-aerian @tony, can you provide minimal reproductions?

I’ve got a complex gatsby-node.js setup that performs many additional tasks and verifications.

I’m running into this issue too when performing the build (everything worked fine until gatsby-script was introduced in v4.15). I guess others might run into this issue too.

thanks. Changing that works for me now too.

Hi @miWatch10 and @gh640, thanks for raising this.

I attempted to reproduce minimally in https://github.com/tyhopp/gatsby-script-jest-repro but wasn’t able to. Can one of you provide a minimal reproduction so we can take this forward?

Edit - Able to reproduce successfully in https://github.com/tyhopp/gatsby-script-jest-repro/commit/4edb24d31a4c8e77bbfd51ca11d8da1ea30398c8, needed to import something from gatsby to see the error.