Skip to content

Adding 'before', 'after', 'beforeEach', 'afterEach', functions to the Test Runner #43403

Description

@yaircohendev

What is the problem this feature will solve?

Ability to write preparation and cleanup code

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

Hey,

I'd like to work on adding before, after, beforeEach, afterEach functions to the test runner.
@benjamingr
@cjihrig

What alternatives have you considered?

No response

Activity

  1. benjamingr commented on Jun 13, 2022

    @benjamingr
    Member

    Yay 🔥

  2. MoLow commented on Jun 29, 2022

    @MoLow
    Member

    Hi @yaircohendev where does this stand?

  3. yaircohendev commented on Jul 9, 2022

    @yaircohendev
    Author

    Hey @MoLow I wrote the initial code for this but having problems with debugging locally, any chance to setup a quick call about this?
    Sorry for late response btw, was not getting notifications and only saw this now.

  4. MoLow commented on Jul 9, 2022

    @MoLow
    Member

    Hey @MoLow I wrote the initial code for this but having problems with debugging locally, any chance to setup a quick call about this? Sorry for late response btw, was not getting notifications and only saw this now.

    Sure I assume we are both in Israel so we can work on this together. Ping me

  5. reopened this on Jul 21, 2022
  6. salihdev0 commented on Jul 30, 2022

    @salihdev0

    right now i'm doing it like this

    "use-strict";
    const assert = require("node:assert");
    const test = require("node:test");
    const { server, app } = require("./app.js");
    
    test("HTTP e2e Stack Testing", async (t) => {
    
      // beforeAll
      await t.test("create connection before tests", async () => {
        return new Promise((resolve, reject) => {
          server.onServerStarted(() => {
            resolve();
          });
          app.start();
        });
      });
    
      // my tests here
      t.test("any testing", () => {
          assert.strickEqual(1, 1)
      })
    
      // afterAll
      server.instance.close();
    });
  7. salihdev0 commented on Jul 30, 2022

    @salihdev0

    But this works in single file. If I use a common source from 2 different files beforeAll doesn't work probably because nodejs runs the files on the fly. They do not wait for each other. Therefore, a global beforeAll and afterAll methods must be provided. Unfortunately, this is a mandatory requirement for memory db connections and http server installations.

  8. added a commit that references this issue on Jul 30, 2022
  9. added a commit that references this issue on Sep 5, 2022
  10. SynLocker commented on Apr 20, 2023

    @SynLocker

    any update about this?

  11. MoLow commented on Apr 20, 2023

    @MoLow
    Member

    @SynLocker this exists in node18 and above

  12. isaacgr commented on Aug 3, 2023

    @isaacgr

    Is it expected that these run regardless of if the test is skipped? So if a test is being skipped the 'beforeEach' call still runs before that test is officially skipped.

  13. ericminio commented on Aug 29, 2023

    @ericminio

    Illustrating the question from @isaacgr

    The code below is green with node 18.17.1.

    const { describe, it, beforeEach } = require('node:test');
    const { strict: assert } = require('node:assert');
    
    describe('skipping test', () => {
        let spy = 0;
        beforeEach(() => {
            spy += 1;
        });
    
        it.skip('tries to leak', () => {
            spy += 1;
        });
    
        it('suffers from leak', () => {
            spy += 1;
            assert.equal(spy, 3);
        });
    });
    
    
  14. listvin commented on Nov 26, 2023

    @listvin

    beforeEach and afterEach should not run for skipped tests. I believe it's a bug.
    As mentioned by @isaacgr and illustrated by @ericminio.
    Following might illustrate better.

    test1 fails with assert message:

       >> beforeEach for 'test0 WILL NOT/HAS NOT RUN'
       >> afterEach for 'test0 WILL NOT/HAS NOT RUN'
       >> beforeEach for 'test1'
       >> it test1
    

    while expected log is:

       >> beforeEach for 'test1'
       >> it test1
    

    test itself:

    const { beforeEach, test, afterEach } = require('node:test');
    const assert = require('node:assert');
    
    const log = [];
    beforeEach((sc) => {
        log.push(`beforeEach for '${sc.name}'`);
    });
    
    test.skip('test0 WILL NOT/HAS NOT RUN', (sc) => {
        log.push(`it ${sc.name}`); //as expected it didn't run
    });
    
    test('test1', (sc) => {
        log.push(`it ${sc.name}`);
        assert.equal(log.length, 2, "call log:\n >> " + log.join('\n >> '));
    });
    
    afterEach((sc) => {
        log.push(`afterEach for '${sc.name}'`);
    });

    Node v20.10.0. Same thing happens within describe. Skipping with --test-name-pattern gives the same also.

    @benjamingr, @MoLow, any comments? It it expected behaviour? Should I open a new issue or can this one be reopened?

  15. louwers commented on May 26, 2024

    @louwers
    Contributor

    @listvin This seems to be fixed now.

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