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

refactor: use vite-node to load globalSetup #624

Merged
merged 4 commits into from
Jan 27, 2022

Conversation

dominikg
Copy link
Contributor

and add test launching a vite instance

suggested by @antfu on discord
related: #522

@netlify
Copy link

netlify bot commented Jan 24, 2022

✔️ Deploy Preview for vitest-dev ready!

🔨 Explore the source changes: b8752ea

🔍 Inspect the deploy log: https://app.netlify.com/sites/vitest-dev/deploys/61f04f70b0837e0008ab6026

😎 Browse the preview: https://deploy-preview-624--vitest-dev.netlify.app

@dominikg
Copy link
Contributor Author

addition of .gitattributes and .editorconfig was done to enforce LF line endings on all platforms. see vitejs/vite#5092

@@ -8,12 +10,23 @@ interface GlobalSetupFile {
}

async function loadGlobalSetupFiles(server: ViteDevServer): Promise<GlobalSetupFile[]> {
const node = new ViteNodeServer(server)
Copy link
Member

@sheremet-va sheremet-va Jan 25, 2022

Choose a reason for hiding this comment

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

can't you reuse vitenode from ctx? it relies on user config

I think it's undefined at the initialization, but you can pass the whole context to GlobalPlugin, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

loadGlobalSetupFiles is called in buildStart hook, so undefined at init wouldn't be an issue.

globalSetupFiles = await loadGlobalSetupFiles(server)

i'm not sure how to get/pass ctx into GlobalPlugin though, can you elaborate on that?

got the code to init the ViteNodeRunner from here

const node = new ViteNodeServer(server)

Copy link
Member

Choose a reason for hiding this comment

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

Is that possible to run the global setup after the vite server starts? Or what will that cause?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

globalSetup should run once before any testfile is executed and teardown once after all testfiles are done. As long as that is true, it's ok to not execute it on vite server start or even before (that would make it impossible to use the vite server for loading the files anyways).

Copy link
Member

@sheremet-va sheremet-va Jan 25, 2022

Choose a reason for hiding this comment

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

loadGlobalSetupFiles is called in buildStart hook, so undefined at init wouldn't be an issue.

globalSetupFiles = await loadGlobalSetupFiles(server)

i'm not sure how to get/pass ctx into GlobalPlugin though, can you elaborate on that?

got the code to init the ViteNodeRunner from here

const node = new ViteNodeServer(server)

You can pass ctx to your plugin when initializing VitestPlugins:

export async function VitestPlugin(options: UserConfig = {}, viteOverrides: ViteUserConfig = {}, ctx = new Vitest()): Promise<VitePlugin[]> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ahh, yes that works although it only saves a little bit of code because the runner would still have to be inizialized. Maybe it is even preferable to use a separate instance to avoid globalSetup polluting the main instance?

On the flipside maybe we could/should pass the ctx down into setup so that it can be used in there and also maybe as a possible backchannel to add something to test contexts?

Copy link
Member

Choose a reason for hiding this comment

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

ahh, yes that works although it only saves a little bit of code because the runner would still have to be inizialized. Maybe it is even preferable to use a separate instance to avoid globalSetup polluting the main instance?

On the flipside maybe we could/should pass the ctx down into setup so that it can be used in there and also maybe as a possible backchannel to add something to test contexts?

Well, it at least should use test config, I think. Or otherwise deps and transformMode will have no effect in user config.

this.vitenode = new ViteNodeServer(server, this.config)

Comment on lines +15 to +24
const runner = new ViteNodeRunner({
root: server.config.root,
base: server.config.base,
fetchModule(id) {
return node.fetchModule(id)
},
resolveId(id, importer) {
return node.resolveId(id, importer)
},
})
Copy link
Member

Choose a reason for hiding this comment

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

Could we also reuse the vite-node server?

Copy link
Contributor Author

@dominikg dominikg Jan 25, 2022

Choose a reason for hiding this comment

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

you mean the runner? The only 2 instances i found of that are in the cli not exposed/sharable and in execute but thats wrapped in VitestRunner and we don't want/need mocking for globalSetup files 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Ah, never mind, I misread :P

@antfu antfu merged commit 5eebd60 into vitest-dev:main Jan 27, 2022
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

3 participants