赞助 webpack,同时从官方商店购买衣服 所有收益将转到我们的 open collective

mocha-loader

Allows Mocha tests to be loaded and run via webpack

安装

npm i -D mocha-loader

用法

##

webpack --module-bind 'mocha-loader!./test'

要求

import test from 'mocha-loader!./test'

配置(推荐)

import test from './test'

webpack.config.js

module.exports = {
  entry: './entry.js',
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /test\.js$/,
        use: 'mocha-loader',
        exclude: /node_modules/
      }
    ]
  }
}

选项

None

示例

基本

module.js

module.exports = true

test.js

describe('Test', () => {
  it('should succeed', (done) => {
    setTimeout(done, 1000)
  })

  it('should fail', () => {
    setTimeout(() => {
      throw new Error('Failed')
    }, 1000)
  })

  it('should randomly fail', () => {
    if (require('./module')) {
      throw new Error('Randomly failed')
    }
  })
})