Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
Created April 5, 2016 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilkerson/d62262090f3ce2fa4fe666d4d95ebadc to your computer and use it in GitHub Desktop.
Save robwilkerson/d62262090f3ce2fa4fe666d4d95ebadc to your computer and use it in GitHub Desktop.
A simple integration test file
'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();
});
};
/* 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();
});
});
});
@robwilkerson
Copy link
Author

The before|afterEach() work fine as written above. They also work when written as:

    beforeEach(function() {
        clock = sinon.useFakeTimers();
        return;
        // return fixtures.load(data);
    });
    afterEach(function() {
        clock.restore();
        return;
        // return fixtures.clear(data);
    });

@robwilkerson
Copy link
Author

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;
            });
    });

@robwilkerson
Copy link
Author

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;
            });
    });

@robwilkerson
Copy link
Author

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