question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to import mocha API functions from 'mocha'

See original GitHub issue

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

It is no longer possible to do ES6 style imports from mocha. e.g.

import { beforeEach, afterEach } from 'mocha';
import { describe, it } from 'mocha';

Steps to Reproduce

Write a my-test.js module like

import { describe, it } from 'mocha';

describe('A test', function () {
  it('should thing', function () {
  });
});

Expected behavior:

This test runs and passes

Actual behavior:

Something like

TypeError: (0 , mocha__WEBPACK_IMPORTED_MODULE_2__.describe) is not a function

(In this case webpack is the bundler)

Reproduces how often:

100%

Versions

I believe this was introduced by this PR: https://github.com/mochajs/mocha/pull/4746 and released as version 9.1.2

Additional info

I agree with the general goal in the PR: to stop polluting the mocha module with the contents of global; but it throws the baby out with the bathwater because this was previously how you could write self-documenting import statements which keeps linters happy. eslint:recommended contains no-undef so if you are writing your tests in modules and bundling them together, you are now in a pickle.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
juergbacommented, Oct 4, 2021

@apellerano-pw @PaperStrike thank you, guys.

In Node this currentContext is set to global (which corresponds to globalThis / window / ?? after bundling). We use a proxy for setting currentContext since in parallel mode each process has its own global. In browser there is no parallel mode, so evtl. by just adding var currentContext = global; to browser-entry.js should do the trick.

I tend to the second solution. It partially does what we had before, assigning from global to mocha, this time just more selective.

Some months ago we bundled to IIFE format, then had to switched to UMD due to problems with other bundlers using our bundle. Mocha, mocha , and global are not identical in these two formats. So - when we drop IE11 with next major release - I’m dreaming of bundling to the ESM format. Which hopefully should be easier to understand and also capable for other bundlers.

1reaction
apellerano-pwcommented, Oct 12, 2021

Thank you for suggesting a patch, @juergba. I tested the following patch and it works.

[
  'after',
  'afterEach',
  'before',
  'beforeEach',
  'context',
  'describe',
  'it',
  'specify',
  'xcontext',
  'xdescribe',
  'xit',
  'xspecify'
].forEach(function (key) {
  mocha[key] = global[key];
});

I added the context and specify (and xcontext, xspecify) entries to the array since they are synonyms for describe and it respectively.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocha + TypeScript: Cannot use import statement outside a ...
I created the app using TypeScript and CommonJS module to transpile, so I added "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' ...
Read more >
Parallel mode fails silently sometimes with tests that import ...
Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.
Read more >
mocha-loader | webpack - JS.ORG
Allows Mocha tests to be loaded and run via webpack. ... To begin, you'll need to install mocha-loader and mocha : ... import...
Read more >
Documentation - Mocha
Manually dispose this mocha instance. Mark this instance as disposed and unable to run more tests. It also removes function references to tests...
Read more >
Mocha documentation — DevDocs
This is useful if the APIs you are testing return promises instead of taking callbacks: beforeEach(function() { return db.clear ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found