How to put a "slow" label in Jest - jestjs

With Mocha there exists a function slow(time) that says after which running time threshold a test is "slow" and labelled as such (docs).
Is there an alternative in Jest? I have not found any.

Related

Is there a way to get the coverage information collected by jest during the test execution?

I want to create some kind of fuzz test with jest. For this I've created a loop within a test definition (it).
As a break condition I want to use a time limit and an achieved coverage count.
Unfortunately jest generates the coverage report at the end of the test execution.
Is there a way to export/access the coverage.json during the test run?
Thanks for your help.

how to make a jest test fail at first time even using jest.retryTimes?

in my Jest test suite,
i use
jest.retryTimes(4)
because of the particular and instable architecture of the software. And this works as expected.
There are some test that must to pass at the first time so for these particular test i need to set
jest.retryTimes(1)
at the beginning of the test, restoring
jest.retryTimes(4)
at the end.
There are two problems :
The problem is this configuration is global, and test are executed
in parallel, so when this test start, it put 1 to jest retry for all
the test running at this moment. I would like to make only this
particular test fail the first time.
Jest Circus ignore the update of jest.retryTimes at the beginning and at the end
of the test, it keep considering 4 temptative before of raise the failure.
I read the documentation but I think I cannot obtain this result.
any suggestion?
Thanks

istanbul with mocha not generating coverage report when a testcase takes more time

I am testing my js file using mocha. Some of the testcases takes time in resolving response so i used timeout with it. Complete command looks like
istanbul cover _mocha test/sol-verifier.js -- --timeout 300000
Problem is that while going for the coverage like this, I am not getting coverage report created, it only runs testcases successfully and stops (not terminates). How can i solve this?
My code is in node.js and it doesn't create any server.
Also when i remove --timeout flag and comment out the test cases that take more time. It works fine and generates the coverage report.
As per my findings, problem is not in the --timeout flag, report is not generated when there is a testcase taking longer than usual to resolve.
You can add a done argument to your test suite functions and invoke done() at the end of each of your test functions:
it('test expectation', function(done) {
// test asynchronous code
// call done() to terminate test and proceed to the next test
done();
}
Alternatively, try running your tests with the --exit flag on mocha:
istanbul cover _mocha --exit test/sol-verifier.js -- --timeout 300000
According to the docs:
To avoid false positives and encourage better testing practices, Mocha will no longer automatically kill itself via process.exit() when it thinks it should be done running.
If the mocha process is still alive after your tests seem "done", then your tests have scheduled something to happen (asynchronously) and haven't cleaned up after themselves properly. Did you leave a socket open?
Supply the --exit flag to use pre-v4 behavior.

Mocha tests stop abruptly with: Cannot find module 'pg-native'

Our mocha tests suddenly stop with this message on the console:
Cannot find module `pg-native`
No stack trace is shown, mocha doesn't render the normal output for the test. The test immediately stops.
If I disable the test in question, all tests run as normal.
Installing pg-native removes the error, but then mocha just hangs at that point instead.
As per this issue the problem is a result of running something that walks sequelize records in depth.
eg
expect(myObject).to.deep.equal(mySequelizeInstance);
changing to
expect(myObject).to.deep.equal(mySequelizeInstance.toJSON());
will solve it
Why?
There's two reasons for the above behaviour
Sequelize records override native getters and so traversing certain properties causes code to be executed. In this case, one of those properties ends up down a rabbit hole that causes require('pg-native') to be executed (and so the error)
The object contains circular references, and so the code hangs traversing the infinite references. Left long enough it will eventually fail when it exhausts the stack.

Running subsets of Jest tests in parallel

We're using Jest to power our Node.js tests, these interact with a Postgres database to test CRUD operations. We're currently passing the --runInBand CLI option to ensure our tests operate in serial, this works fine but is obviously slower than we'd like.
Now from reading around (and previous experience) I've found it useful to be able to mark groups of tests as parallelise-able. This is possible with nose in python but I cannot seem to find the syntax in Jest. Is this possible? Or is there another approach to speeding up database (or state constrained to generalise) tests that Jest advocates?
Thanks,
Alex
Put your tests in separate files (in a new sub folder if you want to keep them organized). That way Jest runs the files in parallel.

Resources