При запуске приложения с sails lift
все работает нормально и как и ожидалось, но когда я запускаю это в тестовом наборе Jest, оно перестает работать.
В другом StackOverflow вопрос упоминается, что это означает, что конфигурация Knex не была правильно передана в Objection, но нет различий между Knex, использующим Jest, и работающим рабочим кодом.
Функция BeforeAll ()
async function createMockUsers() {
config.users.forEach(async function(user) {
await sails.helpers.createUser(
user.firstName,
user.lastName,
user.phone,
user.password,
user.role
);
});
}
// Before running any tests...
beforeAll(function(done) {
// process.env = config.env_variables;
sails.lift({
// Your sails app's configuration files will be loaded automatically,
// but you can also specify any other special overrides here for testing purposes.
// For example, we might want to skip the Grunt hook,
// and disable all logs except errors and warnings:
// hooks: { grunt: false },
log: { level: 'warn' },
port: 1338,
}, async function(err) {
if (err) { return done(err); }
await createMockUsers();
// here you can load fixtures, etc.
return done();
});
}, 60000);
Ошибка
TypeError: builder.knex(...).queryBuilder is not a function
at buildKnexQuery (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\objection\lib\queryBuilder\QueryBuilder.js:1359:63)
at doExecute (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\objection\lib\queryBuilder\QueryBuilder.js:1250:31)
at Bluebird.try.then (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\objection\lib\queryBuilder\QueryBuilder.js:563:19)
at tryCatcher (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\promise.js:694:18)
at _drainQueueStep (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\async.js:138:12)
at _drainQueue (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\async.js:131:9)
at Async.Object.<anonymous>.Async._drainQueues (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\async.js:147:5)
at Immediate.Async.drainQueues (C:\Users\Binny Zupnick\Development\onefiftyone\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:794:20)
at tryOnImmediate (timers.js:752:5)
at processImmediate [as _immediateCallback] (timers.js:729:5)
Модель
let Knex = require('knex');
const knexfile = require('../../knexfile');
const { Model } = require('objection');
// Give the knex object to objection.
const knexConfig = Knex(knexfile['connection']);
Model.knex(knexConfig);
class User extends Model {
static get tableName() {
return 'user';
}
}
module.exports = {
User: User
};
Knexfile
// Update with your config settings.
function getSsl() {
return process.env.PG_SSL !== "false";
}
function connectionStringToObject(connectionString) {
let connectionObject = {};
let noPostgres = connectionString.split('//')[1];
let splitByColon = noPostgres.split(':');
let splitByStrudel = splitByColon[1].split('@');
let splitBySlash = splitByColon[2].split('/');
connectionObject['user'] = splitByColon[0];
connectionObject['host'] = splitByStrudel[1];
connectionObject['password'] = splitByStrudel[0];
connectionObject['database'] = splitBySlash[1];
connectionObject['ssl'] = getSsl();
return connectionObject;
}
let connectionString = process.env.DATABASE_URL;
let connectionObject = connectionStringToObject(connectionString);
let dbConnection = {
client : "pg",
connection: connectionObject,
migrations: {
directory: './db/migrations'
},
useNullAsDefault: true
};
module.exports = {
connection: dbConnection
};