25

I'm using Jest to run Selenium tests. I would like the login tests to happen before the rest of the webapp functionality tests. I'm able to run the files in sequence using jest -i but I can't find a way to control the order that the files get run in. I tried changing the filenames hoping it is by sort order of filename but it still runs in the same order no matter what I call the files. How can this be done?

This is about running in a specific order, NOT sequentially. I'm already doing --runInBand (-i is an alias for that).

Brennan Cheung
  • 4,271
  • 4
  • 30
  • 29

3 Answers3

12

That's currently not possible in Jest (see https://github.com/facebook/jest/issues/6194), and it seems that it won't be possible either (see https://github.com/facebook/jest/issues/4032#issuecomment-315210901).

One option would be to have two different configurations for Jest, one for the login tests and a second one for the rest, and have 2 script entries in the package.json file

// package.json
...
"scripts": {
  "test": "npm run test:login && npm run test:other"
  "test:login": "jest --config jest.login.config.js",
  "test:other": "jest --config jest.other.config.js"
}

I hope it helps!

Pablo Navarro
  • 8,244
  • 2
  • 43
  • 52
  • Interesting, but the problem is if you are using custom reporters for example "jest-html-reporters", you html report generated by test:login will be deleted by test:other 's report (default filename of report is jest_html_reporters.html) – rach Oct 06 '22 at 10:52
8

You can now use a test sequencer. Check the docs: https://jestjs.io/docs/en/configuration#testsequencer-string

You may want to combine the test sequencer with the --runInBand option for a deterministic result.

clapas
  • 1,768
  • 3
  • 16
  • 29
5

In my case I use a single test file where I require the .js in order:

//main.test.js
require("db.js");
require("tables.js");
require("users.js");
require("login.js");
require("other.js");

For example, the file users.js:

const request = require("supertest");
const assert = require("assert");
const { app, pool } = require("../src/app");

describe("POST /user/create", () => {
    it("return unautorized without token", () => {
        return request(app)
            .post("/user/create")
            .set('Accept', 'application/json')
            .send({ token: "bad token..." })
            .expect(401);
    });
    it("create user with token ok", () => {
        return request(app)
            .post("/user/create")
            .set('Accept', 'application/json')
            .send({
                "email": "user1@dom.com",
                "name": "John Doe",
                "pass": "1234",
                "token": "good token..."
            })
            .expect(200)
            .expect('Content-Type', /json/)
            .then(r => {
                assert.deepStrictEqual(r.body, {
                    status: true,
                    info: 'user created',
                    data: { email: 'user1@dom.com', name: 'John Doe' }
                });
                pool.end();
            })
            ;
    });
});

Avoid using the word test or spec in files, just use 1 .test.js

Luis Marin
  • 129
  • 2
  • 6