简体   繁体   中英

Using module.exports in jasmine specs

I would like to use module.exports in tested js and include it to specification script using require().

cat.js - file with object I would like to export

var cat = {
    name: "filemon",

    meow: function () {
        return "miau";
    },
    walk: function () {
        return "siup siup siup";
    }
}
module.exports = cat

catTest.js - spec for cat

var cat = require('../app/cat')
describe("Cat test", function () {
    it("cat should meow with miau", function () {
        expect(cat.meow()).toBe("miau")
    })
})

When I run the tests using jasmine CLI(downloaded with npm) tests are passed:

marcins-iMac:untitled marcin$ jasmine
Started
..

2 specs, 0 failures
Finished in 0.004 seconds

But when I try to run tests with karma runner module is undefined:

marcins-iMac:untitled marcin$ karma start
29 04 2016 23:52:29.238:WARN [karma]: No captured browser, open http://localhost:9876/
29 04 2016 23:52:29.248:WARN [karma]: Port 9876 in use
29 04 2016 23:52:29.248:INFO [karma]: Karma v0.13.22 server started at http://localhost:9877/
29 04 2016 23:52:29.252:INFO [launcher]: Starting browser Chrome
29 04 2016 23:52:30.112:INFO [Chrome 50.0.2661 (Mac OS X 10.11.2)]: Connected on socket /#MJ-R1hm8uQEvSJq9AAAA with id 29613373
Chrome 50.0.2661 (Mac OS X 10.11.2) ERROR
  Uncaught ReferenceError: module is not defined
  at /Users/marcin/WebstormProjects/untitled/app/cat.js:9

Few ideas I got and does not help:

  • adding require.js to "files" in karma.config.js
  • add tested object to window scope(it's ugly and doesnt work with CLI)

If problem may be caused by configuration, project on github: https://github.com/marcinmierzejewski1024/jasmine-karma-playground

So this is what i did to make the test cases work.

change files section in karma.config.js to the below.

files: [
  '/app/cat.js',
  '/app/dog.js',
  'spec/catTest.js',
  'spec/dogTest.js'
],

changed frameworks section in karma.comfig.js to below

frameworks: ['jasmine','browserify'],

changed preprocessor section in karma.config.js to below.

preprocessors: {
  'spec/catTest.js': ["coverage",'browserify'],
  'spec/dogTest.js': ["coverage",'browserify']
},

your final package.json shuld like below.

{
  "name": "untitled",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jasmine",
    "dev-mode": "karma start karma.conf.js",
    "test-report": "TODO:",
    "test-coverage": "TODO:"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "browserify": "^13.0.0",
    "jasmine": "^2.4.1",
    "jasmine-core": "^2.4.1",
    "karma": "^0.13.22",
    "karma-browserify": "^5.0.4",
    "karma-chrome-launcher": "^0.2.3",
    "karma-jasmine": "^0.3.8",
    "karma-safari-launcher": "^0.1.1",
    "require": "^2.4.20",
    "requirejs": "^2.2.0",
    "watchify": "^3.7.0"
  }
}

PS browserify is needed to run your test cases in browser.

The file section might not be the right way to declare your files, but it will get you started and you can play around it.

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