Giter Site home page Giter Site logo

Comments (7)

AriPerkkio avatar AriPerkkio commented on August 24, 2024

I've tried going through and copying the code that vitest uses to clear the dom between test files but it's not trivial.

Vitest doesn't do any DOM specific clean ups between test files. By default each test file is isolated using node:worker_threads or node:child_process depending on selected pool. Isolation (clean DOM) between tests is provided by terminating previous worker/thread and spinning up a new one.

If you are using testing-library packages, you should use their cleanup() method in Vitest's afterEach() hook. https://testing-library.com/docs/react-testing-library/api#cleanup
Otherwise you'll need to provide your own cleanup methods manually, for example: https://github.com/AriPerkkio/aria-live-capture/blob/master/test/setup.ts.

from vitest.

sheremet-va avatar sheremet-va commented on August 24, 2024

As @AriPerkkio already said, we already provide isolation for every test file. This is the intended design of Vitest. If there was a way to isolate global scope in test functions, we would've already used it.

Please follow @AriPerkkio advice on how to cleanup DOM in your Vitest tests, there is nothing more Vitest can do on our side.

from vitest.

gxxcastillo avatar gxxcastillo commented on August 24, 2024

Hi @AriPerkkio, thanks for helping me better understand how each test file is isolated from the other.

I'm currently using the cleanup method from the react-testing-library but it only cleans up the react tree. Specifically from the docs:

Unmounts React trees that were mounted with render.

Is there no way to isolate each test?

from vitest.

gxxcastillo avatar gxxcastillo commented on August 24, 2024

While researching a solution, i found that playwright seems to offer isolation for each test. Though they seem to be leveraging browser context instead of threads and forks:

https://playwright.dev/docs/writing-tests#test-isolation

from vitest.

sheremet-va avatar sheremet-va commented on August 24, 2024

Playwright opens a separate isolate page for every test, the test functions themselves are not isolated. Since Vitest just runs test functions, we cannot provide the same type of isolation that Playwright has.

from vitest.

gxxcastillo avatar gxxcastillo commented on August 24, 2024

If a new instance of jsdom/happy-dom/etc... were instantiated on every test case, would that solve the issue?

from vitest.

sheremet-va avatar sheremet-va commented on August 24, 2024

If a new instance of jsdom/happy-dom/etc... were instantiated on every test case, would that solve the issue?

No, the global scope is the same.

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.