Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Jest fails when using node 20

Aperta
#342 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript, node.js
Ambito
testing-qa

Direzione di ricerca

Inizia controllando jest.config.js e config/jest-before.js, quindi esegui yarn jest sul branch update/node-20 per riprodurre i fallimenti nei test migrations.js e invalidations.js di layout-grid. Traccia gli errori segnalati di Jest, @wordpress/scripts e uuid, oltre agli errori relativi all'assenza di react-resize-aware, senza presumere che le patch tentate siano sufficienti. Il lavoro è completato quando la suite di Jest passa con Node 20 e il repository può completare l'aggiornamento.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

We tried to upgrade node to version 20 in https://github.com/Automattic/block-experiments/pull/340, however, the effort got blocked due to failures in Jest (https://github.com/Automattic/block-experiments/pull/340#issuecomment-1887293084).

⚠️ 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)
Lingua principale
JavaScript
Stelle
132
Fork
18
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Automattic/block-experiments

Tutte le issue di Automattic/block-experiments

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.