gatsby: 4.15: Gatsby Script causing existing Jest tests to fail
Preliminary Checks
- This issue is not a duplicate. Before opening a new issue, please search existing issues: https://github.com/gatsbyjs/gatsby/issues
- This issue is not a question, feature request, RFC, or anything other than a bug report directly related to Gatsby. Please post those things in GitHub Discussions: https://github.com/gatsbyjs/gatsby/discussions
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
- test: ignore transformed module by jest * @see: https://github.com/gatsbyjs/gatsby/issues/35736 — committed to literat/literat by literat 2 years ago
- test: ignore transformed module by jest * @see: https://github.com/gatsbyjs/gatsby/issues/35736 — committed to literat/literat by literat 2 years ago
- fix: error references https://github.com/gatsbyjs/gatsby/issues/35736 — committed to swfz/til by swfz 2 years ago
- Work around ESM issue with gatsby-script See https://github.com/gatsbyjs/gatsby/issues/35736 — committed to maxmind/dev-site by oschwald 2 years ago
This workaround works for me: just add a new empty file
gatsby-script.js
in your__mocks__
folder.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
from4.14.1
to4.15.0
.In my environment, changing
transformIgnorePatterns
injest.config.js
fixed the error though I’m not sure this is the correct fix.jest.config.js
:Edit:
My environment:
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.