How run different describe blocks from different files using Jest

Viewed 777

I need to run differents describe blocks from different files in determinate order. Also, I need pass 1 argument between this blocks, but the argument needs to keep the state with which it left the previous describes. For example, i have this:

file1.test.js

export const Test1= () => {
   describe("Describe 1", () => {
     test("test1", async () => {//my test1
     })
    test("test2", async () => {//my test2
     })

   describe("Describe 2", () => {
     test("test3", async () => {//my test3
     })
    test("test4", async () => {//my test4
     })
}

file2.test.js

export const Test2= () => {
   describe("Describe 3", () => {
     test("test5", async () => {//my test5
     })
    test("test6", async () => {//my test6
     })

   describe("Describe 4", () => {
     test("test7", async () => {//my test7
     })
    test("test8", async () => {//my test8
     })
}

I want to run the tests in the following order with this argument:

let myArgument;
Test1
   Describe 1(myArgument)
Test2
   Describe 3
Test1
   Describe 2(myArgument)
Test2
   Describe 4

I am using to run the tests: jest --runInBand

1 Answers

It is generally a good idea to keep tests isolated from each other, so that failures of one test do not cascade to other tests. Playwright recommends using different browser contexts for tests – browser contexts are concurrent, isolated execution environments. They are cheap/fast to create, and can reuse a single browser instance.

Having said that, it is possible to achieve your use-case with Jest, with some work:

  1. Define myArgument as a global object. Global objects are shared across tests, and can be defined through globals in your Jest config (docs)

  2. Define a custom sequencer in your Jest config to define the sequence in which tests are executed. Use the testSequencer property in Jest config (docs)

Related