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

📝 Docs: Should let people know test file execution order can be controlled by import/require #4925

Closed
vassudanagunta opened this issue Sep 19, 2022 · 3 comments
Labels
area: documentation anything involving docs or mochajs.org status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal

Comments

@vassudanagunta
Copy link

vassudanagunta commented Sep 19, 2022

The docs mention a number of ways that test file execution order can be controlled, but there is no mention of the most semantically powerful way: by importing or requiring testA.js in testB.js, testA.js will execute first.

This is incredibly powerful, as one is able to express that if both testA.js and testB.js tests fail, the failures should be examined in testA.js first, either because there is a dependency (e.g. testB.js depends on things tested by testA.js) or because testA.js tests something more fundamental.

See How to set execution order of mocha test cases in multiple files, in particular this answer.

@vassudanagunta vassudanagunta added the type: feature enhancement proposal label Sep 19, 2022
@JoshuaKGoldberg JoshuaKGoldberg added area: documentation anything involving docs or mochajs.org area: qa labels Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg changed the title [Documentation] Should let people know test file execution order can be controlled by import/require 📝 Docs: Should let people know test file execution order can be controlled by import/require Dec 27, 2023
@JoshuaKGoldberg
Copy link
Member

🤔 importing from one test file to another is a pretty uncommon practice - and for good reason. Depending on how your tests are being run, it can result in describe() blocks getting executed multiple times. mocha --parallel will create a worker pool for tests. Each worker will execute all the describe()s that get imported in its files.

You can reproduce this by creating two test files:

// a.spec.js
require("./b.spec.js");
it("test a", () => {});
// b.spec.js
it("test b", () => {});

Running sequentially gets the nice ordering you're looking for:

$ npx mocha *.spec.js 

  ✔ test b
  ✔ test a

  2 passing (1ms)

...but with -parallel we get a duplicate test b:

$ npx mocha *.spec.js --parallel

  ✔ test b

  ✔ test b
  ✔ test a

  3 passing (74ms)

Other test frameworks such as Jest experience this as well. Which means it's also a problem for portability between frameworks.

An alternative way to control test file execution order is to change the order files are passed to the mocha command. In the above example, mocha b.spec.js a.spec.js runs test b before test a.

Closing as this isn't a recommendation we'll want to add to the docs. Thanks for filing though! +1 on the idea of filing SO answers as docs requests on the framework, that's a very good thing. 🤎

@JoshuaKGoldberg JoshuaKGoldberg closed this as not planned Won't fix, can't repro, duplicate, stale Feb 6, 2024
@JoshuaKGoldberg JoshuaKGoldberg added the status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior label Feb 6, 2024
@vassudanagunta
Copy link
Author

Thanks for your answer. I have zero objection to the wontfix.

An alternative way to control test file execution order is to change the order files are passed to the mocha command. In the above example, mocha b.spec.js a.spec.js runs test b before test a.

This doesn't scale very well, and the logical dependencies between tests aren't expressed where they should be, in the test that assumes functionality covered by another test works correctly.

Should I submit this as a feature request? Or do you think that's not gonna fly?

@JoshuaKGoldberg
Copy link
Member

I wouldn't object to the filing, but note that per #5027 we're trying not to make any big splashy changes -including feature additions- that don't have a lot of demand from users. Given that this issue only had one 👍 I can tell you that it'd be a "no" from me and I'm guessing a "no" from the other maintainers. But I can't say for sure. Sorry to be the bearer of bad news. 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: documentation anything involving docs or mochajs.org status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

2 participants