我有2个数据库;第一个用于开发和生产,第二个用于测试。 knexfile.js:
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 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', () => {
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数据库进行测试,但我无法通过测试。如何解决这个问题?
答案 0 :(得分:0)
您的配置似乎很好,因为以下测试工作。
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
课程进行查询