You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When running tests for Yjs with react-scripts test, import of lib0/webcrypto inside lib0/dist/random.cjs causes test failure with "SyntaxError: Unexpected token 'export'".
FAIL src/yjs.test.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:
/workspaces/lib0-webcrypto-jest-fail/node_modules/lib0/webcrypto.js:3
export const subtle = crypto.subtle
^^^^^^
SyntaxError: Unexpected token 'export'> 1 | const yjs = require('yjs');| ^
2 |
3 |describe('yjs', () => {
4 |it('fails', () => {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
at Object.<anonymous> (src/yjs.test.js:1:13)
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.395 s
To Reproduce
Steps to reproduce the behavior:
Create empty CRA project with npx create-react-app example-app
Thanks for the repository! I fiddled around with it a bit.
I don't think 0.2.88 broke jest. It probably broke when webcrypto was introduced to lib0.
lib0 makes a lot of use of the package exports feature to link to "native" alternatives (the browser uses browser webcrypto, nodejs uses nodejs apis).
package.json exports is a stable feature of nodejs. Pretty much all module bundlers support it. They are very useful.. I'm sorry that they are not well supported in jest..
My best bet would be to try explicitly setting a mapping from lib0/webcrypto to the desired file:
I tried explicitly mapping lib0/webcrypto to the cjs file by setting moduleNameMapper:
Describe the bug
When running tests for Yjs with
react-scripts test
, import oflib0/webcrypto
insidelib0/dist/random.cjs
causes test failure with "SyntaxError: Unexpected token 'export'".To Reproduce
Steps to reproduce the behavior:
npx create-react-app example-app
npm add [email protected]
src/yjs.test.js
with the content:npm run test -- --watchAll=false
Expected behavior
The test should not fail.
Environment Information
[email protected]
,[email protected]
Additional context
I created public repository for reproducing the issue in https://github.com/krotovic/lib0-webcrypto-jest-fail
The text was updated successfully, but these errors were encountered: