I am trying to test my node app.js script in which I have an asynchronous request sendMessageRequest () to a function sendSmtpMessage() [ a Promise ]
app.js
const sendSmtpMessage = require("./sendSmtpMessage.js");
const keys = {....};
const mailOptions = {...}
const sendMessageRequest = async () => {
try {
const result = await sendSmtpMessage(keys,mailOptions);
console.log("... SEND MSG REQUEST FULLFILLED: ", result);
} catch(err){
console.log("... SEND MSG REQUEST FAILED: ");
}
};
sendMessageRequest();
I wrote the following app.spec.js, according to doc on Testing asynchronous code ( with async/await); but I guess my sendSmtpMessage() mocking is wrong...
app.spec.js
jest.mock("../sendSmtpMessage.js");
const sendSmtpMessage = require("../sendSmtpMessage.js");
const app = require("../app.js");
// sendSmtpMessage is a mock function
sendSmtpMessage.mockImplementation(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
(oauth2ClientMock.refreshToken !== "undefined")? resolve() : reject()
, 2000
});
})
});
describe('app', () => {
let keys, mailOptions;
beforeEach(() => {
keys = {....};
mailOptions = {....}
});
afterEach(() => {
keys = {};
mailOptions = {};
});
it("should call successfully sendMessageRequest", async () => {
// GIVEN
// WHEN
// THEN
expect.assertions(1);
await expect(sendSmtpMessage).resolves.toBe("OK");
});
it("should call unsuccessfully sendMessageRequest", async () => {
// GIVEN
// WHEN
keys.oauth.refresh_token = null;
// THEN
expect.assertions(1);
await expect(sendSmtpMessage).rejects.toBeTruthy();
});
});
As the console.log output is showing errors on both expectations in each test ( on resolve and reject )
console.log
jest --detectOpenHandles --coverage "app.spec.js"
FAIL test/app.spec.js
app
✕ should call successfully sendMessageRequest (15ms)
✕ should call unsuccessfully sendMessageRequest (2ms)
● app › should call successfully sendMessageRequest
expect(received).resolves.toBe()
received value must be a Promise.
Received:
function: [Function mockConstructor]
52 | // THEN
53 | expect.assertions(1);
> 54 | await expect(sendSmtpMessage).resolves.toBe("OK");
| ^
55 | });
56 |
57 | it("should call unsuccessfully sendMessageRequest", async () => {
at Object.toBe (node_modules/expect/build/index.js:158:13)
at Object.toBe (test/app.spec.js:54:44)
● app › should call successfully sendMessageRequest
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
51 | // WHEN
52 | // THEN
> 53 | expect.assertions(1);
| ^
54 | await expect(sendSmtpMessage).resolves.toBe("OK");
55 | });
56 |
at Object.assertions (test/app.spec.js:53:12)
● app › should call unsuccessfully sendMessageRequest
expect(received).rejects.toBeTruthy()
received value must be a Promise.
Received:
function: [Function mockConstructor]
61 | // THEN
62 | expect.assertions(1);
> 63 | await expect(sendSmtpMessage).rejects.toBeTruthy();
| ^
64 | });
65 |
66 | });
at Object.toBeTruthy (node_modules/expect/build/index.js:203:13)
at Object.toBeTruthy (test/app.spec.js:63:43)
● app › should call unsuccessfully sendMessageRequest
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
60 | keys.oauth.refresh_token = null;
61 | // THEN
> 62 | expect.assertions(1);
| ^
63 | await expect(sendSmtpMessage).rejects.toBeTruthy();
64 | });
65 |
at Object.assertions (test/app.spec.js:62:12)
Where am I wrong ? I don't understand very well the testing process of such plain js scripts... ( use to work with vue.js, test-utils ...)
thanks for feedback and eventually on any link to make me understanding the test unit in such case...
you are not awaiting the sendMessageRequest method call itself
const sendSmtpMessage = require("./sendSmtpMessage.js");
const keys = {....};
const mailOptions = {...}
const sendMessageRequest = async () => {
try {
const result = await sendSmtpMessage(keys,mailOptions);
console.log("... SEND MSG REQUEST FULLFILLED: ", result);
} catch(err){
console.log("... SEND MSG REQUEST FAILED: ");
}
};
(async function() {
await sendMessageRequest();
})();
Related
I have a Promise.all with async calls to three different services and assigns the resulting lists to three variables
const { getList1 } = require('./module1');
const { getList2 } = require('./module2');
const { getList3 } = require('./module3');
...
const funcToTest = async (params) => {
...
const [list1, list2, list3] = await Promise.all([
getList1(key),
getList2(key),
getList3(key),
]);
// do some stuff
//return promise
}
Here is the getList1 snippet
// ./module1
exports.getList1 = async (param1) => {
...
try {
// make a request that returns a list
return list;
} catch (err) {
if ( err.statusCode === 404) {
return [];
}
throw err;
}
};
To test the function, the Jest case should reject with an error when module1's getList1 request fails
const app = require('../../app');
const module1 = require('../../module1');
const module2 = require('../../module2');
const module3 = require('../../module3');
const nerdStore = require('../../nerdStore');
...
// setup stuff
jest.mock('../../module1');
beforeEach(() => {
module1.getList1
.mockResolvedValue([1,2,3]);
// setup more mocks, stuff .etc
});
afterEach(() => {
jest.clearAllMocks();
});
...
describe('Test funcToTest', () => {
it('should reject with an error when an module1 getList1 fails', async () => {
const errorMessage = 'An error occurred';
module1.getList1
.mockRejectedValue(new Error(errorMessage));
const result = await app.funcToTest(testParams);
await expect(result).rejects.toThrow(errorMessage);
});
The jest test case fails and keeps returning the following
Test funcToTest › should reject with an error when an module1 getList1 fails
An error occurred
120 |
121 | module1.getList1
> 122 | .mockRejectedValue(new Error(errorMessage));
expect(received).rejects.toThrow()
Matcher error: received value must be a promise or a function returning a promise
Received has value: undefined
123 |
124 | const result = await app.funcToTest(testParams);
> 125 | await expect(result).rejects.toThrow(errorMessage);
| ^
126 | });
Promise.allSettled([list1, list2, list3]).
then((results) => results.forEach((result) => console.log(result.status)));
I ran the above and it kept saying fulfilled for all
How do you resolve the undefined issue, mock and assert a rejected promise so that it makes the jest test pass?
Thanks
I have a confusing situation on my tests. See the code below.
file.js
class Test {
constructor() {}
async main() {
const a = await this.aux1("name1");
const b = await this.aux2("name2");
}
async aux1(name) {
console.log(name)
}
async aux2(name) {
console.log(name)
}
}
module.exports = Test;
file.spec.js
describe('Concept proof', () => {
const module = require("./file.js");
const inst = new module();
test('should call aux1', async () => {
const a = jest.fn(() => "return");
inst.aux1 = a
inst.main()
expect(a).toHaveBeenCalled()
});
test('should call aux2', async () => {
const b = jest.fn(() => "return");
inst.aux2 = b
inst.main()
expect(b).toHaveBeenCalled()
});
});
result
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
18 | inst.main()
19 |
> 20 | expect(b).toHaveBeenCalled()
| ^
21 | });
22 | });
at Object.<anonymous> (file.spec.js:20:19)
The method aux2 is not invoked if aux1 is called from main, but behave as expected if I remove aux1 call from main.
I was not able to find an explanation for that behave on the docs. I'm misunderstanding something, or the normal behave should be aux2 be called even if aux1 is called before?
Any help will be appreciated!
Any help will be apreciated!
You should use await inst.main();.
inst.main() is an asynchronous method. If await is not used, the main thread will continue to execute and will not wait for the completion of inst.main().
This means the expect(b).toHaveBeenCalled() statement will be executed before inst.main() method.
I have some code that calls an api. I am testing the functionality that is nested in try catches, and checking the correct behaviour happens when I throw an error with certain values.
async function createDeployment(namespace, config, name) {
try {
await k8sDeploymentApi.createNamespacedDeployment(namespace, config, true);
console.log('Successful deployment');
return Promise.resolve(true);
} catch (e) {
console.log(JSON.stringify(e));
if (e.response.statusCode === HTTP_CONFLICT) {
try {
await k8sDeploymentApi.replaceNamespacedDeployment(name, namespace, config, true);
console.log('Successfully replaced deployment');
return Promise.resolve(true);
} catch (error) {
return Promise.reject(error);
}
}
return Promise.reject(e);
}
}
I am mocking out the api calls node module here
var mockListNamespacedIngress;
var mockCreateNamespacedDeployment;
var mockReplaceNamespacedDeployment;
jest.mock('#kubernetes/client-node', () => {
mockListNamespacedIngress = jest.fn();
mockCreateNamespacedDeployment = jest.fn();
mockReplaceNamespacedDeployment = jest.fn();
return {
KubeConfig: jest.fn().mockImplementation(() => ({
loadFromCluster: jest.fn(),
loadFromDefault: jest.fn(),
makeApiClient: () => ({
listNamespacedIngress: mockListNamespacedIngress,
createNamespacedDeployment: mockCreateNamespacedDeployment,
replaceNamespacedDeployment: mockReplaceNamespacedDeployment,
}),
})),
};
});
And running the test here
it.only('Should return a log of success if replaced deployment resolves', async () => {
mockCreateNamespacedDeployment.mockRejectedValueOnce(() =>
Promise.reject(new Error({ response: { statusCode: 409 } })),
);
mockReplaceNamespacedDeployment.mockResolvedValueOnce(true);
// When
await expect(createDeployment()).resolves.toEqual(true);
});
However on the console.log(JSON.stringify(e)), I am only ever getting undefined coming back. So it does seem to throw the error, but gives no error object from the catch. Here is my full stacktrace
● Kubernetes deployment script tests › Kubernetes Calls › Should return a log of success if replaced deployment resolves
expect(received).resolves.toEqual()
Received promise rejected instead of resolved
Rejected to value: [TypeError: Cannot read property 'statusCode' of undefined]
143 |
144 | // When
> 145 | await expect(createDeployment()).resolves.toEqual(true);
| ^
146 | });
147 | });
148 |
at expect (node_modules/expect/build/index.js:134:15)
at Object.it.only (src/__tests__/index.test.js:145:13)
console.log
Successful deployment
at createDeployment (src/index.js:73:13)
console.log
undefined
at createDeployment (src/index.js:76:13)
You can mock an async function throwing an Error like this
mockCreateNamespacedDeployment.mockRejectedValueOnce(new Error({ response: { statusCode: 409 } });
and then you can expect
await expect(createDeployment()).rejects.toThrow();
Btw in your async function you can replace all of the
- Promise.resolve(xxx)
+ return xxx
// and
- Promise.reject(xxx)
+ throw xxx
Will be grateful if someone could clarify to me how test the async code from inquirer plugin for CLI app.
The module exports updateView function, which calls async inquirer.prompt inside.
const inquirer = require("inquirer");
const getAnswer = async (request) => {
const answer = await inquirer.prompt(request);
return answer;
}
Want to test with Jest that async code works, however all the Jest examples I have seen show ways to test async code only if I pass async function as a parameter.
So my function will have to be refactored to that:
getAnswers.js
const getAnswer = async (request, callback) => {
const answer = await callback(request);
return answer;
}
main.js
const inquirer = require("inquirer");
const getAnswers = require("./getAnswers");
const main = async () => {
const request = "abc";
const result = await getAnswers(request, inquirer.prompt);
...
}
And then test file will look like that:
test.js
const getAnswers = require("./getAnswers");
test("async code works", async () => {
//Arrange
const mock = async () => {
return "Correct Answer";
};
//Act
const result = await getAnswers("abc", mock);
//Assert
expect(result).toEqual("Correct Answer";);
});
Will be very grateful if someone could suggest if there is a way of testing async function without passing it as a callback?
And if the approach itself is correct.
You can use jest.mock to mock the imported dependencies rather than pass them as parameters. Here is the unit test solution:
getAnswers.js:
const inquirer = require('inquirer');
const getAnswers = async (request) => {
const answer = await inquirer.prompt(request);
return answer;
};
module.exports = getAnswers;
getAnswers.test.js:
const getAnswers = require('./getAnswers');
const inquirer = require('inquirer');
jest.mock('inquirer', () => {
return { prompt: jest.fn() };
});
describe('59495121', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
inquirer.prompt.mockResolvedValueOnce('Correct Answer');
const actual = await getAnswers('abc');
expect(actual).toBe('Correct Answer');
});
});
Unit test result with 100% coverage:
PASS src/stackoverflow/59495121/getAnswers.test.js (10.172s)
59495121
✓ should pass (6ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
getAnswers.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.367s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59495121
I have a parsePDF() method that calls extractText(), which returns its results in an async callback.
the question
How do I write a test that tests only that parsePDF calls extractText once, and with whatever path argument was passed to parsePDF? (I have separate unit tests for extractText and cleanUp.)
Here's the basic structure of the parsePDF method:
Parser.parsePDF(path, callback) {
Parser.extractText(path, function gotResult(err, raw_text) {
if (err) {
callback(err)
return;
}
var clean_text = Parser.cleanUp(raw_text)
callback(null, clean_text);
});
};
what I've tried
Despite reading the Sinon documentation on callsArg, Mocha/Chai/Sinon tutorials, various SO posts such as this one about stubbing function with callback - causing test method to timeout, I still haven't grokked what's needed to write a proper test.
This attempt fails with the message
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.
It makes sense, the callback isn't being fired
it('should call extractText() with path argument', function(done) {
sandbox.stub(Parser, 'extractText')
Parser.parsePDF('a known path', function(err, bill) {
expect(sinon).calledOnce(Parser.extractText).calledWith('a known path')
done()
});
});
But the following with yeilds() also fails with the message undefined is not a function pointing at the expect... line:
it('should call extractText() with path argument', function(done) {
sandbox.stub(UtilityBillParser, 'extractText').yields(null, 'some text')
Parser.parsePDF('a known path', function(err, bill) {
expect(sinon).calledOnce(Parser.extractText).calledWith('a known path')
done()
});
});
As does the following with .callsArg(1):
it('should call extractText() with path argument', function(done) {
sandbox.stub(UtilityBillParser, 'extractText').callsArg(1)
UtilityBillParser.parsePDF('a known path', function(err, bill) {
expect(sinon).calledOnce(UtilityBillParser.extractText).calledWith('a known path')
done()
});
});
Here is the unit test solution:
parser.js:
const Parser = {
parsePDF(path, callback) {
Parser.extractText(path, function gotResult(err, raw_text) {
if (err) {
callback(err);
return;
}
var clean_text = Parser.cleanUp(raw_text);
callback(null, clean_text);
});
},
extractText(path, callback) {
callback();
},
cleanUp(rawText) {
return "real clean text";
},
};
module.exports = Parser;
parser.test.js:
const Parser = require("./parser");
const sinon = require("sinon");
describe("Parser", () => {
afterEach(() => {
sinon.restore();
});
describe("#parsePDF", () => {
it("should clean up raw test", () => {
const callback = sinon.stub();
sinon.stub(Parser, "extractText").yields(null, "fake raw text");
sinon.stub(Parser, "cleanUp").returns("fake clean text");
Parser.parsePDF("./somepath", callback);
sinon.assert.calledWith(Parser.extractText, "./somepath", sinon.match.func);
sinon.assert.calledWith(Parser.cleanUp, "fake raw text");
sinon.assert.calledWith(callback, null, "fake clean text");
});
it("should handle err", () => {
const callback = sinon.stub();
const mError = new Error("some error");
sinon.stub(Parser, "extractText").yields(mError, null);
sinon.stub(Parser, "cleanUp").returns("fake clean text");
Parser.parsePDF("./somepath", callback);
sinon.assert.calledWith(Parser.extractText, "./somepath", sinon.match.func);
sinon.assert.calledWith(callback, mError);
});
});
});
Unit test result with coverage report:
Parser
#parsePDF
✓ should clean up raw test
✓ should handle err
2 passing (9ms)
----------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files | 93.75 | 100 | 77.78 | 93.75 | |
parser.js | 80 | 100 | 50 | 80 | 13,16 |
parser.test.js | 100 | 100 | 100 | 100 | |
----------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/30163720
Since you are stubbing out the extractText() method altogether, its callback never gets invoked, so none of the special processing associated with callbacks is required. The following works:
it('should call extractText() with path argument', function() {
sandbox.stub(Parser, 'extractText')
Parser.parsePDF('a known path', 'ignored');
sinon.assert.calledOnce(Parser.extractText)
sinon.assert.calledWith(Parser.extractText, 'a known path', sinon.match.func)
});
Using sinon.match.func in the second assertion reflects the fact that parsePDF creates its own anonymous function to pass to extractText; the best you can do is assert that some function was passed to extractText.