如何设置另一个数据库以测试knexfile?
问题描述:
我有2个数据库;第一个用于开发和生产,第二个用于测试。 knexfile.js:如何设置另一个数据库以测试knexfile?
const config = require('config');
const knexConfig = config.get('knexConfig');
module.exports = {
development: {
client: knexConfig.client,
connection: {
host: knexConfig.host,
database: knexConfig.database,
user: knexConfig.user,
password: knexConfig.password,
},
pool: {
min: knexConfig.min,
max: knexConfig.max,
},
},
test: {
client: 'sqlite3',
connection: {
filename: './file.db',
},
},
};
route.test.js:
const的模型=要求( '异议')模型;
const provider = require('../../../server/models/provider');
const Knex = require('knex');
const knexConfig = require('../../../knexfile');
const knex = Knex(knexConfig.test);
Model.knex(knex);
describe('Should test provider Model',() => {
test('should return provider',() => {
provider
.query()
.then((providers) => {
expect(providers).toBe('array');
});
});
});
我得到这个错误:
Test suite failed to run
ProcessTerminatedError: cancel after 2 retries!
at Farm.<anonymous> (node_modules/worker-farm/lib/farm.js:87:25)
at Array.forEach (<anonymous>)
at Farm.<anonymous> (node_modules/worker-farm/lib/farm.js:81:36)
at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5)
A worker process has quit unexpectedly! Most likely this is an initialization error.
我想连接到SQLite数据库的测试,但我不能这样做,通过我tests.How可以解决呢?
答
你的配置似乎很好,因为下面的测试工作。
https://runkit.com/embed/3w0umojslatc
require('sqlite3');
var knex = require("knex")({
client: 'sqlite3',
connection: {
filename: './db.db'
}
})
const { Model } = require('objection');
Model.knex(knex);
await Model.query().select('1');
看起来你混合DBB和TDD语法和测试返回之前有没有等待异步结果有一定误差。
先尝试这样的:
const Model = require('objection').Model;
const provider = require('../../../server/models/provider');
const Knex = require('knex');
const knexConfig = require('../../../knexfile');
const knex = Knex(knexConfig.test);
Model.knex(knex);
describe('Should test provider Model',() => {
it('should return provider',() => {
return Model.query().from('providers')
.then((providers) => {
expect(providers).toBe('array');
});
});
});
如果这样的作品,然后尝试做自己providers
类,您已经在问题
哪些测试运行,你使用不包含查询?看起来你并没有等待异步代码在测试用例退出之前终止。 –