8

Is there any way to run tests for keystonejs that also hit a test or real mongodb instance?

It would be nice if similar to the way Django does it.

fearphage
  • 16,808
  • 1
  • 27
  • 33
endre
  • 1,363
  • 1
  • 11
  • 22
  • There is now https://github.com/webteckie/keystonejs-stub but it is not an official part of Keystone (yet). – w00t Jun 08 '16 at 11:52

1 Answers1

14

There aren't any official examples of implementing unit testing for KeystoneJS sites yet, but there wouldn't be anything stopping you from writing tests with a framework like mocha, the way you would in any other node.js app.

You'd want to initialise Keystone, register your models, then connect to the database and execute tests without starting the web server. Something like this:

./tests.js

var keystone = require('keystone');

keystone.init({
    'name': 'Your Project'
});

keystone.import('models');
keystone.mongoose.connect('localhost', 'your-database');
keystone.mongoose.connection.on('open', function() {

    // Run tests here

    // Use keystone.list('Key') to access Lists and execute queries
    // as you would in your main application

});

then run tests.js, or make it an npm / grunt / etc. script.

Keep an eye on issue #216 for an integrated testing framework.

Jed Watson
  • 20,150
  • 3
  • 33
  • 43