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).
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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With