mocha-loader

Allows Mocha tests to be loaded and run via webpack

安装

  1. npm i -D mocha-loader

用法

#

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

要求

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

配置(推荐)

  1. import test from './test'

webpack.config.js

  1. module.exports = {
  2. entry: './entry.js',
  3. output: {
  4. path: __dirname,
  5. filename: 'bundle.js'
  6. },
  7. module: {
  8. rules: [
  9. {
  10. test: /test\.js$/,
  11. use: 'mocha-loader',
  12. exclude: /node_modules/
  13. }
  14. ]
  15. }
  16. }

选项

None

示例

基本

module.js

  1. module.exports = true

test.js

  1. describe('Test', () => {
  2. it('should succeed', (done) => {
  3. setTimeout(done, 1000)
  4. })
  5. it('should fail', () => {
  6. setTimeout(() => {
  7. throw new Error('Failed')
  8. }, 1000)
  9. })
  10. it('should randomly fail', () => {
  11. if (require('./module')) {
  12. throw new Error('Randomly failed')
  13. }
  14. })
  15. })