OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

js node test use before outside of describe

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Guest
as you can see from the before image, out of the describe it is not called, but all the others are.

There's a reason or it doesn't work, that's it.

If I put it in describe it works.

enter image description here

Code:
import {
  before,
  beforeEach,
  after,
  afterEach,
  describe,
  it,
  test,
} from "node:test";
import assert from "node:assert";

import { add, subtract } from "../index.js";

before(function () {
  console.log("before");
});
beforeEach(function () {
  console.log("beforeEach");
});
afterEach(function () {
  console.log("afterEach");
});
after(function () {
  console.log("after");
});

describe("Arithmetic Operations", () => {
  it("should correctly add two numbers", () => {
    const result = add(2, 3);
    assert.strictEqual(result, 5);
  });

  it("should correctly subtract two numbers", () => {
    const result = subtract(5, 3);
    assert.strictEqual(result, 2);
  });
});

<p>as you can see from the before image, out of the describe it is not called, but all the others are.</p>
<p>There's a reason or it doesn't work, that's it.</p>
<p>If I put it in describe it works.</p>
<p><a href="https://i.sstatic.net/M6AQH2Ap.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/M6AQH2Ap.png" alt="enter image description here" /></a></p>
<pre class="lang-js prettyprint-override"><code>import {
before,
beforeEach,
after,
afterEach,
describe,
it,
test,
} from "node:test";
import assert from "node:assert";

import { add, subtract } from "../index.js";

before(function () {
console.log("before");
});
beforeEach(function () {
console.log("beforeEach");
});
afterEach(function () {
console.log("afterEach");
});
after(function () {
console.log("after");
});

describe("Arithmetic Operations", () => {
it("should correctly add two numbers", () => {
const result = add(2, 3);
assert.strictEqual(result, 5);
});

it("should correctly subtract two numbers", () => {
const result = subtract(5, 3);
assert.strictEqual(result, 2);
});
});
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
2
Total visitors
2
Top