Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression (8.1.2): mocha/mocha.js is no longer bundle compatible (uses dynamic require) #4422

Closed
4 tasks done
AviVahl opened this issue Aug 26, 2020 · 2 comments · Fixed by #4419
Closed
4 tasks done
Labels
semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer

Comments

@AviVahl
Copy link
Contributor

AviVahl commented Aug 26, 2020

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Starting with v8.1.2, mocha/mocha.js contains node-specific code (dynamic require call) and is no longer bundle compatible

Steps to Reproduce

mkdir mocha-bundle-test
cd mocha-bundle-test
npm init -y
npm i -D webpack webpack-cli mocha
mkdir src
echo "import mocha from 'mocha/mocha.js'; console.log(mocha);" > src/index.js
npx webpack --mode development

Expected behavior: [What you expect to happen]

No errors or warnings when trying to import and bundle mocha/mocha.js.
Same as 8.1.1.

Actual behavior: [What actually happens]

WARNING in ./node_modules/mocha/mocha.js 25044:26-47
Critical dependency: the request of a dependency is an expression
 @ ./src/index.js

Reproduces how often: [What percentage of the time does it reproduce?]
Consistently. 100% of the time.

Versions

npx envinfo

System:
    OS: Linux 5.7 Fedora 32 (Workstation Edition) 32 (Workstation Edition)
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 17.04 GB / 31.21 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.8.0 - ~/.nvm/versions/node/v14.8.0/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v14.8.0/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v14.8.0/bin/npm
  Managers:
    pip3: 19.3.1 - /usr/bin/pip3
    Yum: 4.2.23 - /usr/bin/yum
  Utilities:
    Make: 4.2.1 - /usr/bin/make
    GCC: 10.2.1 - /usr/bin/gcc
    Git: 2.26.2 - /usr/bin/git
    FFmpeg: 4.2.4 - /usr/bin/ffmpeg
  Servers:
    Apache: 2.4.43 - /usr/sbin/apachectl
  IDEs:
    Nano: 4.9.3 - /usr/bin/nano
    VSCode: 1.48.2 - /usr/bin/code
  Languages:
    Bash: 5.0.17 - /usr/bin/bash
    Perl: 5.30.3 - /usr/bin/perl
    Python: 3.8.5 - /usr/bin/python
    Python3: 3.8.5 - /usr/bin/python3
  Databases:
    SQLite: 3.33.0 - /usr/bin/sqlite3
  Browsers:
    Chrome: 85.0.4183.83
    Firefox: 79.0

Additional Information

This issue breaks mocha-loader and mocha-pup.

@Munter
Copy link
Contributor

Munter commented Aug 26, 2020

Looks like Mocha.unloadFile and Mocha.prototype.unloadFiles should be removed from the browser bundle:

Here are the relevant pieces of code from the bundle. Seems webpack is breaking on the require.resolve() call, which should never be used in a browser context anyway:

	  Mocha.unloadFile = function (file) {
	    delete require.cache[require.resolve(file)];
	  };
	  /**
	   * Unloads `files` from Node's `require` cache.
	   *
	   * @description
	   * This allows required files to be "freshly" reloaded, providing the ability
	   * to reuse a Mocha instance programmatically.
	   * Note: does not clear ESM module files from the cache
	   *
	   * <strong>Intended for consumers &mdash; not used internally</strong>
	   *
	   * @public
	   * @see {@link Mocha#run}
	   * @returns {Mocha} this
	   * @chainable
	   */


	  Mocha.prototype.unloadFiles = function () {
	    if (this._state === mochaStates.DISPOSED) {
	      throw createMochaInstanceAlreadyDisposedError('Mocha instance is already disposed, it cannot be used again.', this._cleanReferencesAfterRun, this);
	    }

	    this.files.forEach(function (file) {
	      Mocha.unloadFile(file);
	    });
	    this._state = mochaStates.INIT;
	    return this;
	  };

@Munter Munter added type: bug a defect, confirmed by a maintainer semver-patch implementation requires increase of "patch" version number; "bug fixes" and removed unconfirmed-bug labels Aug 26, 2020
@boneskull
Copy link
Contributor

Fix for this is in #4419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch implementation requires increase of "patch" version number; "bug fixes" type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants