Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Jest run tests in a specific order?

Tags:

jestjs

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).

like image 818
Brennan Cheung Avatar asked Mar 13 '18 02:03

Brennan Cheung


3 Answers

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!

like image 126
Pablo Navarro Avatar answered Oct 27 '22 09:10

Pablo Navarro


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.

like image 25
clapas Avatar answered Oct 27 '22 11:10

clapas


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": "[email protected]",
                "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: '[email protected]', name: 'John Doe' }
                });
                pool.end();
            })
            ;
    });
});

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

like image 3
Luis Marin Avatar answered Oct 27 '22 11:10

Luis Marin