|
| 1 | +// @ts-check |
| 2 | +const path = require('path') |
| 3 | +const debug = require('debug')('cypress-react-unit-test') |
| 4 | +const webpack = require('@cypress/webpack-preprocessor') |
| 5 | +const findWebpack = require('find-webpack') |
| 6 | + |
| 7 | +module.exports = (on, config) => { |
| 8 | + require('@cypress/code-coverage/task')(on, config) |
| 9 | + |
| 10 | + const webpackFilename = config.env && config.env.webpackFilename |
| 11 | + if (!webpackFilename) { |
| 12 | + throw new Error( |
| 13 | + 'Could not find "webpackFilename" option in Cypress env variables %o', |
| 14 | + config.env, |
| 15 | + ) |
| 16 | + } |
| 17 | + debug('got webpack config filename %s', webpackFilename) |
| 18 | + const resolved = path.resolve(webpackFilename) |
| 19 | + debug('resolved webpack at %s', webpackFilename) |
| 20 | + const webpackOptions = require(resolved) |
| 21 | + |
| 22 | + debug('webpack options: %o', webpackOptions) |
| 23 | + |
| 24 | + const coverageIsDisabled = |
| 25 | + config && config.env && config.env.coverage === false |
| 26 | + debug('coverage is disabled? %o', { coverageIsDisabled }) |
| 27 | + |
| 28 | + const opts = { |
| 29 | + coverage: !coverageIsDisabled, |
| 30 | + } |
| 31 | + |
| 32 | + findWebpack.cleanForCypress(opts, webpackOptions) |
| 33 | + debug('claned webpack options: %o', webpackOptions) |
| 34 | + |
| 35 | + const options = { |
| 36 | + webpackOptions, |
| 37 | + watchOptions: {}, |
| 38 | + } |
| 39 | + |
| 40 | + on('file:preprocessor', webpack(options)) |
| 41 | + |
| 42 | + // IMPORTANT to return the config object |
| 43 | + // with the any changed environment variables |
| 44 | + return config |
| 45 | +} |
0 commit comments