knex/test/tape/harness.js

36 lines
898 B
JavaScript
Raw Normal View History

'use strict';
const tape = require('tape');
const debug = require('debug')('knex:tests');
2020-04-19 00:40:23 +02:00
module.exports = function (tableName, knex) {
return function (name, dialects, cb) {
2015-04-24 15:20:35 -04:00
if (arguments.length === 2) {
cb = dialects;
2015-04-24 15:20:35 -04:00
} else {
if (!Array.isArray(dialects)) {
dialects = [dialects];
2015-04-24 15:20:35 -04:00
}
if (dialects.indexOf(knex.client.dialect) === -1) {
debug('Skipping dialect ' + knex.client.dialect + ' for test ' + name);
2015-04-24 15:20:35 -04:00
return;
}
}
2020-04-19 00:40:23 +02:00
return tape(name, async function (t) {
const val = cb(t);
try {
if (!val || typeof val.then !== 'function') {
throw new Error('A promise should be returned to test ' + name);
}
await val;
} catch (err) {
t.error(err);
} finally {
await knex.truncate(tableName).catch((e) => t.fail(e));
t.end();
}
});
};
};