13

Mocha (test framework for Node.js) uses make.

For the life of me I can't find a compatible make.exe for Windows.

Everything works fine on my Mac.

I've tried using VS's nmake.exe and a make.exe I found that was ported from Unix. But they are all incompatible.

It can't just be me

Here's the makefile:

test:
    @./node_modules/.bin/mocha -u tdd -R spec

.PHONY: test

make barfs on the . in PHONY, and even if I remove it, it never runs the mocha command (or at least there's no output).

Running ./node_modules/.bin/mocha -u -tdd -R spec directly gives me my test report:

first suite -
  ? ten should always be equal to 9+1
  ? zero is less all positive numbers
  ? There is no i in team

 ? 3 tests complete (8ms)

EDIT 3/25/12

  • In the end, the easiest way to deal with this is to use Cygwin and ensure that the developer packages for Cygwin are installed. In PowerShell I did Set-Alias make "c:\dev\utils\cygwin\bin\make.exe" and now make test works on standard Mocha Makefiles.
tig
  • 3,424
  • 3
  • 32
  • 65
  • 1
    I suspect that the ``make.exe`` ported from Unix is what you needed, but the ``makefile`` assumes a Unix environment and calls other Unix-style executables you don't have installed. –  Mar 20 '12 at 00:07
  • Not using cygwin, but have it installed. There's no make.exe in my intall of cygwin either (that seems really weird). – tig Mar 20 '12 at 00:10
  • Or MinGW MSYS utilities? – Maarten Bodewes Mar 20 '12 at 00:13
  • @tig depends on how much you installed from cygwin. Cygwin contains packages and may not contain any packages for development by default. – Maarten Bodewes Mar 20 '12 at 00:14
  • @DavidEllis: Nope. It's barfing on the period of .PHONY which is a built-in in make. I think I need a newer make than the one I found here: http://unxutils.sourceforge.net/ – tig Mar 20 '12 at 00:18
  • @tig: I didn't even know about that port. I've always used this one: http://gnuwin32.sourceforge.net/packages/make.htm While ``make`` was last updated in 2006, the last update to the gnuwin32 set of packages was in 2010, so it's not that old (and the packages are pretty stable, so it's not expected to change that often, either). –  Mar 20 '12 at 00:29
  • @DavidEllis The gnuwin32 seems to work better. But no mocha results are printing. Progress... – tig Mar 20 '12 at 00:38
  • Looking at the [Makefile](https://github.com/visionmedia/mocha/blob/master/Makefile), there are several unix-isms, like ``cp``, ``mkdir``, and ``cat``. You'll need those installed, too. –  Mar 20 '12 at 00:47
  • Oh, and @DavidEllis - i'm not trying to BUILD Mocha, just use it from a Makefile (as the docs tell you to: http://visionmedia.github.com/mocha/) – tig Mar 20 '12 at 02:26
  • @tig A little late, but try installing Gow (https://github.com/bmatzelle/gow). It's a lightweight ~10 MBs install of uninvasive GNU utilities that includes Make and many Unix-style tools. Works beautifully for me, and doesn't involve a massive emulated environment like Cygwin, etc. –  Jan 16 '16 at 17:01

4 Answers4

10

Heh I feel you ;) I'm part of a team busy building a new startup using node. Two of our dev's are on on OSX, one is on Linux. I am on Windows.

I downloaded and use GNU's "Make for Windows" and can now quite happily make our installation & test suites.

Also, I STRONGLY encourage you to use PowerShell - it has a bunch of commands aliased to UNIX-friendly commands (e.g. Get-ChildItem -> ls). This allows several of our scripts to work on UNIX or Windows without change.

So, to your issue:

Try replacing your makefile above with the following:

# Detect if we're running Windows
ifdef SystemRoot
    # If so, set the file & folder deletion commands:
    FixPath = $(subst /,\,$1)
else
    # Otherwise, assume we're running *N*X:
    FixPath = $1
endif

NODE_MODULES := ./node_modules/.bin/

test: 
    $(call FixPath, NODE_MODULES)mocha -u tdd -R spec

.PHONY: test

Note: with Makefiles, tasks within targets must be indented with tab characters, not spaces! Go figure!!

I stole the FixPath routine from this post (thanks Paul :)). It replaces a string's / with \ if run on Windows.

One of the problems with make on Windows is that it shells out to NT's command shell (via CreateProcess) in order to execute each task. This means that any NX-isms that Powershell would otherwise handle (e.g. ls, cat, etc.) won't work when executing makefiles. Thus, its advisable to replace inline commands with overridable aliases that you can set to one command for NT and another for NX.

Perhaps I'll get around to forking Gnu Make and seeing if I can get it to shell out to Powershell when executing commands instead of the NT command line. That would also eliminate the need for FixPaths above ;)

Ping me if you get stuck ;)

Community
  • 1
  • 1
Rich Turner
  • 10,800
  • 1
  • 51
  • 68
  • Thanks, but this is the make.exe I'm currently trying. See the makefile above. Can you get it to work? Are you using cmd.exe, Powershell, cygwin? I have tried all 3... – tig Mar 20 '12 at 02:16
  • On the PS point - yes, I've moved to PS finally. I just tried to get this working with cmd and cygwin. – tig Mar 20 '12 at 02:23
  • Updated answer to provide actual answer ;) – Rich Turner Mar 20 '12 at 03:51
  • Glad to know the root cause (/ vs. \). However, this is still not working. $(FixPath, NODE_MODULES) is returning blank (I've verified that $(NODE_MODULES) returns '/node_modules.bin/'). It's the FixPath thing that isn't working for me. – tig Mar 20 '12 at 05:26
  • This works: test: $(call FixPath, ./node_modules/.bin/)mocha -u tdd -R spec (I have to use the "call" keyword, and the variable NODE_MODULES dind't work) – tig Mar 20 '12 at 05:28
  • Argh! Thanks for the note :) I mis-pasted the script omitting the call statement! Thanks for the tip-off. I've corrected my answer. – Rich Turner Mar 20 '12 at 18:37
5

You can use commander to implement your own version in node.js! I use that for my projects, despite that I'm on mac. It'll work anywhere, where node is installed.

The following script assumes that your cli scripts lie in /path/to/your/app/cli.

node cli/test.js -u # runs unit tests in /path/to/your/app/tests/unit

node cli/test.js -f # runs functional tests in /path/to/your/app/tests/functional

test.js

/*
 * CLI for execution of the mocha test suite.
 *
 * test.js --help for instructions.
 */

var program = require('commander');

program
    .version('1.0.0')
    .option('-u, --unit', 'Run unit tests.')
    .option('-f, --functional', 'Run functional tests.')
    .on('--help', function(){
        console.log('Executes the mocha testsuite.');
        console.log('');
    })
   .parse(process.argv)
;

if(!program.unit && !program.functional) {
    console.log();
    console.log('Specify if you want to run unit tests (-u) or functional tests (-f).');
    console.log('Run help (-h) for detailed instructions.');
    console.log();
}

if(program.unit) {
     require('child_process').exec(__dirname + '/../node_modules/.bin/mocha -u tdd -R spec --recursive -c ' + __dirname + '/../tests/unit', standardOutput);
}

 if(program.functional) {
require('child_process').exec(__dirname + '/../node_modules/.bin/mocha -u bdd -R spec --recursive -c ' + __dirname + '/../tests/functional', standardOutput);
}

/**
 * Standard output.
 *
 * @method standardOutput
 * @param {Object} error
 * @param {String} stdout the cli standard output
 * @param {String} stderr output of errors
 */
function standardOutput(error, stdout, stderr) {
    console.log(stdout);
    console.log(stderr);
}
shredding
  • 5,374
  • 3
  • 46
  • 77
4

For those that don't want to use make, you can also install mocha globally so that it works in the regular windows command line:

npm install -g mocha

Then run your tests with mocha path\to\test.js

SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57
3

If you are using cygwin's make (cygwin/bin/make.exe), be aware that it expects tab separators in makefiles. If your editor converts tabs to spaces, you'll end up getting errors like

makefile:23: * missing separator. Stop.

I've read that GNU's Make for Windows, mentioned above, is ok with spaces or tabs, but I haven't tried it yet.

See How To Use Mocha for Node Testing in Windows for more specifics.

J Burnett
  • 2,410
  • 1
  • 13
  • 9