phantomjsmocha.jscasperjsknex.jsmocha-phantomjs

Running Knex Migrations Between Mocha Tests


I was using Mocha to test my Nodejs app with a test database. In order to reset the DB before each test I had the following code, which worked perfectly:

  process.env.NODE_ENV = 'test';
  var knex = require('../db/knex');

  describe("Add Item", function() {

      beforeEach(function(done) {
        knex.migrate.rollback()
        .then(function() {
          knex.migrate.latest()
          .then(function() {
            return knex.seed.run()
            .then(function() {
              done();
            });
          });
        });
      });

  ...

I've since switched from mocha to mocha-casperjs for my integration tests, and now the knex migrations won't run. I'm given this error message with the exact same before each hook:

     undefined is not an object (evaluating 'knex.migrate.rollback')
     phantomjs://platform/new-item.js:12:17
     value@phantomjs://platform/mocha-casperjs.js:114:20
     callFnAsync@phantomjs://platform/mocha.js:4314:12
     run@phantomjs://platform/mocha.js:4266:18
     next@phantomjs://platform/mocha.js:4630:13
     phantomjs://platform/mocha.js:4652:9
     timeslice@phantomjs://platform/mocha.js:12620:27

Solution

  • I'm pretty sure that migration functionality is not included in webpack build. If you go to http://knexjs.org/ open up debug console and checkout different clients e.g. mysql.migrate you see that there are no functions declared at all.

    Actually you can check it out with node too if you explicitly load webpack build instead of node lib.

    // load webpack build instead of node build...
    let knex = require('knex/build/knex')({client : 'pg'});
    console.log(knex.migrate);
    // outputs: {}
    

    So... the question is why are you trying to run your tests on PhantomJS browser instead of node.js?

    client.migrate is empty