Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use book in unit testing using mocha #1

Open
salmankhann opened this issue Apr 25, 2015 · 6 comments
Open

Unable to use book in unit testing using mocha #1

salmankhann opened this issue Apr 25, 2015 · 6 comments

Comments

@salmankhann
Copy link

Hello, Great work! The hook works perfectly when I do a sails lift on the console, but while running unit tests through mocha, it fails with a syntax error,

"before all" hook:

Uncaught /Users/salmanhasratkhan/Desktop/es6_trial/api/controllers/TestController.js:4
        console.log(__.ifNot(s => s == 2, 2));
                               ^^
Unexpected token =>

this is the way I lift sails in mocha

console.log('bootstrap before');
    Sails.lift({
        //configuration for testing purposes
        hooks: {
            // Load the hook
            "babel": require('../../../'),
            // Skip grunt
            "grunt": false
        },
        log: {
            level: "error"
        }

    });

Any ideas what is preventing it from working correctly? Thanks

@salmankhann
Copy link
Author

Okay so I got sails to lift correctly and any ES6 syntax within the project works perfectly using the line
"babel": require('../../../node_modules/sails-hook-babel/') within the call to Sails.lift, but now I'm faced with another issue where any ES6 syntax within the unit test file does not work. How can I fix this?

@Globegitter
Copy link
Member

Glad you could fix the first error - do you have a test repo so I can look at how your tests are set up etc?

@hellowin
Copy link
Contributor

Same problem here, any thought how to enable babel in test environment?

I think it's because our test case are loaded before sails has not been loaded. Even if sails already has babel, it will affect everything inside the sails object, not test case that outside sails object.

@hellowin
Copy link
Contributor

Alright, what I expected is true: use this as bootstrap when doing testing. Change it to something that meet our needs (I don't pretty understand about babel config actually).

bootstrap.test.js

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

var options = {
  loose     : "all",
  stage     : 2,
  ignore    : null,
  only      : null,
  extensions: null
};

global.babel   = require("sails-hook-babel/node_modules/babel/register")(options);

before(function (done) {
  Sails.lift({
    //put your test only config here
  }, 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);
});

and don't forget to run it, example in package.json, I put all my unit test at test/unit/ folder

"scripts": {
    "test": "_mocha test/bootstrap.test.js test/unit/**/*.test.js --timeout 60000"
  },

@austinmao
Copy link

You need the proper polyfills. Here are instructions for Babel 6, which is not supported by sails-hook-babel currently: See issue #10

@dangreen
Copy link
Contributor

I try to run some tests with mocha, and only problem on my way was babel's presets and plugins: Babel could not get configs from package.json, I had to write dependencies in config/babel.js.
packakge.json:

{
  ...,
  "babel": {
    "presets": [
      "es2015",
      "stage-0",
      "react"
    ],
    "plugins": [
      "transform-decorators-legacy",
      "add-module-exports"
    ]
  }
}

config/babel.js:

module.exports.babel = {
    polyfill: true,
    ignore:   /\/node_modules\/(?!lib\/)/,
    "presets": [
      "es2015",
      "stage-0",
      "react"
    ],
    "plugins": [
      "transform-decorators-legacy",
      "add-module-exports"
    ]
};

mocha.opts:

--timeout 50s
--require should
--compilers js:babel-register

bootstrap.test.js:

import Sails from 'sails';
import RC    from 'rc';

before((done) => {

    Sails.lift(RC('sails'), (err, server) => {

        if (err) {
            return done(err);
        }

        // here you can load fixtures, etc.
        done(err, Sails);
    });
});

after((done) => {
    // here you can clear fixtures, etc.
    Sails.lower(done);
});

it('should lift sails', (done) => {
    done();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants