bun icon indicating copy to clipboard operation
bun copied to clipboard

`afterAll` does not run

Open Electroid opened this issue 2 years ago • 6 comments

import { afterAll } from "bun:test"; 

// ...
afterAll(() => {
  console.log("Hi");
});

The code in the afterAll block does not run, under any circumstance.

Electroid avatar Feb 21 '23 00:02 Electroid

Works for me:

--- a/test/bun.js/install/dummy.registry.ts
+++ b/test/bun.js/install/dummy.registry.ts
@@ -71,6 +71,7 @@ export function dummyBeforeAll() {
 }
 
 export function dummyAfterAll() {
+  console.error("AFTER ALL");
   server.stop();
 }
$ bun wiptest bun-install
bun wiptest v0.5.7 (155e3706)

bun-install.test.ts:
✓ should handle missing package
✓ should handle @scoped authentication
✓ should handle empty string in dependencies
✓ should handle workspaces
✓ should handle workspaces with packages array
✓ should handle inter-dependency between workspaces
✓ should handle inter-dependency between workspaces (devDependencies)
✓ should handle inter-dependency between workspaces (optionalDependencies)
✓ should ignore peerDependencies within workspaces
✓ should handle life-cycle scripts within workspaces
✓ should ignore workspaces within workspaces
✓ should handle ^0 in dependencies
✓ should handle ^1 in dependencies
✓ should handle ^0.0 in dependencies
✓ should handle ^0.1 in dependencies
✓ should handle ^0.0.0 in dependencies
✓ should handle ^0.0.2 in dependencies
✓ should handle ^0.0.2-rc in dependencies
✓ should handle ^0.0.2-alpha.3+b4d in dependencies
✓ should handle dependency aliasing
✓ should handle dependency aliasing (versioned)
✓ should handle dependency aliasing (dist-tagged)
✓ should not reinstall aliased dependencies
✓ should handle aliased & direct dependency references
✓ should not hoist if name collides with alias
✓ should handle GitHub URL in dependencies (user/repo)
✓ should handle GitHub URL in dependencies (user/repo#commit-id)
✓ should handle GitHub URL in dependencies (user/repo#tag)
✓ should handle GitHub URL in dependencies (github:user/repo#tag)
✓ should handle GitHub URL in dependencies (https://github.com/user/repo.git)
✓ should handle GitHub URL in dependencies (git://github.com/user/repo.git#commit)
✓ should handle GitHub URL in dependencies (git+https://github.com/user/repo.git)
✓ should consider peerDependencies during hoisting
✓ should not regard peerDependencies declarations as duplicates
✓ should report error on invalid format for package.json
✓ should report error on invalid format for dependencies
✓ should report error on invalid format for optionalDependencies
✓ should report error on invalid format for workspaces
✓ should report error on duplicated workspace packages
✓ should handle Git URL in dependencies
✓ should handle Git URL with committish in dependencies
✓ should fail on invalid Git URL
✓ should fail on Git URL with invalid committish
✓ should de-duplicate committish in Git URLs
AFTER ALL

 44 pass
 0 fail
 569 expect() calls
Ran 44 tests across 1 files [21.38s]

alexlamsl avatar Feb 21 '23 04:02 alexlamsl

There might be something else involved, but here's my repro:

import { afterAll, beforeAll, describe, test, expect } from "bun:test";
import { spawnSync, which } from "bun";

let containerName = `mysql-test-${Date.now()}`;
let containerId: string | undefined;
let containerUrl = new URL("tcp://localhost:3306/");

beforeAll(async () => {
  if (!which("docker")) {
    throw new Error("Command not found: docker");
  }
  const { exitCode } = spawnSync({
    // prettier-ignore
    cmd: [
      "docker", "run",
      "-e", "MYSQL_DATABASE=database",
      "-e", "MYSQL_USER=user",
      "-e", "MYSQL_PASSWORD=password",
      "-e", "MYSQL_ROOT_PASSWORD=password",
      "-p", "3306",
      "--name", containerName,
      "--detach",
      "mysql:8.0.32",
      "--skip-ssl"
    ],
    stdout: "inherit",
    stderr: "inherit",
  });
  if (exitCode !== 0) {
    throw new Error(`Failed to start MySQL container: ${exitCode}`);
  }
  {
    const { stdout } = spawnSync({
      cmd: ["docker", "ps", "-aqf", `name=${containerName}`],
    });
    containerId = stdout?.toString("utf-8")?.trim();
  }
  {
    const { stdout } = spawnSync({
      cmd: ["docker", "port", containerName, "3306/tcp"],
    });
    containerUrl = new URL(`tcp://${stdout?.toString("utf-8")?.trim()}`);
  }
  await Bun.sleep(5000);
});

afterAll(() => {
  console.log("HELLO");
  try {
    if (!containerId) {
      throw new Error("Failed to find MySQL container (resources may have leaked)");
    }
    const { exitCode } = spawnSync({
      cmd: ["docker", "rm", "-f", containerId]
    });
    if (exitCode !== 0) {
      throw new Error(`Failed to stop MySQL container: ${exitCode}`);
    }
  } catch (error) {
    console.log(error);
  }
});

describe("Test", () => {
  test("Another test", () => {
    expect(1).toBe(1);
  });
});
bun wiptest v0.5.7 (bcb7be49)
[0.49ms] ".env"

mysql.test.ts:
0befffbc8f8bbb32b7f1ec8abff22bfddd903517d7047a209367c8904a7670fd
✓ Test > Another test

 1 pass
 0 fail
 1 expect() calls
Ran 1 tests across 1 files [5.41s]

Electroid avatar Feb 21 '23 06:02 Electroid

Is the beforeAll possibly throwing?

Jarred-Sumner avatar Feb 21 '23 07:02 Jarred-Sumner

Oh, it’s because of how spawnSync is implemented. It’s not fully sync. Some things still may happen.

Jarred-Sumner avatar Feb 21 '23 07:02 Jarred-Sumner

Though maybe we need to change that

Jarred-Sumner avatar Feb 21 '23 07:02 Jarred-Sumner

Wrapped both beforeAll and afterAll in try-catch. beforeAll doesn't throw. When I add a console.log at the start/end of the afterAll, neither run.

Electroid avatar Feb 21 '23 17:02 Electroid

This was fixed.

Electroid avatar May 26 '23 19:05 Electroid