[英]How to test async await pg connection with jest?
I am trying to test a function which makes a connection to pg, using async/await
,我正在尝试使用async/await
测试一个与 pg 建立连接的函数,
import pg from 'pg';
module.exports.test = 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);
}
};
Don't really understand how I would mock this using jest?真的不明白我将如何用玩笑来嘲笑这个? Any ideas?有什么想法吗?
first of all, in jest you can return a promise to test async code:首先,开玩笑,你可以返回一个测试异步代码的承诺:
import pg from 'pg';
module.exports.test = async (event, context) => {
const client = new pg.Client(someConnectionString);
try {
await client.connect();
await client.query(await someAsyncFunction(test));
} finally {
client.end();
}
};
As any async
function returns a promise you should not have to handle manually passing the error to a callback.由于任何async
函数都会返回一个承诺,因此您不必手动处理将错误传递给回调。
Now to the question about how to mock pg, you can create a fake pg object, but that's rarely ideal, I'll just show an example to answer the actual question.现在关于如何模拟 pg 的问题,您可以创建一个假 pg 对象,但这很少是理想的,我将仅展示一个示例来回答实际问题。
You should try to setup a test db before running your tests, and then clean it up after, this way you don't have to mock anything and can use the real pg
this would improve a lot the quality of the tests.您应该在运行测试之前尝试设置一个测试数据库,然后在之后对其进行清理,这样您就不必模拟任何内容并且可以使用真正的pg
这将大大提高测试的质量。
// mock example:
class MockClient {
async connect() {
}
async query() {
}
}
Here is the unit test solution:这是单元测试解决方案:
index.js
: 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
: someAsyncFunction.js
:
export async function someAsyncFunction() {}
index.test.js
: 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源代码: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/46152048
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.