Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.20.2]: Tets fails if importing tslib #1753

Closed
6 tasks done
thebuilder opened this issue Jul 31, 2022 · 10 comments · Fixed by #1778
Closed
6 tasks done

[0.20.2]: Tets fails if importing tslib #1753

thebuilder opened this issue Jul 31, 2022 · 10 comments · Fixed by #1778

Comments

@thebuilder
Copy link

Describe the bug

After upgrading to v0.20.x, most of my tests are now failing with:

SyntaxError: Named export '__assign' not found. The requested module 'tslib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'tslib';
const { __assign } = pkg;

It's caused by framer-motion using tslib. The package does include an exports field and module support, so a bit weird why this is popping up. Thinking it's related to how node modules are resolved now after #1673.

Reproduction

The issue can be replicated by importing <framer-motion> in a test file.
https://stackblitz.com/edit/vitest-dev-vitest-ue9bu2?file=test/App.test.tsx

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    @vitejs/plugin-react: ^1.3.2 => 1.3.2 
    vite: ^2.9.9 => 2.9.14 
    vitest: 0.20.2 => 0.20.2

Used Package Manager

npm

Validations

@metonym
Copy link

metonym commented Jul 31, 2022

I'm seeing a similar error after upgrading to vitest^0.20 when using @testing-library/user-event which has @testing-library/dom as a dependency.

SyntaxError: Named export 'createEvent' not found. The requested module '@testing-library/dom' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@testing-library/dom';
const { createEvent: createEventBase } = pkg;

@saharbit
Copy link

I'm experiencing a similar issue in the latest version(0.20.2), on some very basic tests

@thebuilder
Copy link
Author

Looks like tslib is not following the ESM standard correctly, and guessing it's the same issue with other packages. 😑
microsoft/tslib#173

@thebuilder
Copy link
Author

Setting deps.registerNodeLoader to false (to skip the new Node logic) allows the test to run again.

@sheremet-va
Copy link
Member

Yes, for now you can opt-out of this behaviour with deps.registerNideLoader. I guess we can hardcode tslib for now, since it's used in TypeScript extensively, but the project seems to be abandoned according to linked issues.

@sheremet-va
Copy link
Member

I'm experiencing a similar issue in the latest version(0.20.2), on some very basic tests

Please, create a reproduction, if the issue doesn't involve tslib.

@ggowtham67
Copy link

ggowtham67 commented Aug 1, 2022

Getting similar issue with CJS export. In my case it's with one of cheerio packages's dep.

SyntaxError: Named export 'AttributeAction' not found. The requested module 'css-what' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'css-what';
const { AttributeAction, SelectorType } = pkg;

Reproduction: https://github.com/ggowtham67/vitest-v0.20.2-cjs-broken-export-repro

Edit: Solution suggested by @thebuilder works <3

Setting deps.registerNodeLoader to false (to skip the new Node logic) allows the test to run again.

@revmischa
Copy link

Getting this as well

SyntaxError: Named export 'v4' not found. The requested module 'uuid' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

@metonym
Copy link

metonym commented Aug 5, 2022

Upgrading vitest to v0.21.0 now solves the issue.

deps.registerNodeLoader is now false by default so it can be removed from the config.

/** @type {import('vite').UserConfig} */
export default {
  test: {
-   registerNodeLoader: false,
  },
};

@sheremet-va
Copy link
Member

Upgrading vitest to v0.21.0 now solves the issue.

Technically this PR should also work with registerNodeLoader enabled, but performance might suffer for now.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
6 participants