Skip to content

Commit

Permalink
Improve empty test case error messages (denoland#3514)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k authored and ry committed Dec 18, 2019
1 parent bb24fb7 commit ff6b514
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
23 changes: 19 additions & 4 deletions std/testing/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
yellow,
italic
} from "../fmt/colors.ts";
import { assert } from "./asserts.ts";
export type TestFunction = () => void | Promise<void>;

export interface TestDefinition {
Expand Down Expand Up @@ -121,14 +122,28 @@ export function test(
throw new Error("Missing test function");
}
name = t;
if (!name) {
throw new Error("The name of test case can't be empty");
}
} else if (typeof t === "function") {
fn = t;
name = t.name;
if (!name) {
throw new Error("Test function can't be anonymous");
}
} else {
fn = typeof t === "function" ? t : t.fn;
fn = t.fn;
if (!fn) {
throw new Error("Missing test function");
}
name = t.name;
if (!name) {
throw new Error("The name of test case can't be empty");
}
}
assert(!!name, "The name of test case shouldn't be empty");
assert(!!fn, "Test function shouldn't be empty");

if (!name) {
throw new Error("Test function may not be anonymous");
}
if (filter(name)) {
candidates.push({ fn, name });
} else {
Expand Down
30 changes: 30 additions & 0 deletions std/testing/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,34 @@ test("test fn overloading", (): void => {
assert(true);
});

test("The name of test case can't be empty", () => {
assertThrows(
() => {
test("", () => {});
},
Error,
"The name of test case can't be empty"
);
assertThrows(
() => {
test({
name: "",
fn: () => {}
});
},
Error,
"The name of test case can't be empty"
);
});

test("test function can't be anonymous", () => {
assertThrows(
() => {
test(function() {});
},
Error,
"Test function can't be anonymous"
);
});

runIfMain(import.meta);

0 comments on commit ff6b514

Please sign in to comment.