0

I have a node.js mocha test that when I run it through webpack I get:

"ReferenceError: describe is not defined"

How do I make webpack ignore this global method that mocha has defined when when it runs?

Here is my test case with a work-around hack

// This is a nasty hack so webpack can compile without throwing "ReferenceError: describe is not defined"
// This global is replaced via sed before mocha runs it
global.describe = function() {};

describe('My services', async function() { ... });

I'd prefer to not include my full webpack config, but can if required. Here are the important parts

    cache: true,
    mode: 'development',
    target: 'node',
    // Per https://github.com/liady/webpack-node-externals#quick-usage
    externalsPresets: {node: true}, // ignore built-in modules like path, fs, etc.
    externals: [nodeExternals()], // ignore all modules in node_modules folder
    watchOptions: {
      ignored: ['**/node_modules', '**/build'],
    },
    // Don't want source maps
    devtool: undefined,
    module: {
      rules:{
    test: /\.c?js$/,
    exclude: /node_modules/,
    use: {
      loader: 'babel-loader',
      options: {
        presets: [
          [
            '@babel/preset-env',
            {
              // @see https://babeljs.io/docs/en/options#targets (added in @babel/core 7.13)
              targets: 'node 18.0',
              // Per https://babeljs.io/docs/en/babel-preset-env#modules
              // > If you are using a bundler with Babel, the default modules: "auto" is always preferred.
              modules: 'auto',
              useBuiltIns: 'usage',
              corejs: '3.6',
            },
          ],
        ],
        plugins:[omitted],
      },
    },
  },
    },

Here is my sed trick that removes global describe from the bundled js:

sed 's|global.describe = function () {};||' build/serviceTests.cjs > build/serviceTestsNoGlobalDescribe.cjs && mocha build/serviceTestsNoGlobalDescribe.cjs

Environment info:

  Node.js: v18

  package.json:
  "type": "module",
  "[dev]dependencies": {
    "mocha": "10.2.0",
    "@babel/core": "7.20.12",
    "@babel/plugin-proposal-class-properties": "7.18.6",
    "@babel/plugin-proposal-decorators": "7.20.13",
    "@babel/plugin-proposal-throw-expressions": "7.18.6",
    "@babel/plugin-transform-runtime": "7.19.6",
    "@babel/preset-env": "7.20.2",
    "babel-loader": "8.0.6",
    "webpack": "5.75.0",
    "webpack-cli": "5.0.1",
    "webpack-node-externals": "3.0.0"
  },
rynop
  • 50,086
  • 26
  • 101
  • 112
  • Why are you bundling test code? – Phil Feb 13 '23 at 23:00
  • Because I need babel and other transforms/logic I get from webpack – rynop Feb 14 '23 at 19:33
  • Like what? You don't need to bundle your tests and can definitely use Babel with Mocha without involving Webpack. See [Running Mocha 6 ES6 tests with Babel 7, how to set up?](https://stackoverflow.com/q/56682958/283366). That being said, what exactly are you using in your test code that cannot be directly run via Node v18? – Phil Feb 15 '23 at 00:44
  • It's a long story - lets just say I was able to convince you that I needed webpack, is there a cleaner solution than my hack? – rynop Feb 15 '23 at 20:57

0 Answers0