Jasmine js: Add source method for test execution

≯℡__Kan透↙ 提交于 2019-12-05 11:54:16

A solution is to use Grunt.

Create a GruntFile.js containing:

module.exports = function (grunt) {
  grunt.initConfig({
      pkg: grunt.file.readJSON('package.json'),
      jasmine: {
        src: ['src/**/*.js'],
        options: {
          specs: ['spec/**/*Spec.js'],
          vendor: []
        }
      }
    });
  grunt.loadNpmTasks('grunt-contrib-jasmine');
};

Update package.json with grunt, grunt-cli and grunt-contrib-jasmine dependencies

{
  "name": "jasmineTest",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "BSD-2-Clause",
  "dependencies": {
    "jasmine": "~2.1.0",
    "grunt": "~0.4.5",
    "grunt-cli": "~0.1.13",
    "grunt-contrib-jasmine": "~0.8.1"
  }
}

Update npm dependencies:

npm update

And relaunch test using grunt and not directly jasmine:

./node_modules/grunt-cli/bin/grunt jasmine

And you got:

Running "jasmine:src" (jasmine) task
Testing jasmine specs via PhantomJS

 Test
   - it...
log: Spec 'Test it' has no expectations.
   ✓ it

1 spec in 0.008s.
>> 0 failures

Done, without errors.

According to Jasmine staff :

You don't need to specify your source files in the config - just require them in from your spec files.

https://github.com/jasmine/jasmine-npm/issues/49

But then, you do need to use export. This is not a Jasmine issue but vanilla Javascript. You want to call a method from another file so you need to export and require it. Why don't you want to ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!