Sails.js: How to actually run tests

余生长醉 提交于 2019-11-30 14:21:17

See my test structure in we.js: https://github.com/wejs/we-example/tree/master/test

You can copy and paste in you sails.js app and remove we.js plugin feature in bootstrap.js

And change you package.json to use set correct mocha command in npm test: https://github.com/wejs/we-example/blob/master/package.json#L10

-- edit --

I created a simple sails.js 0.10.x test example, see in: https://github.com/albertosouza/sails-test-example

UPDATE-2: After struggling a lil bit with how much it takes to run unit test this way, i decided to create a module to load the models and turn them into globals just as sails does, but without taking so much. Even when you strip out every hook, but the orm-loader depending on the machine, it can easily take a couple seconds WITHOUT ANY TESTS!, and as you add models it gets slower, so i created this module called waterline-loader so you can load just the basics (Its about 10x faster), the module is not stable and needs test, but you are welcome to use it or modify it to suit your needs, or help me out to improve it here -> https://github.com/Zaggen/waterline-loader


UPDATE-1: I've added the info related to running your tests with mocha to the docs under Running tests section.


Just to expand on what others have said (specially what Alberto Souza said).

You need two steps in order to make mocha work with sails as you want. First, as stated in the sails.js Docs you need to lift the server before running your test, and to do that, you create a file called bootstrap.test.js (It can be called anything you like) in the root path (optional) of your tests (test/bootstrap.test.js) that will be called first by mocha, and then it'll call your test files.

var Sails = require('sails'),
  sails;

before(function(done) {
  Sails.lift({
    // configuration for testing purposes
  }, function(err, server) {
    sails = server;
    if (err) return done(err);
    // here you can load fixtures, etc.
    done(err, sails);
  });
});

after(function(done) {
  // here you can clear fixtures, etc.
  sails.lower(done);
});

Now in your package.json, on the scripts key, add this line(Ignore the comments)

 // package.json ....
 scripts": {
    // Some config 
    "test": "mocha test/bootstrap.test.js test/**/*.test.js"
  },
 // More config

This will load the bootstrap.test.js file, lift your sails server, and then runs all your test that use the format 'testname.test.js', you can change it to '.spec.js' if you prefer.

Now you can use npm test to run your test.

Note that you could do the same thing without modifying your package.json, and typying mocha test/bootstrap.test.js test/**/*.test.js in your command line

PST: For a more detailed configuration of the bootstrap.test.js check Alberto Souza answer or directly check this file in hist github repo

Given that they don't give special instructions and that they use Mocha, I'd expect that running mocha from the command line while you are in the parent directory of test would work.

Sails uses mocha as a default testing framework. But Sails do not handle test execution by itself.

So you have to run it manually using mocha command.

But there is an article how to make all Sails stuff included into tests. http://sailsjs.org/#/documentation/concepts/Testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!