这篇文章主要为大家展示了如何使用mocha对webpack打包的项目进行"冒烟测试",内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

第一步: 打包开始之前删除'./dist'目录

rimraf('./dist', () => {constprodConfig = require('../../lib/webpack.prod')webpack(prodConfig, (err, stats) \=> {if (err) { console.log(err) process.exit(2) }console.log(stats.toString({ color:true, modules:false, children:false }))// 第三步: 将测试规则添加到打包后mocha.addFile(resolve(\_\_dirname, './html-test.js'))mocha.addFile(resolve(\_\_dirname, './css-js-test.js'))mocha.run()})})

第二步: 新建测试规则

const glob = require('glob');describe('Checking generated html files',() \=> { it('should generate html files', (done) \=> { constfiles = glob.sync('./dist/+(index|search).html') if (files.length) { done() } else { thrownewError('no html files generated') } });});

Tip: 关于glob.sync()方法的特别说明:

pattern {String}:匹配模式。options {Object}return: {Array<String>}:匹配模式下的文件名。

这里重点说说这个pattern, 这个pattern是字符串, 不是正则, 它有自己的匹配规则, 例如:

'./dist/+(index|search).html'

换成正则的写法为:

/\.\/dist\/(index|search)\.html/

不能苟同, 一定要区分

以上就是关于如何使用mocha对webpack打包的项目进行"冒烟测试"的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。