如何修复 Jest DB fixtures 函数中的 "open handles" 错误?
How do I fix an "open handles" error in a Jest DB fixtures function?
我正在为 node/postgres 网络应用编写一些 Jest 测试。
这是灯具帮助文件:
/*eslint no-console: ["error", { allow: ["warn", "error"] }]*/
const exec = require('child_process').exec;
const create_sql = 'bin/sql/tables_setup.sql';
function prepare_db(){ //eslint-disable-line no-unused-vars
exec(
'PGPASSWORD="node_password" createdb -U node_user testdb; exit 0',
function(err) {
if (err !== null) {
console.error('Error creating db: ' + err);
}
exec(
`PGPASSWORD="node_password" psql -U node_user -d testdb -f ${create_sql}; exit 0`,
function(err){
if (err !== null) {
console.error('Error populating db: ' + err);
}
}
);
}
);
}
function clean_db(){ //eslint-disable-line no-unused-vars
exec(
'PGPASSWORD="node_password" dropdb -U node_user testdb; exit 0',
function(err){
if (err !== null) {
console.error('Error dropping db: ' + err );
}
}
);
}
module.exports = { prepare_db, clean_db };
...这是测试文件...
const app = require('../app');
const request = require('supertest');
const { prepare_db, clean_db } = require('./helpers/dbtool');
describe('GET /requests', () => {
beforeEach( async () => {
await prepare_db();
});
it('retrieves no details on empty database', async done => {
const response = await request(app).get('/requests');
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe('[]');
done();
});
afterEach( async () => {
await clean_db();
});
});
测试通过了(呵呵),但我遇到了常见的 Jest has detected the following 2 open handles potentially keeping Jest from exiting
错误。我正在使用 jest --runInBand --detectOpenHandles test
来 运行 测试。
我尝试在调用 exec
时添加 {timeout: 500}
选项,但没有成功。我尝试更改 prepare_db()
和 clean_db()
的签名,将 done()
作为回调传入,并在 exec 之后调用 done()
。再一次,运气不好 (done()
undefined :-( ).
我是否错过了一些额外的调用来终止 exec
以某种方式解决这个问题?我的 async
/await
有问题吗?
哦!
见In node.js, how to use child_process.exec so all can happen asynchronously?
只需要更改辅助文件中的 exec 实例...
const util = require('util');
const exec = util.promisify(require('child_process').exec);
我正在为 node/postgres 网络应用编写一些 Jest 测试。
这是灯具帮助文件:
/*eslint no-console: ["error", { allow: ["warn", "error"] }]*/
const exec = require('child_process').exec;
const create_sql = 'bin/sql/tables_setup.sql';
function prepare_db(){ //eslint-disable-line no-unused-vars
exec(
'PGPASSWORD="node_password" createdb -U node_user testdb; exit 0',
function(err) {
if (err !== null) {
console.error('Error creating db: ' + err);
}
exec(
`PGPASSWORD="node_password" psql -U node_user -d testdb -f ${create_sql}; exit 0`,
function(err){
if (err !== null) {
console.error('Error populating db: ' + err);
}
}
);
}
);
}
function clean_db(){ //eslint-disable-line no-unused-vars
exec(
'PGPASSWORD="node_password" dropdb -U node_user testdb; exit 0',
function(err){
if (err !== null) {
console.error('Error dropping db: ' + err );
}
}
);
}
module.exports = { prepare_db, clean_db };
...这是测试文件...
const app = require('../app');
const request = require('supertest');
const { prepare_db, clean_db } = require('./helpers/dbtool');
describe('GET /requests', () => {
beforeEach( async () => {
await prepare_db();
});
it('retrieves no details on empty database', async done => {
const response = await request(app).get('/requests');
expect(response.status).toBe(200);
expect(JSON.stringify(response.body)).toBe('[]');
done();
});
afterEach( async () => {
await clean_db();
});
});
测试通过了(呵呵),但我遇到了常见的 Jest has detected the following 2 open handles potentially keeping Jest from exiting
错误。我正在使用 jest --runInBand --detectOpenHandles test
来 运行 测试。
我尝试在调用 exec
时添加 {timeout: 500}
选项,但没有成功。我尝试更改 prepare_db()
和 clean_db()
的签名,将 done()
作为回调传入,并在 exec 之后调用 done()
。再一次,运气不好 (done()
undefined :-( ).
我是否错过了一些额外的调用来终止 exec
以某种方式解决这个问题?我的 async
/await
有问题吗?
哦!
见In node.js, how to use child_process.exec so all can happen asynchronously?
只需要更改辅助文件中的 exec 实例...
const util = require('util');
const exec = util.promisify(require('child_process').exec);