Here is the unit test solution:
index.js
:
import pg from 'pg';
import { someAsyncFunction } from './someAsyncFunction';
const someConnectionString = 'someConnectionString';
const test = 'test';
module.exports = async (event, context, callback) => {
const client = new pg.Client(someConnectionString);
try {
await client.connect();
} catch (e) {
return callback(e);
}
try {
await client.query(await someAsyncFunction(test));
client.end();
return callback(null, 'success');
} catch (e) {
client.end();
return callback(e);
}
};
someAsyncFunction.js
:
export async function someAsyncFunction() {}
index.test.js
:
import pg from 'pg';
import fn from './';
import { someAsyncFunction } from './someAsyncFunction';
jest.mock('./someAsyncFunction', () => {
return { someAsyncFunction: jest.fn() };
});
jest.mock('pg', () => {
const mClient = { connect: jest.fn(), query: jest.fn(), end: jest.fn() };
return { Client: jest.fn(() => mClient) };
});
describe('46152048', () => {
afterEach(() => {
jest.clearAllMocks();
});
afterAll(() => {
jest.resetAllMocks();
});
it('should query success', async (done) => {
someAsyncFunction.mockResolvedValueOnce('select 1;');
const mClient = new pg.Client();
mClient.connect.mockResolvedValueOnce();
await fn({}, {}, (err, result) => {
expect(pg.Client).toBeCalledWith('someConnectionString');
expect(someAsyncFunction).toBeCalledWith('test');
expect(mClient.query).toBeCalledWith('select 1;');
expect(mClient.end).toBeCalledTimes(1);
expect(err).toBeNull();
expect(result).toBe('success');
done();
});
});
it('should handle error if connect database failed', async () => {
const mError = new Error('network');
const mClient = new pg.Client();
mClient.connect.mockRejectedValueOnce(mError);
await fn({}, {}, (err, result) => {
expect(err.message).toBe('network');
expect(result).toBeUndefined();
});
});
it('should handle error if query failed', async () => {
const mError = new Error('network');
const mClient = new pg.Client();
mClient.connect.mockResolvedValueOnce();
mClient.query.mockRejectedValueOnce(mError);
await fn({}, {}, (err, result) => {
expect(err.message).toBe('network');
expect(mClient.end).toBeCalledTimes(1);
expect(result).toBeUndefined();
});
});
});
unit test result with coverage report:
PASS src/stackoverflow/46152048/index.test.js
46152048
✓ should query success (11ms)
✓ should handle error if connect database failed (1ms)
✓ should handle error if query failed (1ms)
----------|----------|----------|----------|----------|-------------------|
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: 3 passed, 3 total
Snapshots: 0 total
Time: 4.103s, estimated 10s
source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/46152048