question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running tests in a specific order

See original GitHub issue

🚀 Feature Proposal

Allow define which files should run before others.

Motivation

Some times, we need to define a specific test to be ran before others. As we want to maintain different test cases in different files, would be essential define the order for some tests. This case implies on environment tests when we are using Jest with other frameworks, like Selenium, for example.

Example

I need to test an email client (browser-oriented). As Jest is a robust framework, we’ll use it with Selenium for environment, unit and integration coverage. The following tests are focused only in environment coverage with Selenium. The tests are:

  • Add an account and make sure that it was added successfully;
  • Check messages in the account;
  • Perform other actions (Open an message, download file, perform filtering, etc);
  • Remove the account.

The idea is to define in the Jest settings an array containing the file names and the order of files that should be ran firstly. For example:

{
    runFirstly: [
        'A.test.js',
        'B.test.js',
        'C.test.js',
        'D.test.js'
}

So, the that would run firstly would be A.test.js, B.test.js, etc.

Once these tests are done, then jest could run other tests sequentially or not.

Pitch

Why does this feature belong in the Jest core platform?

Because we already have a feature that run sequentially --runInBandbut we don’t have a way to define which files should run firstly.

I believe that the way that I described before, is goes beyond the --runInBand feature, allowing define files that must ran first and then, keep the tests according the default behavior of Jest.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:115
  • Comments:48 (8 by maintainers)

github_iconTop GitHub Comments

56reactions
sahil5695commented, Sep 10, 2018

I got a hold on this by creating files of the functions then importing them in the main tests.spec.js describing all the tests in order and assigning them with the imported functions:

test.spec.js

import { signuptests } from './signup'
import { logintests } from './login'

describe('Signup', signuptests)
describe('Signup', signuptests)
signup.js

export const signuptests = () => {
     it('Should have login elements', () => {});
     it('Should Signup', () => {}});
}
login.js

export const logintests = () => {
    it('Should Login', () => {}});
}
46reactions
steventlecommented, Jun 28, 2018

@zGrav @sandorvasas @lukeapage work around would be to have an index file, call it index.spec.js, that dictates the order in which you’d execute:

require('./test1.js)
require('./test2.js)
require('./test3.js)

just make sure that there are no other related tests that match against the jest default testMatch unless o/w custom defined

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running your tests in a specific order - On Test Automation
General consensus within the test automation community is that your automated tests should be able to run independently. That is, tests should be...
Read more >
3 Practical Ways to Run Tests in Specific Order in C#
How to run tests in a specific order with xUnit? · 1. Implement the ITestCaseOrderer and provide implementation to control the test order...
Read more >
The Order of Tests in JUnit - Baeldung
By default, JUnit runs tests using a deterministic but unpredictable order ; In JUnit 5, we can use @TestMethodOrder to control the execution ......
Read more >
How to Run JUnit 5 Tests in a Specific Order - DevQA.io
In JUnit 5, we can use @TestMethodOrder and @Order annotations to run tests in order. Running Tests in Order. Example: import org.junit.jupiter.
Read more >
How to Run JUnit Tests in Order - Apps Developer Blog
To run Unit tests in a specific order we can use the @FixMethodOrder annotation and the MethodSorters class which has three options:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found