lib0
lib0 copied to clipboard
Version 0.2.88 breaks testing Yjs with react-scripts test
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
- Add dependencies via
npm add [email protected]
- Create test
src/yjs.test.js
with the content:
const yjs = require('yjs');
describe('yjs', () => {
it('fails', () => {
expect(yjs).toBeDefined();
});
});
- Run tests with
npm run test -- --watchAll=false
- See error
Expected behavior The test should not fail.
Environment Information
- Node.js
-
[email protected]
,[email protected]
Additional context I created public repository for reproducing the issue in https://github.com/krotovic/lib0-webcrypto-jest-fail
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
:
// jest.config.js
moduleNameMapper: {
'lib0/webcrypto': '<rootDir>/node_modules/lib0/dist/webcrypto.cjs'
}
However, when I set a custom jest config, I can't start using react-scripts anymore.
If you find a solution, it would be great if you could post it here, as it could help others!