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

🐛 Bug: Custom describe block wrapper does not await for an internal process runs in mocha's root hook #5234

Open
3 of 4 tasks
0xmaayan opened this issue Oct 16, 2024 · 2 comments
Labels
stale this has been inactive for a while... status: waiting for author waiting on response from OP - more information needed type: bug a defect, confirmed by a maintainer

Comments

@0xmaayan
Copy link

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

I am using mocha to build a testing framework and wrapping mocha's describe block with a custom describe block function and injecting a custom internal instance to be used in tests suites by the end user.

import { describe as MochaDescribe } from "mocha";

export function describe(description: string, block: (customInstance: CustomInstance) => void) {
  MochaDescribe(description, function () {
    block(customInstance); 
  });
}

Everything was working as expected, until I added an internal process that runs as a root hook and when finishes assign the customInstance a value, that should be used in tests suites by the end user.

Actual

Looks like the custom describe block doesnt await for the root hook to finish before assigning the new global value to be used in a test suite by the end user

Minimal, Reproducible Example

// mochaConfig.ts file

const mochaConfig: CustomMochaOptions = {
    require: [path.join(__dirname, "rootHook.js")],
    parallel: true,
};

const mocha = new Mocha(mochaConfig);

// run mocha
await new Promise<number>((resolve) => {
    mocha.run(resolve);
});




// rootHook.ts file
export const mochaHooks: RootHookObject = {
  beforeAll: async function () {
    await new Promise((resolve) => setTimeout(resolve, 10000));
   // inject customInstance to the global object
   myGlobal.customInstance = {key:"value"};
  },
};





// api.ts file
export interface CustomGlobal extends NodeJS.Global {
  customInstance: {};
}

declare var global: CustomGlobal;

export var myGlobal: CustomGlobal = global;

import { describe as MochaDescribe } from "mocha";

export function describe(description: string, block: (customInstance: CustomInstance) => void) {
  MochaDescribe(description, function () {
    block(myGlobal.customInstance); 
  });
}




// test.ts file
import { describe } from "../api";

describe("my first test", (customInstance) => {
 
});

Versions

"mocha": "^10.7.0",
node --version v21.6.2

Additional Info

No response

@0xmaayan 0xmaayan added status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer labels Oct 16, 2024
@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Oct 29, 2024

Minimal, Reproducible Example

This isn't an MRE 🙂 - could you please post one that we can get running quickly locally? We're a small maintenance team and don't have the time to piece stuff together.

https://antfu.me/posts/why-reproductions-are-required -> https://antfu.me/posts/why-reproductions-are-required#reproducible-projects-or-playgrounds

Thanks!

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for author waiting on response from OP - more information needed and removed status: in triage a maintainer should (re-)triage (review) this issue labels Oct 29, 2024
@JoshuaKGoldberg
Copy link
Member

👋 ping @0xmaayan, is this still something you'd like us to look at?

@JoshuaKGoldberg JoshuaKGoldberg added the stale this has been inactive for a while... label Nov 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale this has been inactive for a while... status: waiting for author waiting on response from OP - more information needed type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

2 participants