I have a mocha unit test suite.
When I run them locally, everything works fine. When I run them on our Jenkins CI server, they sometimes fail, sometimes pass.
I'm just not able to reproduce why they fail. What might cause this behavior?
I have a mocha unit test suite.
When I run them locally, everything works fine. When I run them on our Jenkins CI server, they sometimes fail, sometimes pass.
I'm just not able to reproduce why they fail. What might cause this behavior?
Tests can fail intermittently for a number of reasons and identifying why they fail is often revealing about your codebase and environment.
Here are a few possible causes:
• Shared objects - singletons that hold state can cause problems between tests if the test environment isn't reset to a well known state. if if your test runner executes tests in a non-deterministic order you may see random errors that are actually exposing corrupted state issues
• Environmental and external dependencies - any external object that can hold state can cause unpredictable results
• Timing - sometimes tests are written with timeouts or thread sleeps that are too specific. If the build server is operating under heavy load these timeouts may not be long enough
As general guidance, tests must be:
I would try to narrow down the problem by reducing the number of executors to 1. If the tests are still failing intermittently, then you have a Test Run War, otherwise (given they run fine locally) it looks like a Resource Leakage.
More info at http://xunitpatterns.com/Erratic%20Test.html
I would geuss you are reusing the test fixtures for multiple tests. In some cases the order of the tests is changed from the order on your local machine and you do not have a correct fixture when you start a test. To fix it, you should give each test a clean fixture.
An other cause can be that you reuse a database from multiple locations when runnen tests (two build servers with the same test configuration). Give each build server its own database. Or sub/mock your database layer. Your test should not depend on external resources except when you are testing the connections to your external resources.
It may happen if you're testing multithreaded code and use Thread.sleep(time);
in your tests. If sleep time is not long enough, tests may pass or fail depending on computers processing speed. Related discussions:
I'm working with jest
. My problem was the mock data causing the tests to fail intermittently. Clearing mocks and using fake timers inside each describe
helped my problem:
describe("my test", ()=> {
beforeAll(() => {
jest.setTimeout(30000);
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
beforeEach(() => {
jest.clearAllMocks();
});
it("my test", async()=>{....})
...
})