Skip to content

Nested beforeEach does not execute #11

Description

@hazzard993

One other case with before eaches!

A given-when-then-style test:

suite(function() {
  describe("given", function() {
    a = [];

    beforeEach(function() {
      array_push(a, 1);
    });

    describe("when", function() {
      beforeEach(function() {
        array_push(a, 2);
      });

      it("then", function() {
        expect(a).toBeEqual([1, 2])
      });
    });
  });
});
── given
		└── when
				├── ❌ then (0.12ms)
				│ 	├── On file "Script1" (line 41):
				│ 	├── > expect([ 2 ]).toBeEqual([ 1,2 ]):
				│ 	├── - Expected Result: [ 1,2 ]
				│ 	└── - Received Result: [ 2 ]

Here's the jest equivalent:

describe('given', function () {
  let a = [];

  beforeEach(function () {
    a.push(1);
  });

  describe('when', function () {
    beforeEach(function () {
      a.push(2);
    });

    it('then', function () {
      expect(a).toStrictEqual([1, 2]);
    });
  });
});

Might be best as a separate issue but sharing variables between these function calls would also be great! vitest (a jest-like test framework) actually passes a context object which is unique per test so variables can be shared across these code blocks like this:

import { describe, beforeEach, it, expect } from "vitest";

describe("given a new array a", function() {
  beforeEach(function(context) {
    context.a = [];
  });

  describe("when 1 is added to a", function() {
    beforeEach(function(context) {
      context.a.push(1);
    });

    it("then array a is [1]", function(context) {
      expect(context.a).toStrictEqual([1])
    });
  });
});

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions