node icon indicating copy to clipboard operation
node copied to clipboard

test_runner: add suite hooks

Open kristofferjansson opened this issue 1 year ago • 2 comments

This is a proposal to add suite hooks to the test runner. Makes it possible to add setup and teardown for an entire suite.

kristofferjansson avatar Feb 09 '24 13:02 kristofferjansson

Review requested:

  • [ ] @nodejs/test_runner

nodejs-github-bot avatar Feb 09 '24 13:02 nodejs-github-bot

I'm -1 on adding others hooks, it should already be possible to register hooks at a different scope. Worst case pass an argument to the hook callback

marco-ippolito avatar Feb 09 '24 14:02 marco-ippolito

This is a proposal to add suite hooks to the test runner. Makes it possible to add setup and teardown for an entire suite.

Hey, thanks for the PR! What's the use case for this?

benjamingr avatar Feb 10 '24 22:02 benjamingr

This is a proposal to add suite hooks to the test runner. Makes it possible to add setup and teardown for an entire suite.

Hey, thanks for the PR! What's the use case for this?

Hi! My specific use case is to be able to write tests with BDD syntax using the node:test module. This is possible by wrapping the describe and it functions to get a BDD-interface and write tests like this:

Feature("Returns and exchanges go to inventory.", () => {
  Scenario("1: Items returned for refund should be added to inventory.", () => {
    Given("that a customer previously bought a black sweater from me", () => {});
    and("I have three black sweaters in inventory,", () => {});
    when("they return the black sweater for a refund,", () => {});
    then("I should have four black sweaters in inventory.", () => {});
  });
  Scenario("2: Exchanged items should be returned to inventory.", () => {
    Given("that a customer previously bought a blue garment from me", () => {});
    and("I have two blue garments in inventory", () => {});
    and("three black garments in inventory,", () => {});
    when("they exchange the blue garment for a black garment,", () => {});
    then("I should have three blue garments in inventory", () => {});
    and("two black garments in inventory.", () => {});
  });
});

With a test setup like this it is very common to have the need of setup and teardown code that should run before and after each Scenario. This would be the before/afterEachScenario in mocha-cakes-2 which is a BDD integration for the Mocha testing framework.

I could not find a convenient way to get this behaviour with the current hooks (if there is one I'm very interested to hear more about it 🙏 😃 ). So I added this proposal of suite hooks which makes it possible.

kristofferjansson avatar Feb 11 '24 06:02 kristofferjansson

@benjamingr - Any further thoughts on this?

kristofferjansson avatar Feb 19 '24 13:02 kristofferjansson

I'm -1 on adding others hooks, it should already be possible to register hooks at a different scope. Worst case pass an argument to the hook callback

Hi! I'm interested in this, how would one do this? I can't seem to figure out how. I want to implement something similar to step() in the playwright test runner, i.e. a smaller unit of a test that should not have it's own setup/teardown.

markusn avatar Mar 11 '24 12:03 markusn

I think we can probably close this. We already have suites, tests, and steps (t.test()), and we have all of the common hooks at both the suite and test level. Besides that, the code here looks incorrect and now has merge conflicts.

cjihrig avatar Mar 24 '24 14:03 cjihrig

Closing per the previous comment, but thanks for the PR.

cjihrig avatar Mar 28 '24 16:03 cjihrig