Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I unit test keystonejs models?

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.

like image 694
endre Avatar asked Mar 06 '14 17:03

endre


1 Answers

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.

like image 135
Jed Watson Avatar answered Sep 20 '22 17:09

Jed Watson