简体   繁体   中英

How to run jasmine-node tests with RequireJS

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 ./

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM