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

When using getViteConfig, the files listed in setupFiles are loaded twice. #12287

Closed
1 task done
koyopro opened this issue Oct 22, 2024 · 4 comments · Fixed by #12312
Closed
1 task done

When using getViteConfig, the files listed in setupFiles are loaded twice. #12287

koyopro opened this issue Oct 22, 2024 · 4 comments · Fixed by #12312
Assignees
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@koyopro
Copy link
Contributor

koyopro commented Oct 22, 2024

Astro Info

Astro                    v4.16.7
Node                     v22.2.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When using getViteConfig instead of defineConfig for Vitest, the files listed in setupFiles are loaded twice.

In the Reproducible Example, when you run npm run test, you can see that console.log is output twice as shown below.

❯ npm run test

stdout | tests/sample.test.ts
setup.ts imported
setup.ts imported

stdout | tests/sample.test.ts
beforeAll
beforeAll

I am having trouble when there is initialization processing that I want to execute only once before running the tests.

What's the expected result?

Even when using getViteConfig, I want the files listed in setupFiles to be loaded only once.

❯ npm run test

stdout | tests/sample.test.ts
setup.ts imported

stdout | tests/sample.test.ts
beforeAll

Link to Minimal Reproducible Example

https://stackblitz.com/github/koyopro/astro-debug/tree/vitest_setup_files?file=tests%2Fsetup.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Oct 22, 2024
@koyopro
Copy link
Contributor Author

koyopro commented Oct 24, 2024

It seems that there is an issue with a plugin named @astro/plugin-middleware.

It looks like the problem can be resolved by modifying the following part.

name: '@astro/plugin-middleware',
config(opts, { command }) {
isCommandBuild = command === 'build';
return opts;
},

  config(opts, { command }) {
    isCommandBuild = command === 'build';
-   return opts;
+   return {};
  },

https://vite.dev/guide/api-plugin.html#config

It can return a partial config object that will be deeply merged into existing config, or directly mutate the config (if the default merging cannot achieve the desired result).

In the config, it is recommended to return only the parts you want to change, rather than the entire modified config.
Since @astro/plugin-middleware does not need to change the config itself, it seems correct to modify it to return an empty object as shown above.

@koyopro
Copy link
Contributor Author

koyopro commented Oct 24, 2024

I'll try to create a PR for the fixes.

@bluwy
Copy link
Member

bluwy commented Oct 25, 2024

Good catch. Yeah I think we can skip returning anything altogether.

@ematipico ematipico added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Oct 25, 2024
@koyopro
Copy link
Contributor Author

koyopro commented Oct 26, 2024

@bluwy Thank you for your comment!
I have submitted a Pull Request, could you please review it?
#12312

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@ematipico @koyopro @bluwy and others