Webstorm IDE and mocha tests using global.expect - javascript

I currently run the following command to run my mocha tests:
./node_modules/.bin/mocha --require ./my.js
and in the js file, I am using sinon and expect for SpyOn...
global.expect = require('must');
global.sinon = require('sinon');
How do I configure the Mocha task runner to use this external file.
Using --require ./my.js I still see ReferenceError: spyOn is not defined
Any thoughts on this?

Required modules are resolved relative to the working directory specified in "Working directory" field of Mocha Run/Debug configuration.
Here is the configuration that works for me:
working directory: C:\WebstormProjects\mocha_sinon
Mocha package: C:\WebstormProjects\mocha_sinon\node_modules\mocha
Extra options: --require ./with_req/util.js
Test directory: C:\WebstormProjects\mocha_sinon\with_req
My spec file:
var EventEmitter = require('events').EventEmitter;
var should = require('should');
describe('EventEmitter', function(){
describe('#emit()', function(){
it('should invoke the callback', function(){
var spy = sinon.spy()
, emitter = new EventEmitter;
emitter.on('foo', spy);
emitter.emit('foo');
spy.called.should.equal.true;
})
})
})
util.js;
global.sinon = require('sinon');

Related

Mocking execSync in Jest

How do I mock out executing a child process in Jest
const execSync = require('child_process').execSync;
//...
expect(execSync)
.toHaveBeenCalledWith('npm install');
But not actually have it run the npm install during the test.
Can just do something like the following:
jest.mock("child_process", () => {
return {
execSync: () => "This is a test message"
};
});
Where the return value can be a number, string, object or whatever. It just allows you to override the actual implementation of execsync.
You can use lib mock with __mocks__ folder that will hold child_process folder which will be loaded by jest automatically.
Just create a file
// __mocks__/child_process/index.js
module.exports = {
execSync: jest.fn()
};
that will export a mock implementation of child_process.

How do I write unit tests for a single JS file?

I have a single js file with a function in in. I want to write unit tests for the function and deliver the tests and file to someone. It needs to be standalone.
Here is my project:
src: myFunction.js
tests: empty for now
myFunction.js:
function HelloWord() {
return 'Hello';
}
It would be great to have a test file like this:
import { func } from './myFunction.js';
describe('tests', function () {
it('returns hello', function () {
expect(func()).toEqual('Hello');
});
});
I don't know which unit test framework would be the easiest and fastest to accomplish what I need to do. The user needs to get my directory and just run the tests from the command line.
Using Mocha, a really fast set up would be:
1) Install mocha to package.json:
npm install --save-dev mocha
2)Write down the test. Let it be test.js under /tests/ , for example:
var myFunc = require('./myFunction');
var assert = require('assert');
describe('the function' , function(){
it('works' , function(){
assert.equal( myFunc() , 'hello');
});
});
3) Set up the package.json test command:
{
...
"scripts": {
"test": "node_modules/mocha/bin/mocha tests/test.js"
}
}
4) Call tests by npm test.

Testing reactjs components with mochajs

I'm trying to run a simple test with mochajs:
var assert = require('assert'),
ActionCreators = require('../src/components/Dashboard/ActionCreators.js');
describe('', function () {
it('', function () {
assert.equal(1, 1);
});
});
But it throws errors that the ActionCreators file contain import keywords. It does, becuase I run my code with webpack, and it goes through babel.
I've been trying to run this test with mocha-webpack, but it's the same. How should I do this?
Use mocha command line option --compilers js:babel-core/register. Of course you have to have babel-core npm package installed.

Mocha not recognizing CoffeeScript/JS test when adding programatically

I am trying to add mocha to an existing project. I have the following test just for putting things together...
assert = require('assert');
describe 'Array', ->
describe '#indexOf()', ->
it 'should return -1 when the value is not present', ->
assert.equal(-1, [1,2,3].indexOf(4));
Options...
--compilers coffee:coffee-script/register
Then I run mocha --opts ./mocha.opts src/test/coffee/test/test.coffee and I see
1 passing (6ms)
Now I try to create a runner file to handle
globFlat = require('glob-flat');
Mocha = require('mocha');
mocha = new Mocha();
files = globFlat.sync([
'src/test/coffee/test/test.coffee'
]);
mocha.addFile file for file in files
mocha.run();
And run mocha --opts ./mocha.opts src/test/mocha/mocha-runner.coffee I get
0 passing (0ms)
So why is it not finding the test?
Update
I have also converted everything over to JS to ensure it wasn't an issue with CS and I am getting the same thing...
require('coffee-script');
var globFlat = require('glob-flat');
var Mocha = require('mocha');
var mocha = new Mocha();
mocha.addFile('src/test/coffee/test/test.js');
runner = mocha.run();
console.log("Done");
It runs like this...
mocha src/test/mocha/mocha-runner.js
Done
0 passing (0ms)
Update 2
Ok so it appears I should be using node and not mocha for running it. This presents a problem as the .js version works but the .coffee version throws an error...
(function (exports, require, module, __filename, __dirname) { require 'coffee-script';
^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
Because node cannot recognize the coffeescript
I am going to post this up here as an answer, although I don't like it...
mocha-wrapper.js
require('coffee-script/register');
require('./mocha-runner.coffee');
mocha-runner.coffee
globFlat = require 'glob-flat';
Mocha = require 'mocha';
mocha = new Mocha();
files = globFlat.sync([
'src/test/coffee/test/test.coffee'
]);
mocha.addFile file for file in files
mocha.run();
I would like to avoid the multiple files but for now it looks like I am stuck :-/. That is why I am going to wait to accept my answer.

How to execute jasmine tests for node modules from grunt

I want to run some Jasmine 2.x tests for node.js modules in a Grunt build. My setup looks like this:
src/foo.js
exports.bar = 23;
spec/foo.spec.js
var foo = require("../src/foo.js");
define("foo", function() {
it("exports bar as 23", function() {
expect(foo.bar).toBe(23);
});
});
With grunt-contrib-jasmine the node module system is not available and I get
>> ReferenceError: Can't find variable: require at
>> spec/foo.spec.js:1
There is grunt-jasmine-node, but it depends on jasmine-node which is unmaintained and includes Jasmine 1.3.1, so this is not an option.
Jasmine supports node.js out of the box, by including a file jasmine.json in the spec directory, I can run the tests with the jasmine cli. Is there any clean way to run the same tests from grunt as well?
You could use grunt-exec, which just executes the value as if typed on the command line:
module.exports = function (grunt) {
grunt.initConfig({
exec: {
jasmine: "jasmine"
},
env: {
test: {
NODE_ENV: "test"
}
}
});
grunt.loadNpmTasks("grunt-exec");
grunt.loadNpmTasks("grunt-env");
grunt.registerTask("test", [
"env:test",
"exec:jasmine"
]);
};
This will allow you to keep jasmine up to date as well as use it with other grunt tasks.

Categories

Resources