博客 / 詳情

返回

使用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/
不能苟同, 一定要區分
具體請移步這裏: https://github.com/isaacs/nod...

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.