I found this question, and setting this.timeout works for me when done for each test individually. Is there a way to set it for all tests? I tried putting it in the suite (in the return all([... section) as well as in the config file, but neither worked.
There is not currently a built-in way to globally set the asynchronous test timeout or to change the default from 30000 ms. As pointed out in the question, the timeout can be set on a per-test basis by setting this.timeout. You can also call this.async with a timeout.
Note that the timeout is only meaningful for async tests, not necessarily all tests (although to be honest I don't think I've ever seen a sync test that lasted 30 seconds).
Related
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
Solved : It's a node bug. Happens after ~25 days (2^32 milliseconds), See answer for details.
Is there a maximum number of iterations of this cycle?
function do_every_x_seconds() {
//Do some basic things to get x
setTimeout( do_every_x_seconds, 1000 * x );
};
As I understand, this is considered a "best practice" way of getting things to run periodically, so I very much doubt it.
I'm running an express server on Ubuntu with a number of timeout loops .
One loop that runs every second and basically prints the timestamp.
One that calls an external http request every 5 seconds
and one that runs every X 30 to 300 seconds.
It all seemes to work well enough. However after 25 days without any usage, and several million iterations later, the node instance is still up, but all three of the setTimout loops have stopped. No error messages are reported at all.
Even stranger is that the Express server is still up, and I can load http sites which prints to the same console as where the periodic timestamp was being printed.
I'm not sure if its related, but I also run nodejs with the --expose-gc flag and perform periodic garbage collection and to monitor that memory is in acceptable ranges.
It is a development server, so I have left the instance up in case there is some advice on what I can do to look further into the issue.
Could it be that somehow the event-loop dropped all it's timers?
I have a similar problem with setInterval().
I think it may be caused by the following bug in Node.js, which seem to have been fixed recently: setInterval callback function unexpected halt #22149
Update: it seems the fix has been released in Node.js 10.9.0.
I think the problem is that you are relying on setTimeout to be active over days. setTimeout is great for periodic running of functions, but I don't think you should trust it over extended time periods. Consider this question: can setInterval drift over time? and one of its linked issues: setInterval interval includes duration of callback #7346.
If you need to have things happen intermittently at particular times, a better way to attack this would be to schedule cron tasks that perform the tasks instead. They are more resilient and failures are recorded at a system level in the journal rather than from within the node process.
A good related answer/question is Node.js setTimeout for 24 hours - any caveats? which mentions using the npm package cron to do task scheduling.
I'm calling streamingContext.awaitTerminationOrTimeout(timeout), but I want to make timeout environment dependent.
This means that I want to stop my job if my environment is UAT, but I don't want it to timeout at all if my environment is production.
I have checked the documentation here, but I can't find any references to whether passing 0 or -1 as timeout will just make the job run forever (or until it fails).
Are there any possible timeout values that could do the trick? Or any other alternatives that don't imply calling awaitTermination for my production environment and awaitTerminationOrTimeout for my UAT environment?
After taking a look at the library, I can see that any negative value will do.
awaitTerminationOrTimeout(timeout) internally calls streamingContext.awaitTerminationOrTimeout(timeout), which then calls contextWriter.waitForStopOrError(timeout).
Inside waitForStopOrError there is a condition that says that if the timeout is lower than zero, then we wait.
TL:DR: -1 will never let the job timeout.
Pretty new to Mocha and testing here, so hoping somebody can help me out or point me in the right direction.
What I am trying to do is have a mochaJS test run every 5 minutes for an entire day. However, I don't want to actually type the command, and am hoping that I could write some code that would carry this out.
So far, I have tried adjusting the this.timeout in the mocha test, and then setting javascript intervals (setInterval(function(){}, time)) and using while loops with the setTimeout.
Is it possible to set an interval like this in mocha?
Or is there some other way around this, say through command line, that would execute the mocha test every 5 minutes?
Thank you for your advice and expertise!
Cheers
This really sounds like a task that should be managed by a Continuous Integration server such as Jenkins.
If you install Jenkins, you can create a new job that will run the tests you want on a given interval, ie. every 5 minutes. Better yet, you can connect the Job to your source code repo and run the job on any code push.
I am using jasmine-node for doing unit testing. I did below code for mocking Date.now()
spyOn(Date, 'now').andReturn(1387636363717); //always return a fixed time
Then I tried to run jasmine-node spec/ but it stopped working with no output. I could not figure out what is the reason.
I have written a tiny test. It works just fine. Using jasmin-node in version 1.11.0.
Where is your Date.now function used then?
spyOn(Date, 'now').andReturn(1387636363717);
expect(Date.now()).toEqual(1387636363717);
The problem is that there is code in node runtime (timers.js) that calls Date.now() to mark the passage of time. If you have code that sets a timer (setTimeout) and that code is executed while not using jasmine.Clock, then you may find node.js is waiting for some time to pass before executing the next real timeout. Since this code appears to be called from outside javascript land, you can not rely
on the single-threaded nature of javascript to keep you safe from this. By putting a breakpoint on a .andCallFake on the Date.now spy, I was able to see that the execution of the spec was being indefinitely suspended waiting for a millisecond to pass.
This is a problem for me because we have a very large unit test suite and there are possibilities that setTimeout is called as a side effect of some other calls and jasmine.Clock is not used universally in every spec. Since this is the side effect of an optimization (note: 'too much overhead' in the comments), I would consider this to be a bug in node.js.
If you want to stop time for your tests:
Spy on Date.now inside your spec function (it function - not beforeEach). That way your spec won't be delayed (also explains why #marco.jantke did not see this issue).
if your code under test uses timers, use jasmine.Clock.