Skip to content

fix(expect,testing,internal): throw if expect.hasAssertion and expect.assertions are not checked #6646

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

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 160 additions & 44 deletions expect/_assertions_test.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,180 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { describe, it, test } from "@std/testing/bdd";
import { expect } from "./expect.ts";

Deno.test("expect.hasAssertions() API", () => {
describe("describe suite", () => {
// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot`
it("should throw an error", () => {
expect.hasAssertions();
});
import * as path from "@std/path";
import { assertStringIncludes } from "@std/assert";
import { stripAnsiCode } from "@std/internal/styles";

it("should pass", () => {
expect.hasAssertions();
expect("a").toEqual("a");
});
});

it("it() suite should pass", () => {
expect.hasAssertions();
expect("a").toEqual("a");
Deno.test("expect.hasAssertions() API", async () => {
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_has_assertions_",
});
try {
const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts");
await Deno.writeTextFile(
tempFilePath,
`import { describe, it, test } from "@std/testing/bdd";
import { expect } from "@std/expect";

// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot`
test("test suite should throw an error", () => {
describe("describe suite", () => {
it("describe should throw an error", () => {
expect.hasAssertions();
});

test("test suite should pass", () => {
it("describe should pass", () => {
expect.hasAssertions();
expect("a").toEqual("a");
});
});

Deno.test("expect.assertions() API", () => {
test("should pass", () => {
expect.assertions(2);
expect("a").not.toBe("b");
expect("a").toBe("a");
});
it("it() suite should throw an error", () => {
expect.hasAssertions();
});

// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot`
test("should throw error", () => {
expect.assertions(1);
expect("a").not.toBe("b");
expect("a").toBe("a");
});
it("it() suite should pass", () => {
expect.hasAssertions();
expect("a").toEqual("a");
});

it("redeclare different assertion count", () => {
expect.assertions(3);
expect("a").not.toBe("b");
expect("a").toBe("a");
expect.assertions(2);
});
test("test suite should throw an error", () => {
expect.hasAssertions();
});

test("test suite should pass", () => {
expect.hasAssertions();
expect("a").toEqual("a");
});
`,
);

const command = new Deno.Command(Deno.execPath(), {
args: ["test", "--no-lock", tempDirPath],
});
const { stdout } = await command.output();
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout));

assertStringIncludes(
errorMessage,
"describe should throw an error ... FAILED",
);
assertStringIncludes(errorMessage, "describe should pass ... ok");
assertStringIncludes(
errorMessage,
"it() suite should throw an error ... FAILED",
);
assertStringIncludes(errorMessage, "it() suite should pass ... ok");
assertStringIncludes(
errorMessage,
"test suite should throw an error ... FAILED",
);
assertStringIncludes(errorMessage, "test suite should pass ... ok");

test("expect no assertions", () => {
expect.assertions(0);
assertStringIncludes(
errorMessage,
"error: AssertionError: Expected at least one assertion to be called but received none",
);
} finally {
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("expect.assertions() API", async () => {
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_has_assertions_",
});
try {
const tempFilePath = path.join(tempDirPath, "has_assertions_test.ts");
await Deno.writeTextFile(
tempFilePath,
`import { describe, it, test } from "@std/testing/bdd";
import { expect } from "@std/expect";

// FIXME(eryue0220): This test should through `toThrowErrorMatchingSnapshot`
it("should throw an error", () => {
expect.assertions(2);
test("should pass", () => {
expect.assertions(2);
expect("a").not.toBe("b");
expect("a").toBe("a");
});

test("should throw error", () => {
expect.assertions(1);
expect("a").not.toBe("b");
expect("a").toBe("a");
});

it("redeclare different assertion count", () => {
expect.assertions(3);
expect("a").not.toBe("b");
expect("a").toBe("a");
expect.assertions(2);
});

test("expect no assertions", () => {
expect.assertions(0);
});

it("should throw an error", () => {
expect.assertions(2);
});
`,
);

const command = new Deno.Command(Deno.execPath(), {
args: ["test", "--no-lock", tempDirPath],
});
const { stdout } = await command.output();
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout));

assertStringIncludes(errorMessage, "should pass ... ok");
assertStringIncludes(errorMessage, "should throw error ... FAILED");
assertStringIncludes(
errorMessage,
"redeclare different assertion count ... ok",
);
assertStringIncludes(errorMessage, "expect no assertions ... ok");
assertStringIncludes(errorMessage, "should throw an error ... FAILED");

assertStringIncludes(
errorMessage,
"error: AssertionError: Expected exactly 1 assertion to be called, but received 2",
);
assertStringIncludes(
errorMessage,
"error: AssertionError: Expected exactly 2 assertions to be called, but received 0",
);
} finally {
await Deno.remove(tempDirPath, { recursive: true });
}
});

Deno.test("expect assertions reset after errored tests", async () => {
const tempDirPath = await Deno.makeTempDir({
prefix: "deno_std_assertions_reset_",
});
try {
const tempFilePath = path.join(tempDirPath, "assertion_reset_test.ts");
await Deno.writeTextFile(
tempFilePath,
`import { it } from "@std/testing/bdd";
import { expect } from "@std/expect";
it("should fail", () => {
expect.assertions(2);
expect(1).toBe(1);
});
it("should pass", () => {
expect.assertions(0);
});
`,
);

const command = new Deno.Command(Deno.execPath(), {
args: ["test", "--no-lock", tempDirPath],
});
const { stdout } = await command.output();
const errorMessage = stripAnsiCode(new TextDecoder().decode(stdout));

assertStringIncludes(errorMessage, "should fail ... FAILED");
// Previously "should fail" failing caused "should pass" to fail
assertStringIncludes(errorMessage, "should pass ... ok");
} finally {
await Deno.remove(tempDirPath, { recursive: true });
}
});
14 changes: 14 additions & 0 deletions internal/assertion_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
assertionTriggered: false,
assertionTriggeredCount: 0,
};

// If any checks were registered, after the test suite runs the checks,
// `resetAssertionState` should also have been called. If it was not,
// then the test suite did not run the checks.
globalThis.addEventListener("unload", () => {
if (
this.#state.assertionCheck ||
this.#state.assertionCount !== undefined

Check warning on line 36 in internal/assertion_state.ts

View check run for this annotation

Codecov / codecov/patch

internal/assertion_state.ts#L34-L36

Added lines #L34 - L36 were not covered by tests
) {
throw new Error(
"AssertionCounter was not cleaned up: If tests are not otherwise failing, ensure `expect.hasAssertion` and `expect.assertions` are only run in bdd tests",

Check warning on line 39 in internal/assertion_state.ts

View check run for this annotation

Codecov / codecov/patch

internal/assertion_state.ts#L38-L39

Added lines #L38 - L39 were not covered by tests
);
}

Check warning on line 41 in internal/assertion_state.ts

View check run for this annotation

Codecov / codecov/patch

internal/assertion_state.ts#L41

Added line #L41 was not covered by tests
});
}

/**
Expand Down
48 changes: 38 additions & 10 deletions internal/assertion_state_test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
// Copyright 2018-2025 the Deno authors. MIT license.

import { assertEquals } from "@std/assert";
import { assertEquals, assertStringIncludes } from "@std/assert";
import { AssertionState } from "./assertion_state.ts";
import { stripAnsiCode } from "./styles.ts";

Deno.test("AssertionState checkAssertionErrorState pass", () => {
const assertionState = new AssertionState();
assertionState.setAssertionTriggered(true);

assertEquals(assertionState.checkAssertionErrorState(), false);
try {
assertionState.setAssertionTriggered(true);
assertEquals(assertionState.checkAssertionErrorState(), false);
} finally {
assertionState.resetAssertionState();
}
});

Deno.test("AssertionState checkAssertionErrorState pass", () => {
const assertionState = new AssertionState();
assertionState.setAssertionTriggered(true);
try {
assertionState.setAssertionTriggered(true);

assertEquals(assertionState.checkAssertionErrorState(), false);
assertEquals(assertionState.checkAssertionErrorState(), false);

assertionState.setAssertionCheck(true);
assertEquals(assertionState.checkAssertionErrorState(), false);
assertionState.setAssertionCheck(true);
assertEquals(assertionState.checkAssertionErrorState(), false);
} finally {
assertionState.resetAssertionState();
}
});

Deno.test("AssertionState checkAssertionErrorState fail", () => {
const assertionState = new AssertionState();
assertionState.setAssertionCheck(true);
try {
assertionState.setAssertionCheck(true);
assertEquals(assertionState.checkAssertionErrorState(), true);
} finally {
assertionState.resetAssertionState();
}
});

assertEquals(assertionState.checkAssertionErrorState(), true);
Deno.test("AssertionState throws if not cleaned up", async () => {
const command = new Deno.Command(Deno.execPath(), {
args: [
"eval",
`
import { AssertionState } from "@std/internal/assertion-state";
const assertionState = new AssertionState();
assertionState.setAssertionCount(0);
`,
],
});
const { stderr } = await command.output();
const errorMessage = stripAnsiCode(new TextDecoder().decode(stderr));
// TODO(WWRS): Test for the expected message when Deno displays it instead of "Uncaught null"
assertStringIncludes(errorMessage, "error: Uncaught");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this supposed to be the message generated in unload handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this is an upstream issue, which I've opened: denoland/deno#29087

});
28 changes: 16 additions & 12 deletions testing/_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,19 +429,23 @@
await fn.call(context, t);
}

if (assertionState.checkAssertionErrorState()) {
throw new AssertionError(
"Expected at least one assertion to be called but received none",
);
}
try {
if (assertionState.checkAssertionErrorState()) {
throw new AssertionError(
"Expected at least one assertion to be called but received none",
);
}

if (assertionState.checkAssertionCountSatisfied()) {
throw new AssertionError(
`Expected at least ${assertionState.assertionCount} assertion to be called, ` +
`but received ${assertionState.assertionTriggeredCount}`,
);
if (assertionState.checkAssertionCountSatisfied()) {
throw new AssertionError(
`Expected exactly ${assertionState.assertionCount} ${
assertionState.assertionCount === 1 ? "assertion" : "assertions"
} to be called, ` +
`but received ${assertionState.assertionTriggeredCount}`,

Check warning on line 444 in testing/_test_suite.ts

View check run for this annotation

Codecov / codecov/patch

testing/_test_suite.ts#L440-L444

Added lines #L440 - L444 were not covered by tests
);
}

Check warning on line 446 in testing/_test_suite.ts

View check run for this annotation

Codecov / codecov/patch

testing/_test_suite.ts#L446

Added line #L446 was not covered by tests
} finally {
assertionState.resetAssertionState();
}

assertionState.resetAssertionState();
}
}
28 changes: 16 additions & 12 deletions testing/bdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,20 +600,24 @@ export function it<T>(...args: ItArgs<T>) {
TestSuiteInternal.runningCount--;
}

if (assertionState.checkAssertionErrorState()) {
throw new AssertionError(
"Expected at least one assertion to be called but received none",
);
}
try {
if (assertionState.checkAssertionErrorState()) {
throw new AssertionError(
"Expected at least one assertion to be called but received none",
);
}

if (assertionState.checkAssertionCountSatisfied()) {
throw new AssertionError(
`Expected at least ${assertionState.assertionCount} assertion to be called, ` +
`but received ${assertionState.assertionTriggeredCount}`,
);
if (assertionState.checkAssertionCountSatisfied()) {
throw new AssertionError(
`Expected exactly ${assertionState.assertionCount} ${
assertionState.assertionCount === 1 ? "assertion" : "assertions"
} to be called, ` +
`but received ${assertionState.assertionTriggeredCount}`,
);
}
} finally {
assertionState.resetAssertionState();
}

assertionState.resetAssertionState();
},
};
if (ignore !== undefined) {
Expand Down
Loading