mirror of
https://github.com/knex/knex.git
synced 2025-08-13 03:00:55 +00:00
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
'use strict';
|
|
const tape = require('tape');
|
|
const Bluebird = require('bluebird');
|
|
const debug = require('debug')('knex:tests');
|
|
|
|
module.exports = function(tableName, knex) {
|
|
return function(name, dialects, cb) {
|
|
if (arguments.length === 2) {
|
|
cb = dialects;
|
|
} else {
|
|
if (!Array.isArray(dialects)) {
|
|
dialects = [dialects];
|
|
}
|
|
if (dialects.indexOf(knex.client.dialect) === -1) {
|
|
debug('Skipping dialect ' + knex.client.dialect + ' for test ' + name);
|
|
return;
|
|
}
|
|
}
|
|
|
|
return tape(name, function(t) {
|
|
const disposable = Bluebird.resolve(true).disposer(function() {
|
|
return knex.truncate(tableName).finally(function() {
|
|
t.end();
|
|
});
|
|
});
|
|
|
|
Bluebird.using(disposable, function() {
|
|
const val = cb(t);
|
|
if (val && typeof val.then === 'function') {
|
|
return val.catch(function(err) {
|
|
t.error(err);
|
|
});
|
|
} else {
|
|
t.error(new Error('A promise should be returned to test ' + name));
|
|
t.end();
|
|
}
|
|
});
|
|
});
|
|
};
|
|
};
|