-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Hooks not working when loaded from an external vitest.setup.js file #3403
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
Comments
Without looking at the reproduction yet, it doesn't sound like it's a bug. Externalized dependencies are imported by Node.js and cached by Node.js. When second test starts, Vitest imports setup files but since it's already evaluated, Node doesn't run it. There is no way to clear ESM cache in Node.js. This can only happen when tests are running in a single context where cache is shared between tests. You can bypass it by specifying setup file in |
Thanks for your reaction again @sheremet-va ❤
The fact that the file is only loaded once shouldn't mean that
Interesting... this does seem to work, but only when I put it as a regex: export default defineConfig({
test: {
// ...
deps: { inline: [/vitest.setup.js/] }
},
}) The full file path or relative file path doesn't seem to work: |
All hooks are removed between each test file.
The string is the dependency name. Regexp is checked against the full file path. |
So you disagree that this is a bug?
What would the dependency name be for this file? I just yesterday realized that the |
No, there is no way to do that when you run tests in a single thread. The fact that you run all test files in the same global context is some kind of workaround and there is no abstraction for it. The highest abstraction we operate upon is the test file. You can also use
I don't know 🤷🏻 When a string is received, |
Right. This workaround works for us: we're using I personally think that "external" vs. not-external is not something users should be aware of when configuring a setup file. So I would like to keep this issue open. If you disagree, feel free to close. |
I think we should always internalize the setup file. |
Describe the bug
Hooks (i.e.
beforeEach
and friends) do not work as expected when loaded from an 'external' setup file.The hooks run for the first test file but not the following. For example, when your setup file has this:
Then you only see "Detected: src/math.spec.js" for the first test; it proceeds to the following test files without executing this
beforeEach
again.I'm not entirely sure what "external" means here, but whenever a setup file is loaded from an 'externalize' location from
vite-node
, the problem occurs:https://github.com/vitest-dev/vitest/blob/6433224c0a05d29968eead273d1c57c680034566/packages/vite-node/src/client.ts#L278-285
I can reproduce it by putting the
vitest.setup.js
file inside a sibling directory. Please take a look at the reproduction instructions below.Reproduction
external-setup.zip
unzip external-setup.zip -d external-setup
.cd external-setup
npm install
cd project
npx vitest run
.You see this output:
As you can see, the
beforeEach
hook is only executed formath.spec.js
, notmin.spec.js
.System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: