I am running supertest on Jest and trying to test a simple POST request:
test('A simple POST', done => {
const appHttpServer = app.getHttpServer();
const _request = request(appHttpServer);
return _request
.post(`my-post-url`)
.type('json')
.send(someData)
.expect(HttpStatus.ACCEPTED, done);
});
The test passes as expected, however it stays hang. Running with --detectOpenHandles results with:
Jest did not exit one second after the test run has completed.
If I try a non-post request, everything works as expected and the test exits correctly.
What am I doing wrong in this case?
Related
I get weird error whenever i'm testing my endpoint through jasmine and superset.
import app from '../index'
import supertest from 'supertest'
const request = supertest(app);
describe("suite for testing the image endpoint response", () => {
//done for supertest to tell when our endpoint is done to disconnect from server
it('Server is up', async (done)=>{
const response = await request.get('api/image');
expect(response.status).toBe(200);
done();
})
})
The Error:
suite for testing the image endpoint response Server is up
An asynchronous before/it/after function took a done callback but also returned a promise. Either remove the done callback (recommended)
or change the function to not return a promise. thrown
Unhandled promise rejection: Error: ECONNREFUSED: Connection refused
Error: Timeout - Async function did not complete within 5000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
Executed 1 of 1 spec (1 FAILED) in 5 secs.
I tried to remove "done()" which is wrong to be removed since it closes the connection after the test, but still same error.
By removing done in parameters and done() in the last line, will make it work.
I'm new in unit testing in node.js using mocha and chai.
I'm stuck at the problem in which control return automatically and not executing
chai.expect(123).to.be.a("string");
code is here
it.only("should fetch status",()=>{
return chai.request(server)
.get("/user/status")
.then((result)=>{
let data = result.body;
console.log("till here execute");
//this line is not executed and test case is passed even when the below line expect to fail the test
chai.expect(123).to.be.a("string");
})
.catch(err=>err);
});
Console show that above test case is passed I don't know how and why
chai.expect(123).to.be.a("string");
not executing
This is related to your catch.
Basically, when your chai.expect fails, it will throw an AssertionError.
Inside your given code, you are returning the catching error, and not throwing it.
According to chai.js official documents, found in https://www.chaijs.com/plugins/chai-http/, when dealing with promises, inside the catch you must throw the catch error.
In that way, change:
.catch(err=>err);
to:
.catch(err => {throw err});
I'm new to JS and trying cucumber js for the first time
This is how my step defn looks like:
Pseudocodes
Given("I launch Google.com", async (){
await Launch.launchGoogle():
})
When("I enter search text cucumber js", async (){
await Launch.searchCucumber():
})
This is how my Launch.js looks like:
module.exports launchGoogle= async function() {
await driver.get("www.google.com"):
}
module.exports searchCucumber = async function(){
await driver.findElement(By.name("q")).sendKeys("cucumber");
}
In this case, when I run the feature with 2 steps, I get ELIFECYCLE ERR at the end of first step.
When I remove the await in the step definitions, it runs fine. But, the console displays as 2 steps passed even before the chrome browser is launched. That is, it fires the Given and When steps and shows the result even as the code in Launch.js is still executing.
Pls help how to solve this?
I just figured out that the default step timeout is 5000ms. Since, launching the browser and hitting the URL was taking more than that, it was failing. i just increased the step timeout to 30000ms and it works fine.
I'm correctly working on refactoring my clone of the express-decorator NPM package. This includes refactoring the unit tests that were previously done using AVA. I decided to rewrite them using Mocha and Chai because I like the way they define tests a lot more.
So, what is my issue? Take a look at this code (I broke it down to illustrate the problem):
test('express', (t) => {
#web.basePath('/test')
class Test {
#web.get('/foo/:id')
foo(request, response) {
/* The test in question. */
t.is(parseInt(request.params.id), 5);
response.send();
}
}
let app = express();
let controller = new Test();
web.register(app, controller);
t.plan(1);
return supertest(app)
.get('/test/foo/5')
.expect(200);
});
This code works.
Here's (basically) the same code, now using Mocha and Chai and multiple tests:
describe('The test express server', () => {
#web.basePath('/test')
class Test {
#web.get('/foo/:id')
foo(request, response) {
/* The test in question. */
it('should pass TEST #1',
() => expect(toInteger(request.params.id)).to.equal(5))
response.send()
}
}
const app = express()
const controller = new Test()
web.register(app, controller)
it('should pass TEST #2', (done) => {
return chai.request(app)
.get('/test/foo/5')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
done()
})
})
})
The problem is that the TEST #1 is ignored by Mocha although that part of the code is run during the tests. I tried to console.log something there and it appeared in the Mocha log where I expected it to appear.
So how do I get that test to work? My idea would be to somehow pass down the context (the test suite) to the it function, but that's not possible with Mocha, or is it?
It looks like you are moving from tape or some similar test runner to Mocha. You're going to need to significantly change your approach because Mocha works significantly differently.
tape and similar runners don't need to know ahead of time what tests exist in the suite. They discover tests as they go along executing your test code, and a test can contain another test. Mocha on the other hand requires that the entire suite be discoverable before running any test.* It needs to know each and every test that will exist in your suite. It has some disadvantages in that you cannot add tests while the Mocha is running the test. You could not have a before hook for instance do a query from a database and from that create tests. You'd have instead to perform the query before the suite has started. However, this way of doing things also has some advantages. You can use the --grep option to select only a subset of tests and Mocha will do it without any trouble. You can also use it.only to select a single test without trouble. Last I checked, tape and its siblings have trouble doing this.
So the reason your Mocha code is not working is because you are creating a test after Mocha has started running the tests. Mocha won't right out crash on you but when you do this, the behavior you get is undefined. I've seen cases where Mocha would ignore the new test, and I've seen cases where it executed it in an unexpected order.
If this were my test what I'd do is:
Remove the call to it from foo.
Modify foo to simply record the request parameters I care about on the controller instance.
foo(request, response) {
// Remember to initialize this.requests in the constructor...
this.requests.push(request);
response.send()
}
Have the test it("should pass test #2" check the requests recorded on the controller:
it('should pass TEST #2', (done) => {
return chai.request(app)
.get('/test/foo/5')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
expect(controler.requests).to.have.lengthOf(1);
// etc...
done()
})
})
And would use a beforeEach hook to reset the controller between tests so that tests are isolated.
I'm trying to use retries in mocha. Here is my code:
'use strict';
const assert = require('chai').assert;
describe('retries', function() {
it('should not be equal', function () {
this.retries(10);
assert.equal(1, 2);
});
});
I'm expecting, that test would be retried 10 times, but it didn't.
Version of mocha is 3.3.0
Mocha does in fact retry your code. However, Mocha does not show you each individual attempt. It only reports the final result as to whether the test passed at some point (after some number of tries), or failed (because all the tries failed). If you add console.log("something") to your test, you'll see it retries your test as you specified.