I'm trying to make some tests with Mocha for my Sails application. As described here.
--require test/bootstrap.test.js
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.
console.log("Sails Loaded");
done(err, sails);
});
});
after(function(done) {
// here you can clear fixtures, etc.
sails.lower(done);
});
/* global describe, User */
describe.only('UserModel', function() {
describe('#find()', function() {
it('should check find function', function(done) {
User.find()
.then(function(results) {
console.log(results);
done();
})
.catch(done);
});
});
});
When I'm running tests, I see an error:
...
test\bootstrap.test.js:4
before(function(done) {
^
ReferenceError: before is not defined
...
You don't declare suites, tests or hooks on --required files.
Instead, you could require(...) helpers in your spec/test files, and execute methods that define those hooks.
require('./setup.js')()
describe(..., function () {
it(...)
})
To clarify, --required files are require(...)d _once_ by the CLI even before a Mocha instance is created, so they are useful for one time setup of globals and stuff.
e.g, you can execute require('chai').should() in your --required file to avoid doing so in each of your test files.
Thans for explanation and for solution @dasilvacontin!
I solved it by simply running test/bootstrap.test.js through test/mocha.opts:
test/bootstrap.test.js
@elennaro what do you mean by:
I solved it by simply running test/bootstrap.test.js through test/mocha.opts:
you just put that line as the first line in your test/mocha.opts ?
Yes @aventurella
Here's my mocha opts:
--full-trace
--timeout 20000
test/bootstrap.test.js
I was also getting below mentioned error.
global.window.beforeEach = beforeEach;
^
ReferenceError: beforeEach is not defined
I changed --require to --required and now its working fine.
My application mocha.opt
--compilers js:babel-register,jsx:babel-register
--recursive
--colors
--reporter list
--required ./test/setup.js
And setup.js
delete require.cache[require.resolve('angular')];
delete require.cache[require.resolve('angular/angular')];
delete require.cache[require.resolve('angular-mocks')];
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import 'sinon-as-promised';
import {jsdom} from 'jsdom'
// example of using the should DSL without having to import it anywhere
// see chai docs
global.should = chai.should();
chai.use(chaiAsPromised);
chai.use(sinonChai);
global.sinon = sinon;
// mocha re-executes every time so we need to create this lazily
global.document = jsdom('<html><head></head><body></body></html>');
global.window = global.document.defaultView;
global.navigator = window.navigator = {};
global.Node = window.Node;
global.window.mocha = {};
global.window.beforeEach = beforeEach;
global.window.afterEach = afterEach;
require('angular/angular');
require('angular-mocks/angular-mocks');
global.angular = window.angular;
global.inject = global.angular.mock.inject;
global.ngModule = global.angular.mock.module;
Hi guys, I just find using async call in the required file will solve this problem:
setTimeout(() => {
beforeEach(async () => {
// ***
});
afterEach(async () => {
// ***
});
});
for eslint, a .eslintrc file include below config should be placed inside the test folder:
{
"env": {
"mocha": true
}
}
I got the same error, but the reason was that I was using tdd, which uses suiteSetup instead of before.
Most helpful comment
for
eslint, a.eslintrcfile include below config should be placed inside the test folder: