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

Support named export in a CommonJS module in node_modules #4745

Closed
4 tasks done
Jinjiang opened this issue Dec 14, 2023 · 5 comments
Closed
4 tasks done

Support named export in a CommonJS module in node_modules #4745

Jinjiang opened this issue Dec 14, 2023 · 5 comments

Comments

@Jinjiang
Copy link

Clear and concise description of the problem

Please don't mind me creating this issue from a discussion #1360 to keep on track.

Reproduction
https://github.com/Jinjiang/reproductions/tree/vitest-apollo-20231214

The package in my case is @apollo/client. In source code import { ... } from ... works. However, when it's written in a file inside node_modules, it doesn't work unless using the import pkg ... statement.

// THIS DOESN'T WORK NOW
import { gql } from '@apollo/client';

// THIS WORKS
import pkg from '@apollo/client';
const { gql } = pkg;

Suggested solution

Support named export in CJS package like @apollo/client

Alternative

No response

Additional context

No response

Validations

@sheremet-va
Copy link
Member

This is expected behaviour, this is how Node.js works. To bypass this, you can use server.deps.inline to inline all dependencies in the dependency chain that imports this file.

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2023
@Jinjiang
Copy link
Author

I see. I did a quick trial. Seems adding server: { deps: { inline: true } } to the test config doesn't help. Not sure what is the proper config to bypass. May I double check that with you? Thanks.

Error log:

$ pnpm test

> reproduce@ test /xxx/reproductions
> vitest


 RUN  v1.0.4 /xxx/reproductions


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Error: Failed to load url @vitest/expect (resolved id: @vitest/expect). Does the file exist?
 ❯ loadAndTransform node_modules/.pnpm/vite@5.0.8/node_modules/vite/dist/node/chunks/dep-uAHLeuC6.js:49374:21
 ❯ ViteNodeServer._transformRequest node_modules/.pnpm/vite-node@1.0.4/node_modules/vite-node/dist/server.mjs:393:16
 ❯ ViteNodeServer._fetchModule node_modules/.pnpm/vite-node@1.0.4/node_modules/vite-node/dist/server.mjs:359:17
 ❯ MessagePort.<anonymous> node_modules/.pnpm/vitest@1.0.4/node_modules/vitest/dist/vendor/index.cAUulNDf.js:60:20

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 Test Files  no tests
      Tests  no tests
     Errors  1 error
   Start at  21:32:25
   Duration  304ms (transform 35ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 0ms)

 ELIFECYCLE  Test failed. See above for more details.

@sheremet-va
Copy link
Member

sheremet-va commented Dec 14, 2023

You need to add a dependency that imports @apollo/client:

export default {
  test: {
    server: {
      deps: {
        inline: ['foo']
      }
    }
  }
}

@Jinjiang
Copy link
Author

It works with the config you provided. Thanks so much.

May I have one more last question if you don't mind 😅 ? Why it failed with error about @vitest/expect when I set server.deps.inline as true? What happened behind?

@sheremet-va
Copy link
Member

It works with the config you provided. Thanks so much.

May I have one more last question if you don't mind 😅 ? Why it failed with error about @vitest/expect when I set server.deps.inline as true? What happened behind?

#2806

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

No branches or pull requests

2 participants