【发布时间】:2015-07-21 18:34:26
【问题描述】:
我正在尝试利用 grunt & babel 来加载我的 es6 源代码作为给定测试的依赖项。所以我一直在运行实际的 src 并通过 browserify 编译应用程序就好了:
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。然后我创建了一个规范文件: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);
});
});
});
所以我直接导入商店。我在测试过程的 grunt 文件中添加了几个部分:
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']);
};
babel 编译测试没问题,但是当我运行它时,它会加载 src 文件夹中的 .js 文件,仍然在 es6 中,自然会爆炸。
【问题讨论】:
标签: javascript mocha.js babeljs