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

feat: support npm setup files #1312

Merged
merged 1 commit into from May 17, 2022
Merged

feat: support npm setup files #1312

merged 1 commit into from May 17, 2022

Conversation

tmkx
Copy link
Contributor

@tmkx tmkx commented May 15, 2022

For example:

{
  test: {
    setupFiles: ['@testing-library/jest-dom/extend-expect.js'],
  },
}

@netlify
Copy link

netlify bot commented May 15, 2022

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit 678a1e6
🔍 Latest deploy log https://app.netlify.com/sites/vitest-dev/deploys/628382f87f09c70008ce3718
😎 Deploy Preview https://deploy-preview-1312--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@@ -122,13 +123,15 @@ export function resolveConfig(
if (process.env.VITEST_MIN_THREADS)
resolved.minThreads = parseInt(process.env.VITEST_MIN_THREADS)

resolved.setupFiles = toArray(resolved.setupFiles || []).map(file => resolve(resolved.root, file))
const require = createRequire(resolve(resolved.root, '_'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const require = createRequire(resolve(resolved.root, '_'))
const require = createRequire(resolve(resolved.root, '_'))

What's the _ for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a placeholder, createRequire needs a file, maybe using index.js more readable?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use resolveModule from local-pkg?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried, resolveModule uses const _require = createRequire(import.meta.url), it's hard code as import.meta.url, but we need relative to root here. otherwise causes some bad cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will incorrectly resolve esm files. We use Vite's resolver, when importing files.

async resolveId(id: string, importer?: string): Promise<ViteNodeResolveId | null> {

const resolveId = async (dep: string, callstackPosition = 1) => {

Maybe we should just remove resolving for non-relative paths here. Reporter field for example supports these kind of pathes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved.setupFiles = toArray(resolved.setupFiles || []).map(file => resolve(resolved.root, file))

The setupFiles will prepend config.root path directly,

{
  test: {
    setupFiles: ['@testing-library/jest-dom/extend-expect.js'],
  },
}

will be transformed to:

{
  test: {
    setupFiles: ['/path/to/@testing-library/jest-dom/extend-expect.js'],
  },
}

it should be:

{
  test: {
    setupFiles: ['/path/to/node_modules/@testing-library/jest-dom/extend-expect.js'],
  },
}

@tmkx tmkx requested review from sheremet-va and antfu May 17, 2022 12:28
@sheremet-va
Copy link
Member

LGTM 👍🏻

@antfu antfu merged commit 7b56822 into vitest-dev:main May 17, 2022
@tmkx tmkx deleted the feat/npm-setup-files branch May 17, 2022 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants