Skip to content

Jest fails when using node 20 #342

Open
@fluiddot

Description

@fluiddot

We tried to upgrade node to version 20 in #340, however, the effort got blocked due to failures in Jest (#340 (comment)).

⚠️ In order to upgrade node to version 20 in this repository, we'd need to address this issue.

Steps to reproduce it:

 FAIL  blocks/layout-grid/src/__tests__/migrations.js
  ● Test suite failed to run

    The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string.
    Consider using the "jsdom" test environment.

    ReferenceError: window is not defined

      at Object.<anonymous> (node_modules/@wordpress/jest-preset-default/scripts/setup-globals.js:2:14)

Seems that apart from upgrading the package @wordpress/scripts, we'd need to update the Jest configuration and upgrade other packages. So far, I tried the following updates but none seemed to work:

1 - Use jsdom as the test environment

  • Apply the following patch:
diff --git forkSrcPrefix/jest.config.js forkDstPrefix/jest.config.js
index 76cd5ec2e813f9b6d68a4ac6655d034eb7657b7f..c07fb045d95b75c6b6b17fa640cca8f45f720e0b 100644
--- forkSrcPrefix/jest.config.js
+++ forkDstPrefix/jest.config.js
@@ -12,6 +12,7 @@ module.exports = {
 		),
 		'./config/jest-before.js',
 	],
+	testEnvironment: 'jsdom',
 	testMatch: [
 		'**/__tests__/**/*.[jt]s',
 		'**/test/*.[jt]s',
  • Run yarn jest

Error:

 FAIL  blocks/layout-grid/src/__tests__/migrations.js
  ● 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/<local-path>/block-experiments/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      2 |  * WordPress dependencies
      3 |  */
    > 4 | import { parse, serialize, rawHandler } from '@wordpress/blocks';
        | ^
      5 | import { registerCoreBlocks } from '@wordpress/block-library';
      6 |
      7 | /**

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@wordpress/blocks/build/api/@wordpress/blocks/src/api/factory.js:4:1)
      at Object.<anonymous> (node_modules/@wordpress/blocks/build/api/@wordpress/blocks/src/api/utils.js:20:1)
      at Object.<anonymous> (node_modules/@wordpress/blocks/build/store/@wordpress/blocks/src/store/reducer.js:15:1)
      at Object.<anonymous> (node_modules/@wordpress/blocks/build/store/@wordpress/blocks/src/store/index.js:9:1)
      at Object.<anonymous> (node_modules/@wordpress/blocks/build/@wordpress/blocks/src/index.js:11:1)
      at Object.require (blocks/layout-grid/src/__tests__/migrations.js:4:1)

2 - Issue with package uuid

  • Apply the following patch:
diff --git forkSrcPrefix/jest.config.js forkDstPrefix/jest.config.js
index 76cd5ec2e813f9b6d68a4ac6655d034eb7657b7f..2d5af5d1128c82ff454f67719e4fd03cdf334099 100644
--- forkSrcPrefix/jest.config.js
+++ forkDstPrefix/jest.config.js
@@ -12,6 +12,7 @@ module.exports = {
 		),
 		'./config/jest-before.js',
 	],
+	testEnvironment: 'jsdom',
 	testMatch: [
 		'**/__tests__/**/*.[jt]s',
 		'**/test/*.[jt]s',
@@ -24,5 +25,10 @@ module.exports = {
 		'^.+\\.[jt]sx?$': 'babel-jest',
 		'^.+\\.(css|scss)$': 'jest-transform-css',
 		'\\.(jpg|jpeg|png|gif|webp|svg)$': 'jest-transform-file',
-	}
+	},
+	moduleNameMapper: {
+		// Workaround for Jest not having ESM support yet
+		// Reference: https://t.ly/9ap_
+		'^uuid$': require.resolve( 'uuid' ),
+	},
 };

Error:

 FAIL  blocks/layout-grid/src/__tests__/migrations.js
  ● 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/fluiddot/workspace/gutenberg-mobile/block-experiments/node_modules/@wordpress/block-library/node_modules/is-plain-obj/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export default function isPlainObject(value) {
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      3 |  */
      4 | import { parse, serialize, rawHandler } from '@wordpress/blocks';
    > 5 | import { registerCoreBlocks } from '@wordpress/block-library';
        | ^
      6 |
      7 | /**
      8 |  * Internal dependencies

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.<anonymous> (node_modules/@wordpress/block-library/node_modules/@wordpress/data/build/plugins/persistence/@wordpress/data/src/plugins/persistence/index.js:4:1)
      at Object.<anonymous> (node_modules/@wordpress/block-library/node_modules/@wordpress/data/build/plugins/@wordpress/data/src/plugins/index.js:1:1)
      at Object.<anonymous> (node_modules/@wordpress/block-library/node_modules/@wordpress/data/build/@wordpress/data/src/index.js:10:1)
      at Object.<anonymous> (node_modules/@wordpress/block-library/node_modules/@wordpress/blocks/build/store/@wordpress/blocks/src/store/index.js:4:1)
      at Object.<anonymous> (node_modules/@wordpress/block-library/node_modules/@wordpress/blocks/build/@wordpress/blocks/src/index.js:11:1)
      at Object.<anonymous> (node_modules/@wordpress/block-library/build/@wordpress/block-library/src/index.js:4:1)
      at Object.require (blocks/layout-grid/src/__tests__/migrations.js:5:1)

 FAIL  blocks/layout-grid/src/__tests__/invalidations.js
  ● Test suite failed to run

    Cannot find module 'react-resize-aware' from 'node_modules/@wordpress/block-editor/node_modules/@wordpress/compose/build/hooks/use-resize-observer/index.js'

    Require stack:
      node_modules/@wordpress/block-editor/node_modules/@wordpress/compose/build/hooks/use-resize-observer/index.js
      node_modules/@wordpress/block-editor/node_modules/@wordpress/compose/build/index.js
      node_modules/@wordpress/block-editor/build/hooks/align.js
      node_modules/@wordpress/block-editor/build/hooks/index.js
      node_modules/@wordpress/block-editor/build/index.js
      blocks/layout-grid/src/grid/edit.js
      blocks/layout-grid/src/index.js
      blocks/layout-grid/src/__tests__/invalidations.js

       9 |  */
      10 |
    > 11 | import {
         | ^
      12 | 	InnerBlocks,
      13 | 	InspectorControls,
      14 | 	BlockControls,

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.<anonymous> (node_modules/@wordpress/block-editor/node_modules/@wordpress/compose/build/hooks/use-resize-observer/@wordpress/compose/src/hooks/use-resize-observer/index.js:4:1)
      at Object.<anonymous> (node_modules/@wordpress/block-editor/node_modules/@wordpress/compose/build/@wordpress/compose/src/index.js:32:1)
      at Object.<anonymous> (node_modules/@wordpress/block-editor/build/hooks/@wordpress/block-editor/src/hooks/align.js:10:1)
      at Object.<anonymous> (node_modules/@wordpress/block-editor/build/hooks/@wordpress/block-editor/src/hooks/index.js:5:1)
      at Object.<anonymous> (node_modules/@wordpress/block-editor/build/@wordpress/block-editor/src/index.js:4:1)
      at Object.require (blocks/layout-grid/src/grid/edit.js:11:1)
      at Object.require (blocks/layout-grid/src/index.js:12:1)
      at Object.require (blocks/layout-grid/src/__tests__/invalidations.js:9:1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions