6

How can I properly run jasmine tests using jasmine-node and RequireJS?

I already tried something like this, but doesnt work (CoffeeScript):

requirejs = require 'requirejs'
requirejs.config { baseUrl: __dirname + '/../' }

requirejs ['MyClasses', 'FooClass'], (MyClasses, FooClass) ->

  describe "someProp", ->
    it "should be true", ->
      expect(MyClasses.FooClass.someProp).toEqual true

Finished in 0 seconds 0 tests, 0 assertions, 0 failures

My goal is to write modular classes using RequireJS, CoffeeScript and classes must be testable with jasmine-node (CI server).

How can I do that please? Thank you!

EDIT:

I executing tests with command (at directory with tests):

jasmine-node ./
Martin
  • 490
  • 5
  • 16
  • How are you executing jasmine? If you put a `console.log 'hello'` inside the `requirejs` block, does it print? Inside the `it`? How about outside the `requirejs` block? – Jonathan Tran Nov 28 '12 at 18:03
  • `Outside` of requirejs block will print "hello" first. Second will be "hello" `inside` requirejs block, "hello" from `it` block doesnt print. – Martin Nov 28 '12 at 22:45
  • 1
    Have you tried `jasmine-node --coffee --verbose ./`? Also, what is your spec file named? I think it has to have "spec" as part of the name, as in `Foo.spec.coffee`. – Jonathan Tran Nov 30 '12 at 20:47

3 Answers3

0

Jonathan Tran is right, it's the spec in the file name for me.

I have this:

"scripts": {
   "install": "cake install",
   "test": "node_modules/jasmine-node/bin/jasmine-node --verbose --coffee --runWithRequireJs --captureExceptions spec"
},

in my package.json and I installed jasmine-node from inside the project npm install jasmine-node

Minimal test file called RingBuffer.spec.coffee

require ["disrasher"], (mod) ->

  describe "A test", ->
    it "should fail", ->
      expect(1).toEqual 0

It doesn't actually work at the moment because I haven't got the project hooked up with require properly I don't think. I'll post back here when it does.

Ben Ford
  • 2,087
  • 21
  • 26
0

If anyone is running into this, much has changed since this question was asked. The first thing to check is still that you're naming your files like thing.spec.coffee.

But if you're running the tests and still seeing the output "0 tests", you need to make a JavaScript file with your requirejs config. This must be JavaScript, not CoffeeScript.

// requirejs-setup.js
requirejs = require('requirejs');
requirejs.config({ baseUrl: __dirname + '/../' });

Then tell jasmine to use this setup file:

jasmine-node --coffee --requireJsSetup requirejs-setup.js ./

One nice thing about this is that you don't need to include the requirejs config in every spec file.

I've tested this on node v12.16, jasmine-node v3.0.0, and requirejs v2.3.6.

Jonathan Tran
  • 15,214
  • 9
  • 60
  • 67
-2

It seems that jasmine-node and require.js are completely incompatible. That said, it is possible to run jasmine tests on require.js modules in node using a bit of extra code. Take a look at https://github.com/geddski/amd-testing to see how.

joran
  • 169,992
  • 32
  • 429
  • 468
user659104
  • 14
  • 2