Why is Mocha ignoring asynchronous function calls? - javascript

I am writing automated integration tests with Mocha and Chai. Here is a simplified version of the code I am testing:
exports.doSomething = async function (req, res) {
return executeRequest(req.body)
.then((response) => {
console.log("then running");
res.status(200).send(response);
})
.catch((err) => {
console.error(err);
res.status(500).send(err);
}
}
And here is what my test file looks like:
const { doSomething } = require("../../index");
const { assert } = require("chai");
const { stub } = require("sinon");
const req = { body: { *data* } };
const res = {
status: stub().returnsThis(),
send: stub().returnsThis(),
};
it(`Please work`, async () => {
await doSomething(req, res);
}
When that happens, neither the .then block nor the .catch blocks are entered—console.log does not run; res.send and res.status are never called.
Another interesting note: If I remove the async from the test call and save the result of doSomething() to a variable, it shows as a promise. When I include the async, the result of doSomething is undefined.
I am new to Mocha and have no idea why it seems to be ignoring the asynchronicity of the code.

Your doSomething implementation is broken, the promise that the async function returns will fulfill immediately - before executeRequest is done. You should write either
exports.doSomething = function (req, res) {
return executeRequest(req.body)
//^^^^^^
.then((response) => {
console.log("then running");
res.status(200).send(response);
})
.catch((err) => {
console.error(err);
res.status(500).send(err);
})
}
or
exports.doSomething = async function (req, res) {
try {
const response = await executeRequest(req.body);
// ^^^^^
console.log("then running");
res.status(200).send(response);
} catch(err) {
console.error(err);
res.status(500).send(err);
}
}
Only then your test can properly await the doSomething(req, res) call, and mocha won't prematurely kill the process.

Related

async function on route.js file | nodejs

I have an application that it has nodejs as backend and some scripts in Python
The problem is to make the 'PythonShell' (function to access the scripts) as a async function. I do not know why but it is not working.
I'll put the code from my router.js file and inside of it I put three 'console.log('steps')' to check the sequences.
It should be Step01 > Step02 > Step03, but as it is not working, It always prints Step01 > Step03 > Step02
Everything is working fine, except for this async problem! For me it should work as it is.
How can I edit my functions to execute first the 'goToscript/PythonShell' and then execute 'res.json(responseScript)'?
Thanks
router.put("/uploads/script-03", async (req, res) => {
let options = {
scriptPath: "scripts",
args: JSON.stringify(req.body)
};
const goToScript = async () => {
await PythonShell.run("script-01.py", options, (err, res) => {
if (err) {
}
if (res) {
responseScript = JSON.parse(res)
console.log('Step 02')
}
});
}
console.log('Step 01')
goToScript()
console.log('Step 03')
res.json(responseScript)
});
module.exports = router
A couple things:
1. Your goToScript is not actually async/returning a Promise
From what I can tell, PythonShell doesn't support async, only callbacks, so you can rewrite your gotToScript like so:
const goToScript = () => {
return new Promise((resolve, reject) => {
PythonShell.run("script-01.py", options, (err, res) => {
if (err) {
reject(err)
}
if (res) {
responseScript = JSON.parse(res)
console.log('Step 02')
resolve(responseScript)
}
})
})
}
const scriptResult = await goToScript()
This code will work like a regular async function, where the promise will resolve to the parsed JSON, and reject with the error if it meets one.
2. You are not awaiting your call to goToScript
When you want to make an async call that finishes in sequence with everything else, you need to await it. Take these two examples:
In this first chunk of code, waitFn waits before 100ms before logging "Step 2!":
const waitFn = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Step 2!')
resolve()
}, 100)
})
}
console.log('Step 1!')
waitFn()
console.log('Step 3!')
Because you do not await the result of the Promise, your code doesn't care that is has not finished, and will print:
Step 1!
Step 3!
Step 2!
Instead, however, if you await the result of the Promise returned in waitFn, it will execute in order:
const waitFn = () => {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Step 2!')
resolve()
}, 100)
})
}
console.log('Step 1!')
await waitFn() // Finishes before moving on
console.log('Step 3!')
You can read a bunch more about Promises and async/await here :)
To be able to await function - this function needs to return a promise. In the npm page of your lib there is a description, about what PythonShell.run returns. It does not return a promise. So it is asynchronous, but not awaitable, it is callback based.
All you need to do - to promisify this function. Additionaly - you need to await the call to goToScript();.
router.put("/uploads/script-03", async (req, res) => {
let options = {
scriptPath: "scripts",
args: JSON.stringify(req.body)
};
const goToScript = async () => {
return new Promise((resolve, reject) => {
PythonShell.run("script-01.py", options, (err, res) => {
console.log("Step 02");
if (err) return reject(err);
return resolve(JSON.parse(res));
});
});
};
console.log("Step 01");
const responseScript = await goToScript();
console.log("Step 03");
res.json(responseScript);
});
module.exports = router;

jest timeouts when calling a database query

I have a test file like this.
const { silBastan } = require("../database.js");
const axios = require('axios').default;
describe("authentication", () => {
describe("when data schema is valid", () => {
test("returns 201 response code if the user doesnt already exists", async () => {
await silBastan();
const response = await axios.post('http://localhost:8000/auth/register', {
email: "my_email",
password: "1234"
});
expect(response.status).toBe(201);
});
});
});
And silBastan is defined here like this
const pg = require("pg");
const client = new pg.Client();
async function silBastan() {
return await client.query(`DELETE FROM account`);
}
Of course i made sure the server started and connected to the database before running the tests.
I wondered if there is something wrong with silBastan and tested it inside a express route handler like this
router.post('/register', async (req, res) => {
const { email, password } = req.body;
await silBastan();
try {
await db.createAccount(email, password);
res.sendStatus(201);
} catch (e) {
res.status(400).json({ err: "Already exists" });
}
});
and there was no timeout. And after this i returned another promise from silBastan like this:
async function silBastan() {
// return await client.query(`DELETE FROM account`);
return new Promise((resolve) => setTimeout(() => resolve(), 1000));
}
And again there is no timeout. I tried couple of other variations as well like these:
function silBastan() {
return client.query(`DELETE FROM account`);
}
async function silBastan() {
await client.query(`DELETE FROM account`);
}
Nothing worked i always get this message:
thrown: "Exceeded timeout of 5000 ms for a test.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
I don't think the problem is with the function because i would get the same behavior in the route handler too.

Jest returning false positives for Firebase functions?

Having to write some Jest tests for Firebase functions and I'm running into a little trouble. Here's a basic example of what I'm trying to do:
databaseUtils.js
function funcOne() {
return Promise.resolve();
}
function funcTwo() {
return Promise.resolve();
}
index.js
exports.myTest = functions.https.onRequest(async (req, res) => {
try {
await databaseUtils.funcOne();
await databaseUtils.funcTwo();
return res.status(200);
} catch (error) {
console.log(error);
return res.status(403);
}
});
index.test.js
describe('myTest', () => {
it('test 1', async () => {
const req = {}
const res = {
status: jest.fn()
};
await functions.myTest(req, res);
expect(res.status).toHaveBeenCalledWith(200)
});
it('test 2', async () => {
const req = {}
const res = {
status: code => {
expect(code).toBe(403);
}
};
await functions.myTest(req, res);
});
it('test 3', (done) => {
const req = {}
const res = {
status: code => {
expect(code).toBe(403);
done();
}
};
functions.myTest(req, res);
});
})
When I run my tests, Test 1 fails while Test 2 and 3 pass, but with the wrong values:
JestAssertionError: expect(received).toBe(expected) // Object.is equality
Expected: 403
Received: 200
matcherResult: {
actual: 200,
expected: 403,
message: [Function],
name: 'toBe',
pass: false
}
Been running around in circles trying to get this to work properly but I can't figure out, what am I doing wrong?
Once I've got this figured out I'd then mock funcOne / funcTwo to return a rejected promise so I can get the right status code.
onRequest is promise-aware and expected to return a promise of the entire work it does. That there's a dangling promise inside onRequest callback is an antipattern.
It should be:
return auth.decodeToken(req)...
That there are raw promises with then and catch inside async contributes to the problem because async..await is sugar syntax that allows to avoid several common problems with promises. The said problem as well as nested promises could be avoided if this were written as:
try {
const decodedToken = await auth.decodeToken(req);
...
} catch (err) {
...
}

How can I test a catch block using jest nodejs?

How can i test the catch block on a es6 Class
const fs = require('fs');
class Service {
constructor(accessToken) {
this.accessToken = accessToken;
}
async getData() { // eslint-disable-line class-methods-use-this
try {
const data = fs.readFileSync(`${__dirname}/models/mockData.json`, { encoding: 'utf8' });
const returnData = JSON.parse(data);
return returnData;
} catch (err) {
return err;
}
}
}
module.exports = Service;
using jest how can i write the test case to cover the catch block also.
You can mock the method readFileSync from fs to force it to return undefined. JSON.parse(undefined) will throw an error, thus you can check the catch side of the code.
fs.readFileSync = jest.fn()
fs.readFileSync.mockReturnValue(undefined);
First of all, in the catch side you should throw the error. Just returning it is not a good practise when managing errors, from my point of view. But there is people doing it.
const fs = require('fs');
class Service {
constructor(accessToken) {
this.accessToken = accessToken;
}
async getData() { // eslint-disable-line class-methods-use-this
try {
const data = fs.readFileSync(`${__dirname}/models/mockData.json`, { encoding: 'utf8' });
const returnData = JSON.parse(data);
return returnData;
} catch (err) {
throw err;
}
}
}
Having this code, you can actually test your catch block code in two different ways with Jest:
beforeEach(() => {
fs.readFileSync = jest.fn();
});
afterEach(() => {
fs.readFileSync.mockClear();
});
test('Async expect test', () => {
fs.readFileSync.mockReturnValue(undefined);
const result = service.getData();
expect(result).rejects.toThrow();
});
test('Async / await test', async() => {
fs.readFileSync.mockReturnValue(undefined);
try {
await service.getData();
} catch (err) {
expect(err.name).toEqual('TypeError');
expect(err.message).toEqual(`Cannot read property 'charCodeAt' of undefined`);
}
});
Both of them imply to mock the readFileSync method from fs module as I suggested before. You can even mock the whole fs module with Jest. Or you could just mock the JSON.parse. There are plenty of options to be able to test the catch block.
Jest has its own method for testing exception, you can use toThrow. It looks something like this
test('throws on octopus', () => {
expect(() => {
drinkFlavor('octopus');
}).toThrow(); // Test the exception here
});
Note
Since your function is asynchronous, try to explicitly define your error, then use await to resolve/reject it, After that you can check for the actual rejection
test('throws on octopus', () => {
await expect(user.getUserName(3)).rejects.toEqual({
error: 'User with 3 not found.',
});
});

Paradoxical issue with mocha done() and async await

I have the following test case:
it("should pass the test", async function (done) {
await asyncFunction();
true.should.eq(true);
done();
});
Running it asserts:
Error: Resolution method is overspecified. Specify a callback or
return a Promise; not both.
And if I remove the done(); statement, it asserts:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure
"done()" is called; if returning a Promise, ensure it resolves.
How to solve this paradox?
You need to remove the done parameter as well, not just the call to it. Testing frameworks like Mocha look at the function's parameter list (or at least its arity) to know whether you're using done or similar.
Using Mocha 3.5.3, this works for me (had to change true.should.be(true) to assert.ok(true) as the former threw an error):
const assert = require('assert');
function asyncFunction() {
return new Promise(resolve => {
setTimeout(resolve, 10);
});
}
describe('Container', function() {
describe('Foo', function() {
it("should pass the test", async function () {
await asyncFunction();
assert.ok(true);
});
});
});
But if I add done:
describe('Container', function() {
describe('Foo', function() {
it("should pass the test", async function (done) { // <==== Here
await asyncFunction();
assert.ok(true);
});
});
});
...then I get
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
Removing done as a param from it worked for me! Instead only use expect/should. Example is as follows:
getResponse(unitData, function callBack(unit, error, data){ try {
return request.post(unit, function (err, resp) {
if (!err && resp.statusCode === 200) {
if (resp.body.error) {
return callback(obj, JSON.stringify(resp.body.error), null);
}
return callback(obj, null, resp);
} else {
if (err == null) {
err = { statusCode: resp.statusCode, error: 'Error occured.' };
}
return callback(obj, err, null);
}
});
} catch (err) {
return callback(obj, err, null);
}}
BEFORE:
it('receives successful response', async (done) => {
const getSomeData = await getResponse(unitData, function callBack(unit, error, data){
expect(data.statusCode).to.be.equal(200);
done();
}) })
AFTER (works):
it('receives successful response', async () => {
const getSomeData = await getResponse(unitData, function callBack(unit, error, data){
expect(data.statusCode).to.be.equal(200);
}) })
Sometimes there are cases you need to use async/await + done function in mocha.
For example, in one of my socket.io unit test cases, I have to call db functions with async functions and test socket event handlers which are callback functions:
context("on INIT_CHAT", ()=> {
it("should create a room", async (done) => {
const user = await factory.create("User");
socket.emit("INIT_CHAT", user);
socket.on("JOIN_CHAT", async (roomId) => {
const room = await ChatRoom.findByPk(roomId);
expect(room).to.exist;
// then I need to close a test case here
done();
});
});
});
This will causes the exact same error as in the OP:
Error: Resolution method is overspecified. Specify a callback or return a Promise; not both.
My Workaround:
I just wrapped the entire test code in a promise generator:
context("on INIT_CHAT", ()=> {
it("should create a room", async () => {
const asyncWrapper = () => {
return new Promise(async (resolve) => {
const user = await factory.create("User");
socket.emit("INIT_CHAT", user);
socket.on("JOIN_CHAT", async (roomId) => {
const room = await ChatRoom.findByPk(roomId);
expect(room).to.exist;
resolve(true);
});
});
});
await asyncWrapper();
});
});

Categories