How to run jasmine-node tests with RequireJS
Asked Answered
S

3

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 ./
Skiver answered 28/11, 2012 at 8:43 Comment(3)
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?Alboran
Outside of requirejs block will print "hello" first. Second will be "hello" inside requirejs block, "hello" from it block doesnt print.Skiver
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.Alboran
P
-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.

Pouter answered 10/1, 2013 at 12:51 Comment(0)
A
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.

Alleyne answered 12/2, 2013 at 17:4 Comment(0)
A
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.

Alboran answered 7/4, 2020 at 6:49 Comment(0)
P
-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.

Pouter answered 10/1, 2013 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.