Problem running Jest using CLS with async functions - javascript

I can't seem to get CLS to work with Jest.
The following code:
export {}
const { promises: fs } = require('fs')
describe('CLS tests', () => {
test('Can test CLS', async () => {
var createNamespace = require('cls-hooked').createNamespace
var session = createNamespace('session')
session.run(async function () {
await fs.readFile('package.json', 'utf-8')
console.log('I cant log this')
})
})
})
Results in the following error:
Cannot log after tests are done. Did you forget to wait for something
async in your test?
Attempted to log "I cant log this".
Why is it that my test appears to be exiting early?

Maybe you need to abstract out the asynchronous operations. I tried this on my system and it works.
const {promises: fs} = require('fs')
const runSession = () => new Promise((resolve, reject) => {
const createNamespace = require('cls-hooked').createNamespace
const session = createNamespace('session')
session.run(() => {
fs.readFile('package.json', 'utf-8')
.then(resolve)
})
})
describe('CLS tests', () => {
test('Can test CLS', async () => {
const result = await runSession()
console.log('hello')
console.log(result)
expect(result).toBeTruthy()
console.log('after expect...')
})
})
Good Luck...

Related

Change async/await to Promise

I would like to change the following async/await code
const mongoose = require('mongoose')
const supertest = require('supertest')
const app = require('../app')
const api = supertest(app)
test("total number of blogs", async () => {
const response = await api.get('/api/blogs')
expect(response.body).toHaveLength(1)
})
afterAll(() => {
mongoose.connection.close()
})
to a Promise like this:
const mongoose = require('mongoose')
const supertest = require('supertest')
const app = require('../app')
const api = supertest(app)
test("total number of blogs", () => {
api.get('/api/blogs')
.then( response => {
expect(response.body).toHaveLength(1)
})
})
afterAll(() => {
mongoose.connection.close()
})
I could not manage to solve it correctly and I keep getting an error message:
To clarify #jonrsharpe's comment you should return the promises from your test function:
test("total number of blogs", () => {
return api.get('/api/blogs')
.then( response => {
expect(response.body).toHaveLength(1)
})
})
In the first version you return a Promise in the test. The testing library receives this promise and waits for it to finish before seeing the test as "finished".
So what you'll want to do is:
test("total number of blogs", () => {
return api.get('/api/blogs')
.then( response => {
expect(response.body).toHaveLength(1)
})
})
Don't forget that the first example already works with promises (async/await is just promises)
Edited because I looked into the docs :p

How to mock optimizelySDK.createInstance().onReady() using Jest?

Here is my mock file __mocks__/#optimizely/optimizely-sdk.js
const optimizelySDK = jest.requireActual('#optimizely/optimizely-sdk')
optimizelySDK.createInstance().onReady = () => ({ success: false }))
module.exports = optimizelySDK
Here is my test file Optimizely.test.js
import optimizelySDK from '#optimizely/optimizely-sdk'
test('onReady', () => {
const response = optimizelySDK.createInstance().onReady()
expect(response).toBe({ success: false })
})
I think I might be going about this all wrong. This worked perfectly when I try this with lodash. I believe this is because optimizelySDK is a class. I think I should be mocking that instead. How do I successfully mock and test optimizelySDK?
For anyone who came across this on Google, I had the same problem and got it working with jest:
jest.mock('#optimizely/optimizely-sdk', () => ({
...jest.requireActual('#optimizely/optimizely-sdk'),
createInstance: () => ({
getEnabledFeatures: jest.fn().mockReturnValueOnce(['featureA', 'featureB']),
onReady: jest.fn().mockResolvedValueOnce({ status: 200 })
})
}))
describe('my-test', () => {
it('should pass', async () => {
const result = await getFeatures()
console.log(result) // ['featureA', 'featureB']
// assert on result here
});
});
where my code looked something like:
const getFeatures = async (event) => {
try {
const optimizelyInstance = optimizelySDK.createInstance({
sdkKey: process.env.OPTIMIZLEY_SDK_KEY,
});
const optimizelyParameters = {}
return optimizelyInstance.onReady().then(() => {
const result = optimizelyInstance.getEnabledFeatures('id', optimizelyParameters);
return result;
});
} catch (err) {
console.error('Could not get features', err);
}
};

Locked it method in chai

I have a js file which supplies some db operations. This file works with promises only which can be chained. To test that class I work with an async function.
The problem is, that whenever I work with promises inside my test function the it function gets blocked for every other test later.
Here are two examples:
'use strict'
const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')
test()
async function test () {
await testGetUser()
console.log('1')
await testGetFaculties()
}
function testGetUser () {
return new Promise((resolve1) => {
describe('test get user', function () {
const db = require('../dbInterface')
it('test get user should be complete', function () {
db.dbFunctions.dropAll()
.then(onResolve => {
return db.dbFunctions.createTable(createTableStatements.createTableStatements.user)
}
)
.then(() => {
console.log('success create user table')
return db.dbFunctions.addUser('1', 'firstName', 'lastName', 'email')
})
.then(resolve => {
return db.dbFunctions.getUser('email', undefined)
})
.then(result => {
expect(result.toString().includes('dummy')).to.equal(false)
})
.then(resolve => {
return db.dbFunctions.dropAll()
})
.then(resolve => {
console.log('resolve')
resolve1()
})
.catch(err => console.log(err))
})
})
})
}
function testGetFaculties () {
return new Promise(resolve => {
describe('test get faculties', function () {
let db
before(function () {
db = require('../dbInterface')
})
console.log('displayed')
it('should work', function () {
console.log('locked')
expect(db.dbFunctions.getFaculties('hsa')).to.be.an('array').that.does.include('Science')
resolve()
})
})
})
}
And this is the output
resolve
1
displayed
As you can see console.log('locked') is not being processed.
What i figured out so far, that I only have this issue when I call expect within a then function. But this is necessary for my tests.
The test () function should contain much more tests, only for this question I shortened it.
And for clarification: If I only test methods type of testGetFaculties () which don't contains another promise chain inside it works like it should.
Any idea why this is like it is?
Most probably the console.log( 'locked' ); doesn't do anything, because your previous test case was not finished at all.
Writing describe, it, before inside a Promise and containing unreturned Promises is something that you should not do.
Much better test case would look like :
'use strict'
const exec = require('child_process').exec
const path = require('path')
const request = require('request')
const expect = require('chai').expect
const createTableStatements = require('../data')
// You use this in both test cases anyway
const db = require('../dbInterface');
describe('test get user', function () {
it('test get user should be complete', function () {
return db
// ^ returning promise will make sure that the test ends when the promise ends.
.dbFunctions
.dropAll()
.then(onResolve => { ... } )
...
)
} );
} );
describe('test get faculties', function () {
it('should work', function () {
return db
// ^ returning promise will make sure that the test ends when the promise ends.
.dbFunctions
.getFaculties('hsa')
.then( value => {
// ^ You actually need to test the value of the resolve promise
expect( value ).to.be.an('array').that.does.include('Science');
} )
} );
} );

Node.js - unit test for function with mocked promise inside

I'm currently making a small server in JavaScript and as part of the learning process I'm writing unit tests for the functions. Unfortunately I ran into major difficulties with a certain test that handles a promise. Below is the router module, with a separate handlePUT function for ease of testing.
const express = require('express');
const service = require('../service/user.service');
const dutyStatusRouter = express.Router();
const output = console;
function handlePUT(req, res) {
service.updateUserStatus()
.then((fulfilled) => {
res.status(fulfilled);
res.send();
})
.catch(() => {
res.status(500);
res.send();
});
}
dutyStatusRouter.route('/').put(handlePUT);
The updateUserStatus function basically toggles a Boolean in a database and looks somewhat like this:
function updateUserStatus() {
return new Promise((resolve, reject) => {
if (…) {
resolve(201);
} else if (…) {
resolve(200);
} else {
reject();
}
});
}
As for the unit tests, I'm using mocha/chai, with proxyquire to create a mock updateUserStatus.
const chai = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const serviceStub = {};
describe('=== Unit test ===', () => {
it('Handle PUT test: promise kept', async () => {
const dutyStatusRouter = proxyquire('../../router/duty-status.router', {
'../service/user.service': serviceStub,
});
serviceStub.updateUserStatus = () => {
return new Promise((resolve, reject) => {
resolve(200);
});
};
const res = {
status: sinon.fake(),
send: sinon.fake(),
};
await dutyStatusRouter.handlePUT({}, res);
chai.assert(res.status.calledOnceWith(200));
});
});
Whenever I try to run the unit test, I get the error Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.. If I try to add done() it still fails by giving the error message Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.
Found a solution that works, so I'm adding it here:
const chai = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const serviceStub = {};
const dutyStatusRouter = proxyquire('../../router/duty-status.router', {
'../service/user.service': serviceStub,
});
describe('=== Unit test ===', () => {
it('Handle PUT test: promise kept', (done) => {
serviceStub.updateUserStatus = sinon.stub().resolves(200);
const res = {
status: sinon.fake(),
send: sinon.fake(),
};
dutyStatusRouter.handlePUT({}, res).then(() => {
chai.assert(res.status.calledWith(200));
done();
});
});
});
Note: I changed the handlePUT function just a bit, it now looks like this (I just added a return):
function handlePUT(req, res) {
return service.updateUserStatus()
.then((fulfilled) => {
output.log('Promise fulfilled');
res.status(fulfilled);
res.send();
})
.catch(() => {
output.log('Promise unfulfilled');
res.status(500);
res.send();
});
}

Nodejs: Testing with sinon and async/await

Having trouble getting this test to run using sinon and async/await. Here is an example of what I'm doing:
// in file funcs
async function funcA(id) {
let url = getRoute53() + id
return await funcB(url);
}
async function funcB(url) {
// empty function
}
And the test:
let funcs = require('./funcs');
...
// describe
let stubRoute53 = null;
let stubFuncB = null;
let route53 = 'https://sample-route53.com/'
let id = '1234'
let url = route53 + id;
beforeEach(() => {
stubRoute53 = sinon.stub(funcs, 'getRoute53').returns(route53);
stubFuncB = sinon.stub(funcs, 'funcB').resolves('Not interested in the output');
})
afterEach(() => {
stubRoute53.restore();
stubFuncB.restore();
})
it ('Should create a valid url and test to see if funcB was called with the correct args', async () => {
await funcs.funcA(id);
sinon.assert.calledWith(stubFuncB, url)
})
Via console.log I've verified that funcA is producing the correct URL, however, I'm getting the error AssertError: expected funcB to be called with arguments. When I try calling stubFuncB.getCall(0).args it prints out null. So maybe it is my lack of understanding of async/await, but I cannot figure out why the url is not being passed to that function call.
Thanks
I think your funcs declaration is not correct. Sinon could not stub getRoute53 and funcB called inside funcA Try this one:
funcs.js
const funcs = {
getRoute53: () => 'not important',
funcA: async (id) => {
let url = funcs.getRoute53() + id
return await funcs.funcB(url);
},
funcB: async () => null
}
module.exports = funcs
tests.js
describe('funcs', () => {
let sandbox = null;
beforeEach(() => {
sandbox = sinon.createSandbox();
})
afterEach(() => {
sandbox.restore()
})
it ('Should create a valid url and test to see if funcB was called with the correct args', async () => {
const stubRoute53 = sandbox.stub(funcs, 'getRoute53').returns('https://sample-route53.com/');
const stubFuncB = sandbox.stub(funcs, 'funcB').resolves('Not interested in the output');
await funcs.funcA('1234');
sinon.assert.calledWith(stubFuncB, 'https://sample-route53.com/1234')
})
})
P.S. Also, use sandbox. It's easier to clean stubs

Categories