Jam*_*son 5 javascript postgresql node.js jestjs pg-promise
在 Jest 中测试函数后,我需要关闭 PG-Promise 数据库连接。
这是在一个地方(db.js)和required 在任何需要它的地方初始化。在下面的代码的情况下,seed.js需要它,seed.spec.js正在测试它。
我知道afterAllJest 中有一个钩子,但这会在任何地方关闭连接,这可能会导致测试错误地失败?
该问题已通过该--forceExit选项解决,但它给出了一条错误消息,并且感觉不是解决此问题的正确方法?
数据库.js:
const pgp = require('pg-promise')();
const db = pgp(connection);
module.exports = {db, pgp};
Run Code Online (Sandbox Code Playgroud)
种子.spec.js:
require('dotenv').config();
const {pgp} = require('./db');
expect.extend(require('jest-json-schema').matchers);
const schema = require('./schemas');
const generate = require('./generate');
const seed = require('./seed');
const data = generate();
test ('the data returned by the seed function matches the schema', () => {
return seed(data)
.then(outputData => {
expect(outputData).toMatch(schema);
});
});
Run Code Online (Sandbox Code Playgroud)
PS我见过类似的问题,但没有一个完全符合我的情况。
与任何其他数据库一样,连接应该在afterAll.
作为基准状态,它要么pgp.end()或db.$pool.end():
afterAll(db.$pool.end);
Run Code Online (Sandbox Code Playgroud)
从 pg-promise v10.11.0,您不再需要明确地关闭池。相反,您可以只设置连接选项allowExitOnIdle: true,让进程在池空闲时退出。
| 归档时间: |
|
| 查看次数: |
2537 次 |
| 最近记录: |