镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Fast fail feature for the node:test context #42990

Description

@null8626

What is the problem this feature will solve?

A test's progress can sometimes be crucial for the next one, if one of them fails, say like this:

let module;
let instance;

test("importing module", async () => {
  module = await import("...");
});

test("using module feature 1", () => {
  instance = new module.SomeExportedClass("arg1", { option: true });
});

test("request to API", async () => {
  await instance.apiEndpoint({ ... });
});

While yes, if the library has checks, this would be pointless, but it would be better to have an option for the the rest of the tests to fail if one had failed.

What is the feature you are proposing to solve the problem?

Something like this:

test("test for myModule", { fastFail: true }, (t) => {
  t("test that fails", () => {
    // This should make the next tests to fail as well.
    assert(false);
  });

  t("test that will never succeed", () => {
    console.log("This won't log to the console.");
  });
});

What alternatives have you considered?

No response

Activity

  1. moved this to Pending Triage in Node.js feature requestson May 6, 2022
  2. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on May 6, 2022
  3. VoltrexKeyva commented on May 6, 2022

    @VoltrexKeyva
    Contributor

    @nodejs/test_runner

  4. RaisinTen commented on May 6, 2022

    @RaisinTen
    Member

    GitHub Actions does provide something like this - https://github.com/proxy/docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategyfail-fast. Not sure if this is something that's already present in other existing test runners.

  5. ljharb commented on May 6, 2022

    @ljharb
    SponsorMember

    I believe jest does as well. It’s typically present in CI runners with a matrix, but it’s very rare for a test runner to have it.

    Usually when running your tests, you don’t want it to fail fast - you want the maximal information. Otherwise, you’re constantly running, fixing one problem, and rerunning - it’s a game of whack-a-mole.

  6. null8626 commented on May 6, 2022

    @null8626
    ContributorAuthor

    But even though it might be rare for it to exist on other existing test runners, it can be useful for it to fail and not continue further with the other tests until the one failed is also fixed

  7. RaisinTen commented on May 6, 2022

    @RaisinTen
    Member

    You could move the part you would expect to fail the rest of the tests outside the test and that should work as expected, right?

    const test = require('node:test');
    
    test("test for myModule", async (t) => {
      let module = require('node:does-not-exist');
      let instance;
    
      await t.test("using module feature 1", () => {
        instance = new module.SomeExportedClass("arg1", { option: true });
      });
    
      await t.test("request to API", async () => {
        await instance.apiEndpoint({ ... });
      });
    });
  8. ljharb commented on May 6, 2022

    @ljharb
    SponsorMember

    @vierofernando the claim i'm making is that it's rare because it's not actually useful.

    With no fast-fail, if you only want one failure, you can control-C, or mentally ignore everything after the first failure - both of which are easy.

    With fast-fail, if you want more than one failure, you have to rerun your entire test suite - which is hard.

  9. juliangruber commented on May 6, 2022

    @juliangruber
    Member

    I assume this depends on the test suite size.

    When you have a large test suite, you want all tests to always run, because test runs are expensive.

    When you have a small test suite, running it is cheap so the main point becomes convenience - you don't want to have to scroll through / see test output after the first failure, because that's already all you want to know.

  10. ljharb commented on May 6, 2022

    @ljharb
    SponsorMember

    @juliangruber sure! but since running is cheap, if you want the first failure, you can quickly jump to the top of the scrollback (which you cleared prior to running the test suite) and it'll be right there :-)

  11. juliangruber commented on May 6, 2022

    @juliangruber
    Member

    Good point! You could also use Unix tools like less for this. Or have a test runner that offers this.

    It's a question of dx vs simplicity, or docs I would say?

  12. juliangruber commented on May 6, 2022

    @juliangruber
    Member

    I wouldn't be surprised if 80% of node users don't know how to clear their scrollback

  13. ljharb commented on May 6, 2022

    @ljharb
    SponsorMember

    agreed, but i'd also be surprised if even 1% of that set of users wanted to see one failure at a time ;-)

  14. himself65 commented on May 7, 2022

    @himself65
    Member

    I think your example is not a good idea. test API shouldn't be related to each other.

    the correct API and usage should be like

    let module;
    let instance;
    
    before("importing module", async () => {
      module = await import("...");
    });
    
    before("using module feature 1", () => {
      instance = new module.SomeExportedClass("arg1", { option: true });
    });
    
    test("request to API", async () => {
      await instance.apiEndpoint({ ... });
    });
  15. github-actions commented on Nov 3, 2022

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

    For more information on how the project manages feature requests, please consult the feature request management document.

  16. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Nov 3, 2022
  17. kmannislands commented on Nov 11, 2022

    @kmannislands

    It's easily possible to bail out on the first failure by implementing your own entry using the run API if that's what you desire.

    It seems like unnecessary complexity to support this feature in the default CLI, IMO.

    I'd suggest closing this as "Won't do."

  18. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Nov 12, 2022
  19. EndangeredMassa commented on Apr 18, 2023

    @EndangeredMassa

    I use Fail Fast options in other test runners as a debugging tool in CI, specifically. When CI is flakey or just takes a long time to run, I want to know as soon as possible when there's a failure so that I can act. Watching fast moving logs for a single failure is annoying. Seeing the full test suite failed indicator light up is much easier. This reduces the feedback loop.

    I consider this a temporary flag. I wouldn't run normal test suites with this on, because (as others have said) you usually want to know about all failing tests. However, as a tool I can use during debugging, then revert it, I find it very useful.

    It's not a necessity, sure. Lots of test runners don't even have it. It is very handy when you need it, though.

  20. MoLow commented on Jun 5, 2023

    @MoLow
    Member

    this feature can be achieved very in a very straightforward way using reporters, so I will close now.
    for convenience, I have created a user-land implementation of this: https://www.npmjs.com/package/@reporters/bail
    even if you do not want to install an npm package for this, implementing this yourself is ±5 lines of code

  21. extremeheat commented on Jun 30, 2023

    @extremeheat

    It's a shame a trivial, but useful, feature like this doesn't exist natively inside the test runner as a CLI flag. For codebases that have many tests inside them, waiting for an entire test suite to run before being able to read an error stack trace can be incredibly annoying.

    I use mocha --bail or jest --bail all the time, if it needs hacking on some extra code to the project to get this functionality or requires installing some other 3P package, you may as well continue using mocha or jest.

  22. MoLow commented on Jul 2, 2023

    @MoLow
    Member

    It's a shame a trivial, but useful, feature like this doesn't exist natively inside the test runner as a CLI flag. For codebases that have many tests inside them, waiting for an entire test suite to run before being able to read an error stack trace can be incredibly annoying.

    PR's attempting to add this feature are welcome. just note adding cli flags to node.js isn't as easy as adding them for standalone test runners.
    I recommend @cjihrig 's blog post https://cjihrig.com/test_runner_expectations explaining why not evert "trivial" feature is going to be implemented into node.js test runner

  23. Pheromon commented on Dec 5, 2023

    @Pheromon

    WTH a bail() and timeout() function to migrate almost seamlessly from mocha would have been so great!

  24. inf3rnus commented on Feb 12, 2025

    @inf3rnus

    I'm gonna be harsh for a second.

    Of all of the features we could have, this really should be prioritized.

    Really takes away from the value of tests to the programmer when iterating on code. Comparing against Jest, I have to scour through the output to the terminal, which really messes up the natural flow of writing tests and writing new code.

    Anyone in the same boat has to hack their way through this, it just feels like something that should have been a core feature.

    Not to be a hater, but Bun has this already :/

  25. pmarchini commented on Feb 12, 2025

    @pmarchini
    Member

    Hey @inf3rnus, we're discussing an implementation of this feature.
    Here's a link to an open MR: #56490

  26. inf3rnus commented on Feb 12, 2025

    @inf3rnus

    Amazing, thanks for the update 🙏. Appreciate everyone's hard work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues requesting new Node.js features.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions