-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Open
Description
Whether a file with a .js extension is supposed to contain ESM or CommonJS code is determined by whether the nearest package.json sets the type field to module. Most of the packages in Gutenberg do not have the type field, meaning that the .js files in build-module are incorrectly interpreted as CommonJS by various tooling.
This happens to work ok with bundlers such as Webpack and transpilers such as Babel, since they are very lenient in what they accept. On the other hand, Jest running in native-ESM mode (rather than transpiling everything through Babel) chokes on this.
● 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.
[...]
Details:
/tmp/test/node_modules/@wordpress/dataviews/build-module/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as DataViews } from './components/dataviews';
^^^^^^
SyntaxError: Unexpected token 'export'
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
There are several ways to fix this:
- Name the files in
build-module(and any other ESM-containing files) with the.mjsextension instead of.js. - Set
typetomodulein package.json, and name any files containing CommonJS code with the.cjsextension instead of.js. - Since it’s the nearest
package.jsonrather than the package’spackage.jsonthat counts, you could add additionalpackage.jsonfiles in subdirs to change thetypesetting. Note these would also need to copysideEffectsand other fields, so this probably isn’t a very good solution.
Metadata
Metadata
Assignees
Labels
No labels