Created
April 5, 2016 11:28
A simple integration test file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
let Promise = require('bluebird'); | |
let db = require('../config/knexfile')[process.env.NODE_ENV]; | |
let knex = require('knex')(db); | |
/** | |
* Loads fixture data. Expects fixture data to be exported by a file in the | |
* ./fixtures/ directory (e.g. ./fixtures/users.js). | |
* | |
* @param {string[]} fixtures A list of fixtures to load. Must match the file | |
* without the .js extension. For example, a value | |
* of ['users', 'tokens'] would load data from | |
* users.js and tokens.js. | |
* @return {Promise} A promise | |
*/ | |
exports.load = function(fixtures) { | |
console.log('init load'); | |
return Promise.resolve(fixtures) | |
.each(function(fixture) { | |
console.log('loading %s', fixture); | |
// eslint-disable-next-line global-require | |
let records = require('./fixtures/' + fixture); | |
// Just in case a test failed and exports.clear didn't get called, | |
// delete the table contents before insering fixtures. | |
return knex(fixture).del() | |
.then(function() { | |
return knex.insert(records).into(fixture); | |
}); | |
}) | |
.catch(function(err) { | |
console.log(err); | |
}); | |
}; | |
/** | |
* Deletes fixture data. Expects fixture data to be exported by a file in the | |
* ./fixtures/ directory (e.g. ./fixtures/users.js). | |
* | |
* @param {string[]} fixtures A list of fixtures whose data should be deleted. | |
* Must match the file without the .js extension. | |
* For example, a value of ['users', 'tokens'] would | |
* load data from users.js and tokens.js. | |
* @return {Promise} A promise | |
*/ | |
exports.clear = function(fixtures) { | |
return Promise.resolve(fixtures) | |
.map(function(fixture) { | |
return knex(fixture).del(); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable max-nested-callbacks, no-shadow */ | |
'use strict'; | |
let sinon = require('sinon'); | |
let clock = null; | |
let fixtures = require('../fixtures'); | |
let data = [ | |
'users', | |
'tokens' | |
]; | |
describe('USER & AUTHENTICATION ENDPOINTS', function() { | |
beforeEach(function() { | |
// clock = sinon.useFakeTimers(); | |
// return; | |
return fixtures.load(data); | |
}); | |
afterEach(function() { | |
// clock.restore(); | |
// return; | |
return fixtures.clear(data); | |
}); | |
context('POST /users', function() { | |
it('should prevent the creation of a user with an existing email address', function(done) { | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'll leave this thread with a link to the Knex issue that helped me resolve this: knex/knex#1267 (comment).