-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running tests in a specific order #6194
Comments
I would like to see this implemented in a future version of jest. |
We have a similar need ; we have a bug somewhere in our 600 spec files where changing the order of execution changes code coverage %.. but we have no way to narrow down the problem... |
+1. |
@zGrav @sandorvasas @lukeapage work around would be to have an index file, call it
just make sure that there are no other related tests that match against the jest default |
@steventle |
@sapjax If you're looking for that type of feature, try Mocha for your test runner as they support that with their bail option & no additional set up. |
I've had some success with an index file - for my use case I am trying to track down why code coverage changes with different runs. So I've made one script that finds spec files, makes an index file importing them all, runs jest, then reverse the order of the imports, runs jest again and compares the coverage.. I use this to narrow down which files have a problem, then another script runs two at a time in different orders to narrow it down to a set of two tests. The problems are with importing in a index files:
What would allow me to test my tests consistently without work-arounds is if jest had an order parameter (only making sense in runInBand?) that allowed say alphabetical order and reverse alphabetical order. Then I could run the tests one way and then the other and confirm it passes both directions and coverage is consistent. |
Note, reading the original issue, I wouldn't want a field in the package.json - it would be workable / better than nothing but not ideal. Maybe what I really need is a --testTheTests option that does everything within jest. For the original reporter I would define 1/2/3 jest setups and then run them one after another. wouldn't that work? |
@lukeapage the idea is let the programmer define:
Also, avoid writing huge tests. I believe that using a index is not a good approach for many reasons, but, most focusing in maintenance, allowing me to test or perform small pieces of code as well. Defining a 1/2/3 jest setups would work for small projects, but, for more complexes tests, I don't think that it would be a good approach. |
I agree with DanZeuss about runFirstly: array to allow the programmer defines the order in which the tests will be done. I'm arranging everything to implement the DanZeuss solution. Do everybody agree? As you can see, I'm new here. In order to begin the development I cloned the repository locally. Now I'm trying to execute Jest locally. Which way you develop? Any IDE or only and editor like notepad++ and command line? |
Something else you could consider is to name your files something like:
then you can run your jest command (from your script in package.json or command line) as: |
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:
|
I doubt we'll implement this, see #4032 (comment) |
Well, that's too bad because it could definitely help, especially when testing crud operations to run create-read-update-read-delete in a specific order, and avoid lots of boilerplate from using beforeEach everywhere, and alike. |
Well I would very much appreciate such a feature as I am currently in desperate need of it, because I use Jest together with Puppeteer and it would help me lot. Was someone able to come up at least with some reliable workaround ? |
If you need tests to run in a specific order you can automated this yourself with a bash script to execute specific tests/patterns in a particular order. Jest likely won't provide this functionality because it can absolutely lead to situations where tests HAVE to be run in a particular order and it also makes it insanely hard to parallelize tests. But you could, yourself, write a npm/bash script similar to so:
|
Just to chip in to support a previously-made point: it's all very well trying to prevent people from building in a reliance on a test order, but the trade-off is that given an accidental test-order problem, I am now denied a particularly useful tool to find out where it is. That's less helpful than it could be. |
@palmerj3 unfortunately that prevents those becoming a single run for report & coverage purposes. |
PR #8209 implements this (and it looks like a lot of effort put in, thanks @WeiAnAn), but I do share the concerns about adding this feature stated by @palmerj3 . |
I agree making it pluggable might make sense, if we wanna support this. I don't think it makes sense in core, so I think plugability is the thing we should explore. @cpojer @scotthovestadt thoughts? |
Providing something almost exactly like the |
Or moving |
(I meant moving |
I think move |
@WeiAnAn the custom sequencer would be an external package, Jest itself would only need to have the default sequencer extracted to a package and a config option to allow using a custom one, similar to |
This needn't be complex. I've spiked out something which looks like this:
...and the CLI flag to select it. That's enough for my use case. The order's arbitrary, but I can control it from the command line by explicitly naming files, in the order I want them to run. |
Creating a |
I'll call this closed via #8223. Will be available whenever the next release of Jest happens (no timeline) |
Released in 24.7.0 |
Have jest-test-sequencer been released as well? |
Note that this package contains the default implementation though, one that orders tests in a specific order will have to be user-provided (I'm sure someone will publish an alphabetic test sequencer to npm 😅) |
How should it be used? |
Please refer to the docs https://jestjs.io/docs/en/next/configuration#testsequencer-string |
|
Hey guys, Someone could send a clear example of how can I use the test sequencer? I need to run in order my test but the --runInBand is not working properly. I will really appreciate it. Cheers! |
Suppose you have 3 files, a, b, c, and you want to run in the order of b, a, c. CustomSequencer.js const TestSequencer = require('@jest/test-sequencer').default;
class CustomSequencer extends TestSequencer {
sort(tests) {
const orderPath = ['b', 'a', 'c'];
return tests.sort((testA, testB) => {
const indexA = orderPath.indexOf(testA.path);
const indexB = orderPath.indexOf(testB.path);
if (indexA === indexB) return 0; // do not swap when tests both not specify in order.
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA < indexB ? -1 : 1;
}
}
module.exports = CustomSequencer; note: you need to ensure you orderPath is in absolute path, you can do it with You can refer to jest default TestSequencer sort implementation . And run
Or set in jest config
If you want to make sure the next test start running after previous test completed, add |
Thank you very much! Helped me a lot :) |
Is there any way that i can pass parameters from the describes to the IT(tests)?
If it is possible to pass arguments, that would be great. I will be able to write reusable tests |
@akash8663 just do |
How do you use the test sequencer to randomize tests? instead of the usual sort? |
import TestSequencer from '@jest/test-sequencer';
export default class CustomSequencer extends TestSequencer {
sort(tests) {
return tests.sort(() => Math.random() > 0.5);
}
} |
@SimenB here's a ❤️ for you. Thanks. Is this per file? Or per test block? |
Per file, tests within a file is always ran in declaration order |
@SimenB Any solution to sequence tests per test block? |
Would it be an idea to add a parameter to the I have tests in a file, some of which I wish to run in sequence and some that I do not care and could be run in parrarell to anything.
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 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
andintegration
coverage. The following tests are focused only in environment coverage with Selenium. The tests are: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:
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
--runInBand
but 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.The text was updated successfully, but these errors were encountered: