node.jstestingvitest

Run a function before all the test modules in Vitest


I am using Vitest as my testing framework in a project.

I have multiple test files in the project, let's say A.spec.ts and B.spec.test. I am using the standard test script (vitest run --no-threads --coverage) to test my code. I want to run a certain function (to purge and clean the testing database), before and after all the test suites are run (i.e. before all the tests in A.spec.ts and B.spec.ts, and after them as well).

Is there any way to achieve the same? I read about the methods like beforeAll and afterAll, but they work in the context of a file, and thus do not help with my use case.


Solution

  • you should try global setup

    globalsetup