我想利用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还在,自然会爆炸。