本例使用 karma、mocha、chai 为 webpack 项目添加单元测试。其中,karma 是测试过程管理框架,mocha 是运行测试用例的框架,chai 是单元测试断言库。下面一步步开始实践吧:
1、项目目录
- src:源代码目录
- test:测试用例目录
- unit:测试文件目录
- cases.js:测试用例入口文件
- karma.conf.js:karma 配置文件
2、karma.conf.js
const path = require('path');
const ROOT_PATH = path.resolve(__dirname, '..');
const SRC_PATH = path.resolve(ROOT_PATH, 'src');
module.exports = (config) => {
config.set({
// Relative directory, files and exclude will based on it if it is set. The directory of karma.conf.js is the default value.
basePath: path.resolve(ROOT_PATH),
// Assert directory, directories can be use