How do I unit test keystonejs models?
Asked Answered
B

1

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.

Bacterin answered 6/3, 2014 at 17:0 Comment(1)
There is now github.com/webteckie/keystonejs-stub but it is not an official part of Keystone (yet).Dippold
M
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.

Malvie answered 9/3, 2014 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.