简体   繁体   中英

Sails.js: How to actually run tests

I'm completely new to sails, node and js in general so I might be missing something obvious.

I'm using sails 0.10.5 and node 0.10.33.

In the sails.js documentation there's a page about tests http://sailsjs.org/#/documentation/concepts/Testing , but it doesn't tell me how to actually run them.

I've set up the directories according to that documentation, added a test called test/unit/controllers/RoomController.test.js and now I'd like it to run.

There's no 'sails test' command or anything similar. I also didn't find any signs on how to add a task so tests are always run before a 'sails lift'.

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

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

鉴于他们没有给出特殊说明并且他们使用 Mocha,我希望当您在test目录中时从命令行运行mocha会起作用。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM