mochajs / mocha

β˜•οΈ simple, flexible, fun javascript test framework for node.js & the browser

Home Page:https://mochajs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› Bug: Test titles not shown for ES dynamic imports

nicholaswmin opened this issue Β· comments

Bug Report Checklist

  • I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
  • I have searched for related issues and issues with the faq label, but none matched my issue.
  • I have '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, my usage of Mocha, or Mocha itself.
  • I want to provide a PR to resolve this

Expected

Grouping tests using dynamic imports under a describe block would show them grouped under it's test title.

i.e this:

describe('Rate Limiting', async () => {
  await import('./conn.spec.js')
  await import('./event.spec.js')
})

would show up as this:

βœ” Rate Limiting
    Connections
      βœ” Rejects further handshake attempts
    Events
      βœ” Disconnects client if rate limit exceeded

Actual

Does not show outer describe test title. Instead it reports the run like this:

Connections
  βœ” Rejects further handshake attempts
Events
  βœ” Disconnects client if rate limit exceeded

Minimal, Reproducible Example

// index.spec.js
describe('Rate Limiting', async () => {
  await import('./conn.spec.js')
  await import('./event.spec.js')
})
// conn.spec.js
describe('Connections', () => {
  it ('does some handshake-y thing', () => {
    // some test
  })
})
// event.spec.js
describe('Events', () => {
  it ('does some thing with rate limiting', () => {
    // some test
  })
})

and run:

mocha index.spec.js

Versions

  • Mocha: v10.4.0
  • Node: v21.0.0
  • OS: Mac OS Sonoma v14.4.1

Additional Info

CommonJS modules don't seem to have this issue. This works out fine, no problem.

describe('Rate Limiting', () => {
  require('./conns.spec.cjs')
  require('./event.spec.cjs')
})

πŸ‘‹ Sorry for the long wait time on this one @nicholaswmin!

I played around with it a bit locally and couldn't figure out why imports would have anything to do with test titles...

...because they don't - this repros with an single-file test!

describe("example", async () => {
    await new Promise();
    it("2 + 2", () => {});
});
$ npx mocha test.spec.js


  example
    βœ” before


  1 passing (2ms)

Two related existing items to look at:

Closing as a duplicate of #2116, as that one is already accepting PRs.