test rejecting an axios promise - javascript

I am trying to figure out how to correctly mock this code. I have figured out how to mock a success, but I cannot figure out how to mock the catch/reject block.
App code:
function getData(url = '') {
return new Promise((resolve, reject) => {
axios
.get(`${baseURL}${url}`)
.then((res) => {
resolve(res.data);
})
.catch((err) => {
reject(err);
});
});
}
in testing I have:
import axios from 'axios';
const MockAdapter = require('axios-moc-adapter');
describe('getData', () => {
it('test success', async () => {
const mock = new MockAdapter(axios);
mock.onGet(`${service.baseURL}/sites`).reply(200, 'success');
const axiosSpy = jest.spyOn(axios, 'get');
const rtn = await service.getData('/sites');
expect(axiosSpy).toHaveBeenCalled();
expect(rtn).toBe('success');
});
it('test failure', async () => {
const mock = new MockAdapter(axios);
mock.onGet(`${service.baseURL}/sites`).networkError();
const rtn = await service.getData('/sites').catch((err) => {
expect(err.message).toBe('message');
});
expect(rtn).toBe(undefined);
});
});
The test success test works, but when I test the failure, I get:
Expected: "message"
Received: "Cannot read properties of undefined (reading 'then')"
29 | mock.onGet(`${service.baseURL}/sites`).networkError();
30 | const rtn = await service.getData('/sites').catch((err) => {
> 31 | expect(err.message).toBe('message');
| ^
32 | });
33 | expect(rtn).toBe(undefined);
34 | });
How can I write a test that successfully passes the failure case? Specifically, I can make this test pass by not checking inside the catch block, but when I run coverage, it doesn't cover the catch block in the App code. How can I correctly cover this line?

Can't reproduce your issue, but .networkError() will throw an error with 'Network Error' message rather than 'message', see source code index.js#L141
service.ts:
import axios from 'axios';
export const baseURL = 'http://localhost:3000/api';
export function getData(url = '') {
return new Promise((resolve, reject) => {
axios
.get(`${baseURL}${url}`)
.then((res) => {
resolve(res.data);
})
.catch((err) => {
reject(err);
});
});
}
service.test.ts:
import * as service from './service';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
describe('getData', () => {
it('test success', async () => {
const mock = new MockAdapter(axios);
mock.onGet(`${service.baseURL}/sites`).reply(200, 'success');
const axiosSpy = jest.spyOn(axios, 'get');
const rtn = await service.getData('/sites');
expect(axiosSpy).toHaveBeenCalled();
expect(rtn).toBe('success');
});
it('test failure', async () => {
const mock = new MockAdapter(axios);
mock.onGet(`${service.baseURL}/sites`).networkError();
const rtn = await service.getData('/sites').catch((err) => {
expect(err.message).toBe('Network Error');
});
expect(rtn).toBe(undefined);
});
it('test failure 2', async () => {
const mock = new MockAdapter(axios);
mock.onGet(`${service.baseURL}/sites`).networkError();
await expect(service.getData('/sites')).rejects.toThrowError('Network Error');
});
});
test result:
PASS examples/69947398/service.test.ts (8.89 s)
getData
✓ test success (5 ms)
✓ test failure (1 ms)
✓ test failure 2 (2 ms)
------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 100 | 0 | 100 | 100 |
service.ts | 100 | 0 | 100 | 100 | 4
------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 9.52 s

Related

Stubing AWS SQS client

Im trying to stub aws-sdk/client-sqs calls. Im both new to aws and stubbing and I cant find much in the docs to do with stubbing using mocha/chai
My file
import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand } from '#aws-sdk/client-sqs'
const sqsClient = new SQSClient()
const params = {
AttributeNames: ['SentTimestamp'],
MaxNumberOfMessages: 10,
QueueUrl: paymentsUrl,
//....more params
}
const getPolledMessages = async sqsClient => {
const data = await sqsClient.send(new ReceiveMessageCommand(params))
//....continue to do stuff with the data
}
My test is as below. I heavily borrowed from this test which stubs #aws-sdk/client-ses
import { ReceiveMessageCommand, SQSClient } from '#aws-sdk/client-sqs'
import { expect } from 'chai'
describe('mock sqs', () => {
let stub;
before(() => {
stub = sinon.stub(SQSClient.prototype, 'ReceiveMessageCommand')
})
after(() => stub.restore())
it('can send', async () => {
await SQSClient.send(new ReceiveMessageCommand(params))
expect(stub.calledOnce).to.be.true
})
})
Currently getting the following error
TypeError: Cannot stub non-existent property ReceiveMessageCommand
at Function.stub (node_modules/sinon/lib/sinon/stub.js:73:15)
at Sandbox.stub (node_modules/sinon/lib/sinon/sandbox.js:333:37)
You can stub out dependencies with link seams.. We should use proxyquire package to do that.
E.g.
index.ts:
import { SQSClient, ReceiveMessageCommand } from '#aws-sdk/client-sqs';
const sqsClient = new SQSClient({ region: 'REGION' });
const params = {
AttributeNames: ['SentTimestamp'],
MaxNumberOfMessages: 10,
QueueUrl: 'paymentsUrl',
};
export const getPolledMessages = async () => {
const data = await sqsClient.send(new ReceiveMessageCommand(params));
};
index.test.ts:
import proxyquire from 'proxyquire';
import sinon from 'sinon';
describe('68017252', () => {
it('should pass', async () => {
const sqsClientInstance = {
send: sinon.stub(),
};
const SQSClient = sinon.stub().returns(sqsClientInstance);
const ReceiveMessageCommand = sinon.stub();
const { getPolledMessages } = proxyquire('./', {
'#aws-sdk/client-sqs': {
SQSClient,
ReceiveMessageCommand,
},
});
await getPolledMessages();
sinon.assert.calledWithExactly(SQSClient, { region: 'REGION' });
sinon.assert.calledOnce(sqsClientInstance.send);
sinon.assert.calledWithExactly(ReceiveMessageCommand, {
AttributeNames: ['SentTimestamp'],
MaxNumberOfMessages: 10,
QueueUrl: 'paymentsUrl',
});
});
});
test result:
68017252
✓ should pass (3331ms)
1 passing (3s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------

how to test cheerio js

this is my code and i don't know how to write test for it:
html.js
const getHtml = async (url) => {
const { data } = await axios.get(url);
return data;
};
const cheerioInit = async (url) => cheerio.load(await getHtml(url));
module.exports = {
cheerioInit,
getHtml
};
i think i should mock this, but don't know how to do that. i wrote this but getting error:
const htmlJs = require("./html");
describe("html", () => {
it("initializes cheerio js", () => {
const mock = jest.spyOn(htmlJs, "cheerioInit");
expect(mock).toHaveBeenCalled();
});
});
this is error:
You can use jest.spyOn(object, methodName) to mock axios.get() method and its resolved value and mock cheerio.load() method with a fake implementation. After executing the getHtml and cheerioInit functions, make assertion for above mocks to check if they have been called with specific arguments.
E.g.
html.js:
const axios = require('axios');
const cheerio = require('cheerio');
const getHtml = async (url) => {
const { data } = await axios.get(url);
return data;
};
const cheerioInit = async (url) => cheerio.load(await getHtml(url));
module.exports = {
cheerioInit,
getHtml,
};
html.test.js:
const { getHtml, cheerioInit } = require('./html');
const axios = require('axios');
const cheerio = require('cheerio');
describe('html', () => {
afterEach(() => {
jest.restoreAllMocks();
});
describe('getHtml', () => {
it('should get html', async () => {
const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: '<div>teresa teng</div>' });
const actual = await getHtml('http://localhost:3000');
expect(actual).toEqual('<div>teresa teng</div>');
expect(getSpy).toBeCalledWith('http://localhost:3000');
});
});
describe('cheerioInit', () => {
it('should initializes cheerio', async () => {
const loadSpy = jest.spyOn(cheerio, 'load').mockImplementation();
const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: '<div>teresa teng</div>' });
await cheerioInit('http://localhost:3000');
expect(loadSpy).toHaveBeenCalledWith('<div>teresa teng</div>');
});
});
});
unit test result:
PASS examples/66304918/html.test.js
html
getHtml
✓ should get html (3 ms)
cheerioInit
✓ should initializes cheerio
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
html.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 4.088 s

Mocking method of instance with Jest

How to mock the call of service.request in the code bellow?
import url from 'url'
import jayson from 'jayson/promise'
export async function dispatch(webHook, method, payload) {
const service = jayson.Client.https({ ...url.parse(webHook) })
return service.request(method, { ...payload })
}
In my unit-test I want to do something like this
jest.mock("") // what should go here?
it(() => {
const method = 'test'
expect(request).toHaveBeenCalledWith(method...) ?
})
UPDATE
I updated with my findings my code, but still no luck
import { Client } from 'jayson/promise'
import { dispatch } from '../src/remote'
jest.mock('jayson')
describe('remote', () => {
let spy: jest.SpyInstance<any>
beforeEach(() => {
spy = jest.spyOn(Client.https.prototype, 'request')
})
afterEach(() => {
spy.mockClear()
})
it('should invoke request method', () => {
const url = 'http://example.com:8000'
const method = ''
const payload = {}
dispatch(url, method, payload)
expect(spy).toHaveBeenCalledWith({})
})
})
You can use jest.mock to mock jayson/promise module. Don't need to use jest.spyOn.
E.g.
index.ts:
import url from 'url';
import jayson from 'jayson/promise';
export async function dispatch(webHook, method, payload) {
const service = jayson.Client.https({ ...url.parse(webHook) });
return service.request(method, { ...payload });
}
index.test.ts:
import { dispatch } from './';
import jayson, { HttpsClient } from 'jayson/promise';
import { mocked } from 'ts-jest';
jest.mock('jayson/promise');
const httpsClientMock = mocked(jayson.Client.https);
describe('65924278', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
const url = 'http://example.com:8000';
const method = '';
const payload = {};
const serviceMock = ({
request: jest.fn(),
} as unknown) as HttpsClient;
httpsClientMock.mockReturnValueOnce(serviceMock);
await dispatch(url, method, payload);
expect(jayson.Client.https).toBeCalledTimes(1);
expect(serviceMock.request).toBeCalledWith('', {});
});
});
unit test result:
PASS examples/65924278/index.test.ts (10.748 s)
65924278
√ should pass (13 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 13.15 s

TypeError: sns.publish is not a function , jest mock AWS SNS

I am trying to mock AWS.SNS and I am getting error. I referred posts on StackOverflow and could come up with below. Still, I am getting an error. I have omitted the irrelevant portion. Can someone please help me?
Below is my index.ts
import { SNS } from "aws-sdk";
export const thishandler = async (event: thisSNSEvent): Promise<any> => {
// I have omitted other code that works and not related to issue I am facing.
// I am receiving correct value of 'snsMessagetoBeSent' I verified that.
const response = await sendThisToSNS(snsMessagetoBeSent);
} // thishandler ends here
async function sendThisToSNS(thisMessage: snsAWSMessage) {
const sns = new SNS();
const TOPIC_ARN = process.env.THIS_TOPIC_ARN;
var params = {
Message: JSON.stringify(thisMessage), /* required */
TopicArn: TOPIC_ARN
};
return await sns.publish(params).promise();
}
My test case is below
jest.mock('aws-sdk', () => {
const mockedSNS = {
publish: jest.fn().mockReturnThis(),
promise: jest.fn()
};
return {
SNS: jest.fn(() => mockedSNS),
};
});
import aws, { SNS } from 'aws-sdk';
const snsPublishPromise = new aws.SNS().publish().promise;
import { thishandler } from "../src/index";
describe("async testing", () => {
beforeEach(() => {
jest.restoreAllMocks();
jest.resetAllMocks();
});
it("async test", async () => {
const ENRICHER_SNS_TOPIC_ARN = process.env.ENRICHER_SNS_TOPIC_ARN;
process.env.ENRICHER_SNS_TOPIC_ARN = "OUR-SNS-TOPIC";
const mockedResponseData ={
"Success": "OK"
};
(snsPublishPromise as any).mockResolvedValueOnce(mockedResponseData);
const result = await thishandler(thisSNSEvent);
});
I get error as TypeError: sns.publish is not a function
Here is the unit test solution:
index.ts:
import { SNS } from 'aws-sdk';
export const thishandler = async (event): Promise<any> => {
const snsMessagetoBeSent = {};
const response = await sendThisToSNS(snsMessagetoBeSent);
return response;
};
async function sendThisToSNS(thisMessage) {
const sns = new SNS();
const TOPIC_ARN = process.env.THIS_TOPIC_ARN;
const params = {
Message: JSON.stringify(thisMessage),
TopicArn: TOPIC_ARN,
};
return await sns.publish(params).promise();
}
index.test.ts:
import { thishandler } from './';
import { SNS } from 'aws-sdk';
jest.mock('aws-sdk', () => {
const mSNS = {
publish: jest.fn().mockReturnThis(),
promise: jest.fn(),
};
return { SNS: jest.fn(() => mSNS) };
});
describe('59810802', () => {
let sns;
beforeEach(() => {
sns = new SNS();
});
afterEach(() => {
jest.clearAllMocks();
});
it('should pass', async () => {
const THIS_TOPIC_ARN = process.env.THIS_TOPIC_ARN;
process.env.THIS_TOPIC_ARN = 'OUR-SNS-TOPIC';
const mockedResponseData = {
Success: 'OK',
};
sns.publish().promise.mockResolvedValueOnce(mockedResponseData);
const mEvent = {};
const actual = await thishandler(mEvent);
expect(actual).toEqual(mockedResponseData);
expect(sns.publish).toBeCalledWith({ Message: JSON.stringify({}), TopicArn: 'OUR-SNS-TOPIC' });
expect(sns.publish().promise).toBeCalledTimes(1);
process.env.THIS_TOPIC_ARN = THIS_TOPIC_ARN;
});
});
Unit test results with 100% coverage:
PASS src/stackoverflow/59810802/index.test.ts (13.435s)
59810802
✓ should pass (9ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 15.446s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59810802

Using Jest to test anonymous function inside 3rd party module

I have a pretty simple module that use pg (node-postgre lib) module,
I'd like to implement a Jest test and while mocking the pg module I would like to run it's callback function to see the console.log runs and my callback is being invoked
I have mocked the module and tried to spy and replace the 'query' method but it failed and crushed,
any Idea what am I doing wrong?
Test Subject:
import {Pool} from 'pg';
const pool = new Pool();
module.exports = {
query: (text, params, callback) => {
const start = Date.now();
return pool.query(text, params, (err, res) => {
const duration = Date.now() - start;
console.log('executed query', {text, duration, rows: res.rowCount});
callback(err, res);
});
}
};
Test:
jest.mock('pg');
import module from './index';
import { Pool } from 'pg'
beforeAll(() => {
Pool.mockImplementation(()=>{return jest.fn()});
});
it('callback is called', () => {
const cb = (err, res) => true;
const query = jest.spyOn(Pool, "query"); // <---- Not right, Error
query.mockImplementation((a,b,c) => c({},{}));
const resolve = module.query('QUERY TEXT', { a: 1, b: 2}, cb);
resolve(); // <---- Not what I expect
expect(cb).toBeCalled();
});
});
Error thrown:
Error: Cannot spy the query property because it is not a function; undefined given instead
20 | it('callback is called', () => {
21 | const cb = (err, res) => true;
> 22 | const query = jest.spyOn(Pool, "query");
| ^
23 | query.mockImplementation((a,b,c) => c({},{}));
24 | const resolve = module.query('QUERY TEXT', { a: 1, b: 2}, cb);
25 | resolve();
at ModuleMockerClass.spyOn (node_modules/jest-mock/build/index.js:697:15)
at Object.spyOn (src/db/index.test.js:22:24)
Thanks
Here is the unit test solution:
index.js:
import { Pool } from 'pg';
const pool = new Pool();
module.exports = {
query: (text, params, callback) => {
const start = Date.now();
return pool.query(text, params, (err, res) => {
const duration = Date.now() - start;
console.log('executed query', { text, duration, rows: res.rowCount });
callback(err, res);
});
}
};
index.spec.js:
import mod from '.';
import { Pool } from 'pg';
jest.mock('pg', () => {
const mPool = {
query: jest.fn()
};
return { Pool: jest.fn(() => mPool) };
});
const pool = new Pool();
afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});
it('callback is called', done => {
let queryCallback;
pool.query.mockImplementation((text, params, callback) => {
queryCallback = callback;
});
const logSpy = jest.spyOn(console, 'log');
const userCallback = jest.fn();
mod.query('text', 'params', userCallback);
const mRes = { rowCount: 1 };
queryCallback(null, mRes);
expect(pool.query).toBeCalledWith('text', 'params', queryCallback);
expect(userCallback).toBeCalledWith(null, mRes);
expect(logSpy).toBeCalledWith('executed query', { text: 'text', duration: expect.any(Number), rows: 1 });
done();
});
Unit test result with 100% coverage:
PASS src/stackoverflow/52831401/index.spec.js
✓ callback is called (15ms)
console.log node_modules/jest-mock/build/index.js:860
executed query { text: 'text', duration: 0, rows: 1 }
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.026s
Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/52831401

Categories

Resources