How to avoid timeout for mocha asynchronous tests [duplicate] - selenium

This question already has answers here:
How to increase timeout for a single test case in mocha
(8 answers)
Closed 5 years ago.
I have a mocha test that I am trying to run but it keeps on giving me the following error
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called
it('should login into account', (done) => {
let user_login = require("../../data/login.json");
mongoManager.insertDocuments("user", user_login.content, () => {
loginPage.setUserName('demodgsdg');
loginPage.setPassword('123');
loginPage.submit();
browser.waitForAngularEnabled(true);
Assert.equal(element(by.id('navbar')).isDisplayed(), true, "login page is not loaded");
setTimeout(done(), 50000);
done();
});
});
Whats the best way to run an asynchronous test in mocha so that it doesnt exceed its alotted time? Or should I set the timeout on the test function

You need to do it this way
it('should login into account', function (done) {
this.timeout(50000);
let user_login = require("../../data/login.json");
mongoManager.insertDocuments("user", user_login.content, () => {
loginPage.setUserName('demodgsdg');
loginPage.setPassword('123');
loginPage.submit();
browser.waitForAngularEnabled(true);
Assert.equal(element(by.id('navbar')).isDisplayed(), true, "login page is not loaded");
setTimeout(done(), 50000);
done();
});
});
If you read https://mochajs.org/#timeouts
Passing arrow functions (“lambdas”) to Mocha is discouraged. Due to the lexical binding of this, such functions are unable to access the Mocha context. For example, the following code will fail due to the nature of lambdas:
describe('my suite', () => {
it('my test', () => {
// should set the timeout of this test to 1000 ms; instead will fail
this.timeout(1000);
assert.ok(true);
});
});
If you do not need to use Mocha’s context, lambdas should work. However, the result will be more difficult to refactor if the need eventually arises.

Related

Mongoose connection closing too soon before tests have run

I am a beginner using jest to test a node/express app with mongo database.
I am getting an issue where different tests are failing each time I run the tests and sometimes they all pass/all fail. I think it is because of a time-out or things not happening in the right order because I'm getting this error:
MongoPoolClosedError: Attempted to check out a connection from closed connection pool
a) can you let me know if you think I'm on the right track?
b) if so, is the solution to make this into an async function and how can I do that? (I have tried making it into async await, using .then and also putting the code that clears the database collections into the test files instead of the helper and have had no success so far.
beforeAll( (done) => {
mongoose.connect("mongodb://127.0.0.1/jobBuddy_test", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
var db = mongoose.connection;
const users = db.collection('users')
users.deleteMany({})
const applications = db.collection('applications')
applications.deleteMany({})
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.on("open", function () {
done();
});
});
afterAll(function (done) {
mongoose.connection.close(true, function () {
done();
});
});

Angular 8 karma test both success and error of Observable from service

I'm testing MyComponent that, in ngOnInit, calls a service method that returns an observable:
this.myService.getMyData().subscribe(
(data) => {
//do something
},
(error) => {
//do something - error
}
);
I've installed a spy on getMyData, this way:
mySpy = spyOn(myService, 'getMyData').and.returnValue(of(mockObject));
and this covers the "success branch" of subscribe. But I need also to cover the "error branch". I've tried doing this:
spyOn(myService, 'getMyData').and.returnValue(throwError(new Error('Error')));
but of course Jasmine tells me that getMyData has already been spied upon. How can I cover both branches?
What you are doing is correct but, you can't spy on the same service more than once in the same test case.
Hence you need to write a second test case which will cover your error scenario.
Something like this:
it('should run success scenario', () => {
mySpy = spyOn(myService, 'getMyData').and.returnValue(of(mockObject));
})
it('should run error scenario', () => {
mySpy = spyOn(myService, 'getMyData').and.returnValue(throwError(new Error('Error')));
})

Jest unresolved promise do not fail

Jest docs says:
Unresolved Promises
If a promise doesn't resolve at all, this error might be thrown:
(and so on)
In my case this not happen.
I have this test:
test('detect infinite loop', () => {
expect.assertions(1);
const vastPromise = VastUtils.parseFromUrl(infiniteLoopUrl);
const expectedError =
new VastError(VastErrorCodes.WRAPPER_LIMIT_REACHED);
return expect(vastPromise).rejects.toEqual(expectedError);
});
VastUtils simply fetch an XML located at infiniteLoopUrl, parse it, and if this xml point to another xml, VastUtils follow the link, parse the new xml, merge them and repeat the process.
Now, infiniteLoopUrl point to an XML that refers itself, so it is an infinite loop.
"correctly", the code follow xml link infinitely, and never resolve or reject the promise.
I expect above test fail after a certain timeout, but it didn't.
Someone can help me?
Thanks
EDIT:
I'm trying to reproduce an infinite Promise loop with a smaller example, and this is what i've noticed:
this test correctly FAIL after 5s:
test('Promise2', () => {
const genPromise = (): Promise<void> => {
return new Promise((res) => {
setTimeout(() => {
res();
}, 200);
})
.then(() => {
return genPromise();
});
};
const vastPromise = genPromise();
const expectedError =
new VastError(VastErrorCodes.WRAPPER_LIMIT_REACHED);
return expect(vastPromise).rejects.toEqual(expectedError);
});
This test DO NOT FAIL after 5s (jest remain in an infinite loop)
test('Promise', () => {
const genPromise = (prom: Promise<void>): Promise<void> => {
return prom
.then(() => {
return genPromise(Promise.resolve());
});
};
const vastPromise = genPromise(Promise.resolve());
const expectedError =
new VastError(VastErrorCodes.WRAPPER_LIMIT_REACHED);
return expect(vastPromise).rejects.toEqual(expectedError);
});
Apparently these are similar, but I don't understand the difference that cause the jest infinite loop...
Ok, I've understand the problem.
The cause is the mono thread nature of js.
In the two examples of the edit section, te first one have a timeout so there is a moment whent jest take the control and could check the timeout.
In the second one no, so jest never check the timeout.
In my real case, the problem was the fake server: it was created as:
server = sinon.fakeServer.create({
respondImmediately: true
});
respondImmediately make sinon respond syncroniously, so jest never have the control.
Creating it as:
server = sinon.fakeServer.create({
autoRespond: true
});
sinon respond after 10ms and jest can check the time passing

Async test never calls done() if assertion fails [duplicate]

This question already has answers here:
mocha with nodejs assert hangs/timeouts for assert(false) instead of error
(3 answers)
Closed 5 years ago.
I'm trying to unit test a controller method with mocha + chai + sinon, but I'm struggling to understand how the assertion works with these async methods. Here is my test:
it('should fetch all registered books and send them as json', (done) => {
res.json = sandbox.spy();
const books = [];
BookMock.expects('find').withArgs()
.resolves(books);
booksController.index(req, res, () => {
res.json.should.have.been.calledWith(200, { data: books });
done();
});
});
The problem with this test is that if I change the assertion to expect a 300 instead of 200 parameter, for example, my test will stop (fail) and never call done(), leading to a generic failure which tells nothing more than a 'Test failed because done was never called', which says nothing related to the reason the test failed at all.
If I keep the test like that and my controller method does everything good, the test passes and it's all ok, but if the controller does anything wrong, the test fails and doesn't tell me the reason it failed (which should be "res.json was not called with bla bla bla parameters").
I am also not sure this is the correct way to unit test controller methods, since they don't return a Promise I can't use chai-as-promised or promise chains at all, so I used the next() callback (which always gets called using restify) to make the assertion, that might not be right, so I'm open to a full refactor to make this test the most correct.
Thank you in advance!
Digging a little bit more I've found this answer in other question: https://stackoverflow.com/a/26793989/4233017
So I updated my code and wrapped the assertion in a try/catch, ending up like this:
it('should fetch all registered books and send them as json', (done) => {
res.json = sandbox.spy();
const books = [];
BookMock.expects('find').withArgs()
.resolves(books);
booksController.index(req, res, () => {
try {
res.json.should.have.been.calledWith(200, { data: books });
done();
} catch (e) {
done(e);
}
});
});
Now when the test fails it gives me the correct error message. I could also do it with booleans as the answer says, but I think it's better this way.
Any other solution is still very much appreciated.

How to set jasmine for karma e2e for testing angular app?

I try to create e2e tests with karma and jasmine with yeoman. In my karma-e2e.conf.js I add jasmine:
files = [
JASMINE,
JASMINE_ADAPTER,
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'test/e2e/**/*.js'
];
A need async testing so I need to use runs, waits, waitsFor (https://github.com/pivotal/jasmine/wiki/Asynchronous-specs)
But if I try to use it:
it('test', function () {
runs(function () {
...
});
});
Scenatio test runner returns this:
TypeError: Cannot call method 'runs' of null
at runs (http://localhost:8080/adapter/lib/jasmine.js:562:32)
at Object.<anonymous> (http://localhost:8080/base/test/e2e/eduUser.js:42:3)
at Object.angular.scenario.SpecRunner.run (http://localhost:8080/adapter/lib/angular-scenario.js:27057:15)
at Object.run (http://localhost:8080/adapter/lib/angular-scenario.js:10169:18)
I don't know where the problem is. Can you help me please?
Angular e2e tests with Karma don't and can't use the JASMINE adapter. Instead you have the ANGULAR_SCENARIO_ADAPTER which has a similar feel to writing Jasmine tests.
All commands in the adapter's API are asynchronous anyway. For example element('#nav-items').count() doesn't return a number, it returns a Future object. Future objects are placed in a queue and executed asynchronously as the runner progresses. To quote the API docs:
expect(future).{matcher}:
[...] All API statements return a future object, which get a value assigned after they are executed.
If you need to run your own asynchronous test code, you can extend the adapter's DSL, this is easier than it might sound. The idea is that you return your own Future which can be evaluated by a matcher such as toBe(). There are some examples on how to do this in the e2e-tests.js Gist from Vojta. Just remember to call done(null, myRetrunValue); when your test code is successful (myRetrunValue is the value evaluated by your matcher). Or done('Your own error message'); if you want the test to fail.
UPDATE: In response to question below. To simulate a login, first add a function called login to the dsl:
angular.scenario.dsl('login', function() {
return function(selector) {
// #param {DOMWindow} appWindow The window object of the iframe (the application)
// #param {jQuery} $document jQuery wrapped document of the application
// #param {function(error, value)} done Callback that should be called when done
// (will basically call the next item in the queuue)
return this.addFutureAction('Logging in', function(appWindow, $document, done) {
// You can do normal jQuery/jqLite stuff here on $document, just call done() when your asynchronous tasks have completed
// Create some kind of listener to handle when your login is complete
$document.one('loginComplete', function(e){
done(null, true);
}).one('loginError', function(e){
done('Login error', false);
});
// Simulate the button click
var loginButton = $document.find(selector || 'button.login');
loginButton.click();
})
};
});
And then call:
beforeEach( function()
{
expect( login('button.login') ).toBeTruthy();
});