Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterAll isn't triggered if there is no test at the same level #3494

Closed
riywo opened this issue Jul 2, 2023 · 4 comments · Fixed by #3663
Closed

afterAll isn't triggered if there is no test at the same level #3494

riywo opened this issue Jul 2, 2023 · 4 comments · Fixed by #3663
Assignees
Labels
bug Something isn't working bun:test Something related to the `bun test` runner

Comments

@riywo
Copy link

riywo commented Jul 2, 2023

What version of Bun is running?

0.6.12

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 unknown

What steps can reproduce the bug?

Run bun test with the test code below:

describe("without test at the same level", () => {
  afterAll(() => console.log("afterAll without test at the same level"));

  describe("a", () => {
    test("b", () => {
      expect(true).toBe(true);
    });
  });
});

describe("with test at the same level", () => {
  afterAll(() => console.log("afterAll with test at the same level"));

  describe("a", () => {
    test("b", () => {
      expect(true).toBe(true);
    });
  });

  test("c", () => {
    expect(true).toBe(true);
  });
});

What is the expected behavior?

Both afterAll are executed:

bun test v0.6.12 (039bbc68)

test/index.test.ts:
✓ without test at the same level > a > b [0.05ms]
✓ with test at the same level > a > b [0.01ms]
✓ with test at the same level > c [0.02ms]
afterAll without test at the same level
afterAll with test at the same level

Note: This is the Jest's behavior:

  console.log
    afterAll without test at the same level

      at Object.<anonymous> (test/index.test.ts:24:26)

  console.log
    afterAll with test at the same level

      at Object.<anonymous> (test/index.test.ts:34:26)

 PASS  test/index.test.ts
  without test at the same level
    a
      ✓ b (1 ms)
  with test at the same level
    ✓ c
    a
      ✓ b

What do you see instead?

No afterAll without test at the same level:

bun test v0.6.12 (039bbc68)

test/index.test.ts:
✓ without test at the same level > a > b [0.05ms]
✓ with test at the same level > a > b [0.01ms]
✓ with test at the same level > c [0.02ms]
afterAll with test at the same level

Additional information

The workaround is adding a test at the same level:

  afterAll(() => console.log("afterAll without test at the same level"));

  // Without any `test` on the same level of `afterAll`, it isn't triggered.
  // Workaround to trigger it. https://github.com/oven-sh/bun/issues/3494
  test("Workaround to run afterAll", () => undefined);
@riywo riywo added the bug Something isn't working label Jul 2, 2023
@TiBianMod
Copy link

yep, the afterAll is not working as top level lifecycle hook, it works only as scoped lifecycle hook 🥲

import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test";

beforeAll(() => {
    console.log("lifecycle hook: beforeAll");
});

beforeEach(() => {
    console.log("lifecycle hook: beforeEach");
});

afterAll(() => {
    console.log("lifecycle hook: afterAll");
});

afterEach(() => {
    console.log("lifecycle hook: afterEach");
});

describe("start testing", () => {
    afterAll(() => {
        console.log("lifecycle hook: afterAll scoped");
    });

    test("dummy", () => {
        expect(1).toEqual(1);
    });
});

output

bun test v0.6.12 (039bbc68)

lifecycle_hooks.spec.ts:
lifecycle hook: beforeAll
lifecycle hook: beforeEach
✓ start testing > dummy [0.04ms]
lifecycle hook: afterEach
lifecycle hook: afterAll scoped

 1 pass
 0 fail
 1 expect() calls
Ran 1 tests across 1 files. 1 total [10.00ms]

the top level "lifecycle hook: afterAll" is missing from the output

@riywo
Copy link
Author

riywo commented Jul 3, 2023

Thank you for your comment! It misses even nested if there is no test at the same level:

Note: Both beforeAll are called. Only afterAll are missing.

import {
  afterAll,
  afterEach,
  beforeAll,
  beforeEach,
  describe,
  expect,
  test,
} from "bun:test";

beforeAll(() => {
  console.log("lifecycle hook: beforeAll");
});

beforeEach(() => {
  console.log("lifecycle hook: beforeEach");
});

afterAll(() => {
  console.log("lifecycle hook: afterAll");
});

afterEach(() => {
  console.log("lifecycle hook: afterEach");
});

describe("start testing", () => {
  beforeAll(() => {
    console.log("lifecycle hook: beforeAll scoped");
  });

  afterAll(() => {
    console.log("lifecycle hook: afterAll scoped");
  });

  describe("nest", () => {
    test("dummy", () => {
      expect(1).toEqual(1);
    });
  });
});
bun test v0.6.12 (039bbc68)

test/indext.test.ts:
lifecycle hook: beforeAll scoped
lifecycle hook: beforeAll
lifecycle hook: beforeEach
✓ start testing > nest > dummy [0.05ms]
lifecycle hook: afterEach

 1 pass
 0 fail
 1 expect() calls
Ran 1 tests across 1 files. 1 total [10.00ms]

This pair of beforeAll and afterAll is useful when users want to establish connection to DB before all tests and disconnect it after all tests (especially for some clients that keep running JS runtime until it's disconnected).

@Electroid Electroid added the bun:test Something related to the `bun test` runner label Jul 3, 2023
@xavikortes
Copy link

Is it in progress? I'm very interested in this issue. My tests get stucked after the first suite execution because of the lack of afterAll

@riywo
Copy link
Author

riywo commented Jul 24, 2023

Confirmed that 0.7.0 fixed my original issue. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun:test Something related to the `bun test` runner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants