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: when using exports interface for tests, there's no file on suites objects #5146

Open
4 tasks done
shadowusr opened this issue May 22, 2024 · 10 comments · May be fixed by #5147
Open
4 tasks done

🐛 Bug: when using exports interface for tests, there's no file on suites objects #5146

shadowusr opened this issue May 22, 2024 · 10 comments · May be fixed by #5147
Labels
semver-minor implementation requires increase of "minor" version number; "features" status: accepting prs Mocha can use your help with this one!

Comments

@shadowusr
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

You can access the file field on the suite object regardless of the interface you use.

Actual

file is undefined for exports mocha interface, but works correctly for bdd interface.

Minimal, Reproducible Example

  1. Use Mocha API with exports interface:
const mocha = new Mocha({ui: "exports"});
  1. Subscribe to events to receive suite object and load files:
const eventBus = MochaEventBus.create(mocha.suite);
eventBus.on(MochaEventBus.events.EVENT_SUITE_ADD_SUITE, (suite) => {
    console.log(suite.file); // will output undefined
});

await mocha.loadFilesAsync();
  1. See that suite.file is undefined.

Versions

10.2.0, 10.4.0

Additional Info

We use mocha API in on of our tools internally and this use case is crucial for us. Otherwise, we'll need to implement dirty hacks to retrieve file field from test objects.

@shadowusr shadowusr added status: in triage a maintainer should (re-)triage (review) this issue type: bug a defect, confirmed by a maintainer labels May 22, 2024
@shadowusr shadowusr linked a pull request May 22, 2024 that will close this issue
3 tasks
@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented May 24, 2024

Thanks for filing!

Minimal, Reproducible Example

This is most of the way to an MRE but:

  • It doesn't explain what MochaEventBus is
  • It doesn't include what file(s) need to be on disk to populate suite.file

Could you please provide a way for us to try out your issue locally? I think I get it, but really need a local reproduction to be sure.

@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 May 24, 2024
@shadowusr
Copy link
Author

@JoshuaKGoldberg yeah sorry for not providing totally reproducible example.

Here it goes: https://stackblitz.com/edit/stackblitz-starters-pnnurz?file=mocha-bdd-demo.js

You can try running node mocha-bdd-demo.js vs node mocha-exports-demo.js. In the bdd case, you'll see file path on suite, but on the exports ui it will be undefined.

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

@JoshuaKGoldberg sorry to ping you again, but do we have any updates on this? Looks like a tiny issue to me with a ready PR, I would really appreciate if you could take a look at it.

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jun 15, 2024

No update just yet - we've all been pretty swamped on the maintainer team. It's in our queue. Sorry for not having anything more specific.

For context, in addition to this & other issues in triage, we've got some PR reviews to finalize (time boxed to the end of this month), as well as some administrative work going on behind-the-scenes still.

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jul 3, 2024

Ok! Sorry for the delay: yes, I think this makes sense. I'll throw this in front of @mochajs/maintenance-crew just in case I'm missing something, but 👍 from me.

@voxpelli
Copy link
Member

Looks like all the other three ui options call this common.suite.create, which in turn call Suite.create:

create: function create(opts) {
var suite = Suite.create(suites[0], opts.title);
suite.pending = Boolean(opts.pending);
suite.file = opts.file;

See eg. bdd.js:

context.describe = context.context = function (title, fn) {
return common.suite.create({
title,
file,
fn
});
};

But exports.js work different, it calls Suite.create directly in some cases and in other cases uses the Suite created as part of Mocha itself.

module.exports = function (suite) {
var suites = [suite];
suite.on(Suite.constants.EVENT_FILE_REQUIRE, visit);
function visit(obj, file) {
var suite;
for (var key in obj) {
if (typeof obj[key] === 'function') {
var fn = obj[key];
switch (key) {
case 'before':
suites[0].beforeAll(fn);
break;
case 'after':
suites[0].afterAll(fn);
break;
case 'beforeEach':
suites[0].beforeEach(fn);
break;
case 'afterEach':
suites[0].afterEach(fn);
break;
default:
var test = new Test(key, fn);
test.file = file;
suites[0].addTest(test);
}
} else {
suite = Suite.create(suites[0], key);
suites.unshift(suite);
visit(obj[key], file);
suites.shift();
}
}
}
};

It does look like this was supposed to be fixed in 2015 through #1993, but it only added .file to tests.

I wonder if it would be breaking in any way to add suites[0].file = file before the for-loop here? That should ensure it's available everywhere.

function visit(obj, file) {
var suite;
for (var key in obj) {

(I also wonder how often mocha is used with these non-bdd interfaces)

@voxpelli
Copy link
Member

(I also wonder how often mocha is used with these non-bdd interfaces)

Answering myself somewhat: Summarizing mocharc files on sourcegraph gives that 75% specify bdd, 25% tdd and pretty much no one qunit or exports

That's probably not an exhaustive result though

@JoshuaKGoldberg
Copy link
Member

I wonder if it would be breaking in any way to add suites[0].file = file before the for-loop here? That should ensure it's available everywhere.

IMO it should be fine as a purely additive change. We can treat it as a minor version rather than a patch if we really want to be careful.

@JoshuaKGoldberg JoshuaKGoldberg added status: accepting prs Mocha can use your help with this one! semver-minor implementation requires increase of "minor" version number; "features" and removed type: bug a defect, confirmed by a maintainer labels Oct 29, 2024
@JoshuaKGoldberg
Copy link
Member

Marking as accepting PRs for a semver-minor change. 🚀

@JoshuaKGoldberg JoshuaKGoldberg removed the status: in triage a maintainer should (re-)triage (review) this issue label Oct 30, 2024
@JoshuaKGoldberg
Copy link
Member

By the way, sorry for the wait @shadowusr. I took a look at #5147 and it looks great to me - just one request for testing. Since it's been so long, I'd understand if you no longer have time for this and can add the test + merge it in a few weeks if you don't. Cheers! 🤎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-minor implementation requires increase of "minor" version number; "features" status: accepting prs Mocha can use your help with this one!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants