Repository navigation
Adding 'before', 'after', 'beforeEach', 'afterEach', functions to the Test Runner #43403
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Jun 13, 2022 - addedtest_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on Jun 13, 2022 Yay 🔥
Hi @yaircohendev where does this stand?
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.Reacted by Moshe Atlow and Erick WendelHey @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
Reacted by Yair Cohen and Erick Wendelright 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(); });
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.
- added a commit that references this issue
on Jul 30, 2022 - added a commit that references this issue
on Aug 16, 2022 - added a commit that references this issue
on Aug 23, 2022 - added a commit that references this issue
on Sep 5, 2022 - added 2 commits that reference this issue
on Jan 3, 2023 any update about this?
@SynLocker this exists in node18 and above
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.
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); }); });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 test1while expected
logis:>> beforeEach for 'test1' >> it test1test 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?
Reacted by Bray Almini- added a commit that references this issue
on Mar 21, 2024 @listvin This seems to be fixed now.
Reacted by listvin
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,afterEachfunctions to the test runner.@benjamingr
@cjihrig
What alternatives have you considered?
No response