Sails js tests

I'm trying to run sail tests (using mocha and istanbul)

at startup

grunt test 

I get errors

  1) "before all" hook 2) "after all" hook 0 passing (5s) 2 failing 1) "before all" hook: Error: timeout of 2000ms exceeded at null.<anonymous> (/vagrant/node_modules/mocha/lib/runnable.js:157:19) at Timer.listOnTimeout [as ontimeout] (timers.js:112:15) 2) "after all" hook: ReferenceError: sails is not defined 

the installation does not seem to find my sails ... but does

 which sails 

I get

 /usr/local/node/node-default/bin/sails 

and works sails lift works great

Here is the mocha test file in my project

 //boostrap.test.js var Sails = require('sails'); before(function(done) { Sails.lift({ // configuration for testing purposes }, function(err, sails) { 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); }); 
+5
source share
4 answers

Firstly, you have a typo, you have:

 var Sails = require('sails'); 

But you are trying to use

 sails.lower(done); 

Instead

 sails.lower(done); 

Then you need to configure the "migrate" setting for the entire project ( http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html?q=migrate ) You just need to edit the config / models.js file. You can simply uncomment the following line:

 //migrate: 'alter' 

Enjoy :)

+4
source

I have not seen the actual code of your test case, so I will just list a few possibilities here.

If you run your test asynchronously, I think you need to set the timeout attribute in mocha a few more. I wrote three very simple test cases, but I always get the same problem that you described. I tried 10,000 ms, 20,000 ms and 30,000 ms. When I increase it to 90,000 ms, all test cases are passed. Therefore, I think that sails really need some time to rise before the start of the test.

Another thought is that you set the "migrate" attribute in the environment configuration file? If not, the sail control team will wait for your entry to select drop, alter, or safe before continuing, which will also cause a timeout problem in the test.

+2
source

It doesn't seem like I know your problem, but it can help you find out if I write some snipers of my operational tests in sails. I also include the package.json file so that you know my version of each module.

Here are the relevant modules in package.json:

rootproject / test / mocha.opts , maybe what you need

 --timeout 5000 

rootproject / package.json

 { ... "dependencies": { ... "sails": "0.9.7", "sails-disk": "~0.9.0", "sails-memory": "^0.9.1", "sails-mongo": "^0.9.7", ... }, "devDependencies": { "mocha": "^1.20.1", "barrels": "^0.0.3", "supervisor": "^0.6.0" }, "scripts": { "start": "node app.js", "debug": "node debug app.js", "test": "PORT=9999 NODE_ENV=test mocha -R spec -b --recursive" }, "main": "app.js", ... } 

I also added another model adapter that will be used in tests in rootproject / config / adapters.js

 test: { module : 'sails-memory' }, 

rootproject / test / index.js

 var assert = require('assert'); var Sails = require('sails'); var barrels = require('barrels'); var fixtures; var userTest = require('./controllers/User.js'); //... other test controllers ... //in case you need different simulations per controller you could add a custom Response in your test controller and use them instead var defaultCustomRequest = function(urlParams, bodyParams/*whatever else u need*/) { //simulates the sails request //create an object here, based on how u use the req object in your sails controllers //.eg return { params: urlParams, body: bodyParams }; } //in case you need different simulations per controller or per method you could add multiple custom Responses in your test controller and use them instead var defaultCustomResponse = function(expectedCode, dataExpecting/*whatever else u need*/) { //simulates the sails res which I was using like this: res.status(httpCode).json({somedata}) //use the assert function here to validate //.eg status: function (responseCode){ assert(expectedCode === responseCode, 'Expected status is ' + expectedCode + ' but ' + responseCode + ' was returned.'); return this; }, json: function (responseData){ //assert your responseData with your expectedData return this; } return this; }, } before(function (done) { // Lift Sails with test database Sails.lift({ log: { level: 'error' }, adapters: { default: 'test' } }, function(err, sails) { if (err) return done(err); // Load fixtures barrels.populate(function(err) { done(err, sails); }); // Save original objects in `fixtures` variable fixtures = barrels.objects; }); }); // Global after hook after(function (done) { //console.log('fixtures loaded: ' + JSON.stringify(fixtures)); sails.lower(done); }); describe('User', function() { userTest.run(fixtures, customRequest, customRespose); }); //or describe('User', function() { userTest.run(fixtures); }); //... other test controllers ... 

rootproject / test / Controllers / user.js

 var assert = require('assert'); var UserController = require('../../api/controllers/UserController.js'); module.exports = { run: function(fixtures, customReq, customRes) { describe('create', function(customReq, customRes) { it ('should create a few user entries', function() { if (!customReq) customReq = {/*custom request for user create*/}; if (!customRes) customRes = {/*custom response for user create*/}; //call your controllers for testing here //.eg UserController.create( new req({}, {email: ' someemail@gmail.com ', password: 'password'}) ,new res(201, {email: ' someemail@gmail.com ', password: null}); UserController.create( new req({}, {email: ' someemail@gmail.com ', password: 'password'}) ,new res(400, {error: true}); ); }); }); //... more guns } }; 

As you can see on package.json , I used 0.9.7 sails, so additional changes for versions 0.10.x..eg may be required instead of config / adapters.js config / models.js and config / connections.js are used . p>

+1
source

The accepted answer is correct for one of the errors.

For timeout error just add

 this.timeout(5000); 

in the line after

 before(function(done) { 
0
source

Source: https://habr.com/ru/post/1204425/


All Articles