2015-07-21 88 views
4

我想利用grunt &巴贝尔加载我的es6源作为给定的测试依赖项。所以我一直在运行的实际src和通过browserify编译应用程序就好了:试图设置与摩卡,巴贝尔和es6模块测试

module.exports = function (grunt) { 

    // Import dependencies 
    grunt.loadNpmTasks('grunt-contrib-watch'); 
    grunt.loadNpmTasks('grunt-contrib-jshint'); 
    grunt.loadNpmTasks('grunt-browserify'); 

    grunt.initConfig({ 
    browserify: { 
     dist: { 
     files: { 
      'www/js/bundle.js': ['src/app.js'], 
     }, 
     options: { 
      transform: [['babelify', { optional: ['runtime'] }]], 
      browserifyOptions: { 
      debug: true 
      } 
     } 
     } 
    }, 
    jshint : { 
     options : { 
     jshintrc : ".jshintrc", 
     }, 

     dist: { 
     files: { 
      src: ["src/**/*.js"] 
     } 
     } 
    }, 
    watch: { 
     scripts: { 
     files: ['src/**/*.js'], 
     tasks: ['jshint', 'browserify'], 
     options: { 
      atBegin: true, 
      spawn: true 
     }, 
     }, 
    } 
    }); 

    grunt.registerTask("default", ['watch']); 

}; 

它编译一个bundle.js文件,其中包括我在我的index.html文件。大!

所以我想从测试中做的是导入我测试的文件。所以我有一个名为InteractionStore的简单存储对象,位于src/stores/interaction_store.js。然后我创建了一个spec文件:test/stores/interaction_store_spec.js

import expect from "expect.js"; 
import InteractionStore from '../../../src/stores/interaction_store.js'; 

describe("InteractionStore",() => { 
    beforeEach(() => { 
    InteractionStore.data = []; 
    }); 
    describe("#start()",() => { 
    it ("should apped multiple", function() { 
     InteractionStore.start(); 
     InteractionStore.start(); 
     InteractionStore.start(); 
     expect(InteractionStore.data.length).toEqual(3); 
    }); 
    }); 
}); 

因此我直接导入商店。我已经加入到繁重的文件中的几节的测试过程:

module.exports = function (grunt) { 

    // Import dependencies 
    grunt.loadNpmTasks('grunt-contrib-watch'); 
    grunt.loadNpmTasks('grunt-contrib-clean'); 
    grunt.loadNpmTasks('grunt-contrib-jshint'); 
    grunt.loadNpmTasks('grunt-browserify'); 
    grunt.loadNpmTasks('grunt-contrib-sass'); 
    grunt.loadNpmTasks('grunt-mocha-test'); 
    grunt.loadNpmTasks('grunt-babel'); 

    grunt.initConfig({ 
    babel: { 
     options: { 
     sourceMap: true, 
     modules: "common" 
     }, 
     test: { 
     files: [{ 
      expand: true, 
      cwd: 'test', 
      src: ['**/*.js'], 
      dest: 'test/compiled_specs', 
      ext:'.js' 
     }] 
     } 
    }, 
    browserify: { 
     dist: { 
     files: { 
      'www/js/bundle.js': ['src/app.js'], 
     }, 
     options: { 
      transform: [['babelify', { optional: ['runtime'] }]], 
      browserifyOptions: { 
      debug: true 
      } 
     } 
     } 
    }, 
    clean: ["test/compiled_specs"], 
    jshint : { 
     options : { 
     jshintrc : ".jshintrc", 
     }, 

     dist: { 
     files: { 
      src: ["src/**/*.js"] 
     } 
     } 
    }, 
    watch: { 
     scripts: { 
     files: ['src/**/*.js'], 
     tasks: ['jshint', 'browserify:dist'], 
     options: { 
      atBegin: true, 
      spawn: true 
     }, 
     }, 
    }, 

    mochaTest: { 
     test: { 
     src: ['test/compiled_specs/**/*_spec.js'] 
     } 
    } 
    }); 

    grunt.registerTask("default", ['watch']); 
    grunt.registerTask("test", ['clean', 'babel', 'mochaTest']); 

}; 

通天编译测试没问题,但后来当我运行它,它加载在src文件夹是在.js文件es6还在,自然会爆炸。

回答

3

我设法解决与reddit上的帮助原来的问题。我不会使用摩卡测试,而是直接运行摩卡命令:

mocha --ui tdd --compilers js:babel/register test/**/*.js 

可以很容易地将它添加为npm脚本。

更进一步,因为我正在测试cordova项目,我需要幻影js。这也花了一些时间,从这个要点得到很多帮助:https://gist.github.com/nmabhinandan/6c63463d9f0987020c6f。但这里是我的情况下,任何人最后的安装感兴趣:

文件夹结构:

src/ 
    -- app code 
test/ 
    spec/ 
    stores/ 
     interaction_store_spec 
    SpecRunner.js 
tests.html 

module.exports = function (grunt) { 

    // Import dependencies 
    grunt.loadNpmTasks('grunt-contrib-watch') 
    grunt.loadNpmTasks('grunt-contrib-jshint'); 
    grunt.loadNpmTasks('grunt-browserify'); 
    grunt.loadNpmTasks('grunt-contrib-sass'); 
    grunt.loadNpmTasks('grunt-babel'); 
    grunt.loadNpmTasks('grunt-exec'); 
    grunt.loadNpmTasks('grunt-contrib-clean'); 

    grunt.initConfig({ 
    babel: { 
     options: { 
     sourceMap: true, 
     modules: "amd" 
     }, 
     test: { 
     files: [{ 
      expand: true, 
      src: ["test/**/*.js"], 
      dest: "dist", 
      ext: ".js" 
     }, { 
      expand: true, 
      src: ["src/**/*.js"], 
      dest: "dist", 
      ext: ".js" 
     }] 
     } 
    }, 
    browserify: { 
     dist: { 
     files: { 
      'www/js/bundle.js': ['src/app.js'], 
     }, 
     options: { 
      transform: [['babelify', { optional: ['runtime'] }]], 
      browserifyOptions: { 
      debug: true 
      } 
     } 
     } 
    }, 
    clean: { 
     test: ["dist/test"] 
    }, 
    eslint : { 
     target: ["src/**/*.js"] 
    }, 
    exec: { 
     run_tests: "node_modules/.bin/mocha-phantomjs -p $(which phantomjs) tests.html" 
    }, 
    sass: { 
     dist: { 
     options: { 
      style: 'compressed' 
     }, 
     files: { 
      'www/css/styles.css': 'www/css/sass/styles.scss' 
     } 
     } 
    }, 
    watch: { 
     css: { 
     files: ['www/css/sass/*.scss'], 
     tasks: ['sass'] 
     }, 
     scripts: { 
     files: ['src/**/*.js'], 
     tasks: ['eslint', 'browserify:dist'], 
     options: { 
      atBegin: true, 
      spawn: true 
     }, 
     }, 
    } 
    }); 

    grunt.registerTask("default", ['sass','watch']); 
    grunt.registerTask("test", ["clean", "babel:test", "exec:run_tests"]); 
}; 

从gruntfile需要注意的重要一点是利用巴别塔本身,而不是browserify的。我还加入了摩卡幻影和requirejs来支持测试。手表,sass和eslint仅用于构建运行应用程序的源代码。 babel将源码&测试编译到AMD模块。使用requirejs时,您必须在导入字符串中放置扩展名。它适用于browserify,但requirejs不会使用SpecRunner中的baseUrl(如果它具有扩展名)。

SpecRunner.js:

// RequireJS configuration 
require.config({ 
    baseUrl: 'dist/test', 
    urlArgs: 'cb=' + Math.random(), 
    paths: { 
    spec: 'spec', // lives in the test directory 
    }, 
    hbs: { 
    disableI18n: true 
    } 
}); 

let testSuite = { 
    specs: [ 
    'spec/stores/interaction_store_spec' 
    ] 
}; 

// run mocha 
(function() { 
    require(testSuite.specs, function() { 

    if (window.mochaPhantomJS) { 
     mochaPhantomJS.run(); 
    } else { 
     mocha.run(); 
    } 

    }); 
})(); 

Tests.html:

<!doctype html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
    <title>Mocha Spec Runner</title> 
    <link rel="stylesheet" href="node_modules/mocha/mocha.css"> 
</head> 
<body> 
    <div id="mocha"></div> 

    <script src="node_modules/mocha/mocha.js"></script> 
    <script src="node_modules/expect.js/index.js"></script> 

    <script> 
     mocha.ui('bdd'); 
     mocha.reporter('html'); 
    </script> 
    <script src="node_modules/requirejs/require.js" data-main="dist/test/SpecRunner"></script> 
</body> 
</html> 

包装JSON:

{ 
    "name": "test", 
    "version": "0.0.1", 
    "devDependencies": { 
    "babel": "^5.8.12", 
    "babel-runtime": "^5.8.12", 
    "babelify": "^6.1.3", 
    "eslint": "^0.24.1", 
    "expect.js": "^0.3.1", 
    "grunt": "^0.4.5", 
    "grunt-babel": "^5.0.1", 
    "grunt-browserify": "^3.8.0", 
    "grunt-contrib-clean": "^0.6.0", 
    "grunt-contrib-watch": "^0.6.1", 
    "grunt-eslint": "^16.0.0", 
    "grunt-exec": "^0.4.6", 
    "mocha": "^2.2.5", 
    "mocha-phantomjs": "^3.6.0", 
    "requirejs": "^2.1.20" 
    } 
} 

所以当我运行grunt testgrunt babel更具体地说,我得到一个dist文件夹在我的应用程序的根目录中:

dist/ 
    test/ 
    -- compiled specs 
    src/ 
    -- compiled source 

幻影运行的规格就好了。请注意,您需要通过自制软件或类似的东西安装phantomjs才能使其运行。我用1.9.2