Created
April 5, 2016 11:28
-
-
Save robwilkerson/d62262090f3ce2fa4fe666d4d95ebadc to your computer and use it in GitHub Desktop.
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(); | |
}); | |
}); | |
}); |
In the case below, the beforeEach()
works, but the afterEach()
does not:
beforeEach(function() {
// clock = sinon.useFakeTimers();
// return;
return fixtures.load(data)
.then(function() {
clock = sinon.useFakeTimers();
return true;
});
});
afterEach(function() {
// clock.restore();
// return;
return fixtures.clear(data)
.then(function() {
clock.restore();
return true;
});
});
If I follow through on the promise returned from fixtures.load()
, I can get through the beforeEach()
and run the test:
beforeEach(function() {
return fixtures.load(data)
.then(function() {
clock = sinon.useFakeTimers(new Date().getTime());
return;
});
});
However, even after updating the afterEach()
in a similar manner the timeout still occurs in that callback:
afterEach(function() {
return fixtures.clear(data)
.then(function() {
// clock.restore();
return;
});
});
I'll leave this thread with a link to the Knex issue that helped me resolve this: knex/knex#1267 (comment).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
before|afterEach()
work fine as written above. They also work when written as: