详解使用mocha对webpack打包的项目进行"冒烟测试"的大致流程

(编辑:jimmy 日期: 2024/9/21 浏览:2)

第一步: 打包开始之前删除'./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/

不能苟同, 一定要区分

具体请移步这里: https://github.com/isaacs/node-glob

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。