Skip to content

Commit

Permalink
BREAKING: remove overload of Deno.test() (#4951)
Browse files Browse the repository at this point in the history
This commit removes overload of Deno.test() that accepted named
function.
  • Loading branch information
bartlomieju authored Apr 28, 2020
1 parent b508e84 commit 8feb30e
Show file tree
Hide file tree
Showing 69 changed files with 456 additions and 481 deletions.
18 changes: 0 additions & 18 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ declare namespace Deno {
*/
export function test(t: TestDefinition): void;

/** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module.
* `fn` can be async if required.
*
* import {assert, fail, assertEquals} from "https://deno.land/std/testing/asserts.ts";
*
* Deno.test(function myTestFunction():void {
* assertEquals("hello", "hello");
* });
*
* Deno.test(async function myAsyncTestFunction():Promise<void> {
* const decoder = new TextDecoder("utf-8");
* const data = await Deno.readFile("hello_world.txt");
* assertEquals(decoder.decode(data), "Hello world")
* });
**/
export function test(fn: () => void | Promise<void>): void;

/** Register a test which will be run when `deno test` is used on the command
* line and the containing module looks like a test module.
* `fn` can be async if required.
Expand Down
8 changes: 1 addition & 7 deletions cli/js/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@ export interface TestDefinition {
const TEST_REGISTRY: TestDefinition[] = [];

export function test(t: TestDefinition): void;
export function test(fn: () => void | Promise<void>): void;
export function test(name: string, fn: () => void | Promise<void>): void;
// Main test function provided by Deno, as you can see it merely
// creates a new object with "name" and "fn" fields.
export function test(
t: string | TestDefinition | (() => void | Promise<void>),
t: string | TestDefinition,
fn?: () => void | Promise<void>
): void {
let testDef: TestDefinition;
Expand All @@ -116,11 +115,6 @@ export function test(
throw new TypeError("The test name can't be empty");
}
testDef = { fn: fn as () => void | Promise<void>, name: t, ...defaults };
} else if (typeof t === "function") {
if (!t.name) {
throw new TypeError("The test function can't be anonymous");
}
testDef = { fn: t, name: t.name, ...defaults };
} else {
if (!t.fn) {
throw new TypeError("Missing test function");
Expand Down
10 changes: 0 additions & 10 deletions cli/js/tests/testing_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,3 @@ unitTest(function nameOfTestCaseCantBeEmpty(): void {
"The test name can't be empty"
);
});

unitTest(function testFnCantBeAnonymous(): void {
assertThrows(
() => {
Deno.test(function () {});
},
TypeError,
"The test function can't be anonymous"
);
});
10 changes: 5 additions & 5 deletions cli/tests/057_revoke_permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function assert(cond: unknown): asserts cond {
}
}

function genFunc(grant: Deno.PermissionName): () => Promise<void> {
function genFunc(grant: Deno.PermissionName): [string, () => Promise<void>] {
const gen: () => Promise<void> = async function Granted(): Promise<void> {
const status0 = await Deno.permissions.query({ name: grant });
assert(status0 != null);
Expand All @@ -26,11 +26,11 @@ function genFunc(grant: Deno.PermissionName): () => Promise<void> {
assert(status1 != null);
assert(status1.state === "prompt");
};
// Properly name these generated functions.
Object.defineProperty(gen, "name", { value: grant + "Granted" });
return gen;
const name = grant + "Granted";
return [name, gen];
}

for (const grant of knownPermissions) {
Deno.test(genFunc(grant));
const [name, fn] = genFunc(grant);
Deno.test(name, fn);
}
24 changes: 12 additions & 12 deletions cli/tests/compiler_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { assert, assertEquals } from "../../std/testing/asserts.ts";
const { compile, transpileOnly, bundle, test } = Deno;

test(async function compilerApiCompileSources() {
test("compilerApiCompileSources", async function () {
const [diagnostics, actual] = await compile("/foo.ts", {
"/foo.ts": `import * as bar from "./bar.ts";\n\nconsole.log(bar);\n`,
"/bar.ts": `export const bar = "bar";\n`,
Expand All @@ -18,7 +18,7 @@ test(async function compilerApiCompileSources() {
]);
});

test(async function compilerApiCompileNoSources() {
test("compilerApiCompileNoSources", async function () {
const [diagnostics, actual] = await compile("./subdir/mod1.ts");
assert(diagnostics == null);
assert(actual);
Expand All @@ -28,7 +28,7 @@ test(async function compilerApiCompileNoSources() {
assert(keys[1].endsWith("print_hello.js"));
});

test(async function compilerApiCompileOptions() {
test("compilerApiCompileOptions", async function () {
const [diagnostics, actual] = await compile(
"/foo.ts",
{
Expand All @@ -45,7 +45,7 @@ test(async function compilerApiCompileOptions() {
assert(actual["/foo.js"].startsWith("define("));
});

test(async function compilerApiCompileLib() {
test("compilerApiCompileLib", async function () {
const [diagnostics, actual] = await compile(
"/foo.ts",
{
Expand All @@ -61,7 +61,7 @@ test(async function compilerApiCompileLib() {
assertEquals(Object.keys(actual), ["/foo.js.map", "/foo.js"]);
});

test(async function compilerApiCompileTypes() {
test("compilerApiCompileTypes", async function () {
const [diagnostics, actual] = await compile(
"/foo.ts",
{
Expand All @@ -76,7 +76,7 @@ test(async function compilerApiCompileTypes() {
assertEquals(Object.keys(actual), ["/foo.js.map", "/foo.js"]);
});

test(async function transpileOnlyApi() {
test("transpileOnlyApi", async function () {
const actual = await transpileOnly({
"foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
});
Expand All @@ -86,7 +86,7 @@ test(async function transpileOnlyApi() {
assert(actual["foo.ts"].map);
});

test(async function transpileOnlyApiConfig() {
test("transpileOnlyApiConfig", async function () {
const actual = await transpileOnly(
{
"foo.ts": `export enum Foo { Foo, Bar, Baz };\n`,
Expand All @@ -102,7 +102,7 @@ test(async function transpileOnlyApiConfig() {
assert(actual["foo.ts"].map == null);
});

test(async function bundleApiSources() {
test("bundleApiSources", async function () {
const [diagnostics, actual] = await bundle("/foo.ts", {
"/foo.ts": `export * from "./bar.ts";\n`,
"/bar.ts": `export const bar = "bar";\n`,
Expand All @@ -112,14 +112,14 @@ test(async function bundleApiSources() {
assert(actual.includes(`__exp["bar"]`));
});

test(async function bundleApiNoSources() {
test("bundleApiNoSources", async function () {
const [diagnostics, actual] = await bundle("./subdir/mod1.ts");
assert(diagnostics == null);
assert(actual.includes(`__instantiate("mod1")`));
assert(actual.includes(`__exp["printHello3"]`));
});

test(async function bundleApiConfig() {
test("bundleApiConfig", async function () {
const [diagnostics, actual] = await bundle(
"/foo.ts",
{
Expand All @@ -134,7 +134,7 @@ test(async function bundleApiConfig() {
assert(!actual.includes(`random`));
});

test(async function bundleApiJsModules() {
test("bundleApiJsModules", async function () {
const [diagnostics, actual] = await bundle("/foo.js", {
"/foo.js": `export * from "./bar.js";\n`,
"/bar.js": `export const bar = "bar";\n`,
Expand All @@ -143,7 +143,7 @@ test(async function bundleApiJsModules() {
assert(actual.includes(`System.register("bar",`));
});

test(async function diagnosticsTest() {
test("diagnosticsTest", async function () {
const [diagnostics] = await compile("/foo.ts", {
"/foo.ts": `document.getElementById("foo");`,
});
Expand Down
8 changes: 4 additions & 4 deletions cli/tests/test_runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

import { assert } from "../../std/testing/asserts.ts";

Deno.test(function fail1() {
Deno.test("fail1", function () {
assert(false, "fail1 assertion");
});

Deno.test(function fail2() {
Deno.test("fail2", function () {
assert(false, "fail2 assertion");
});

Deno.test(function success1() {
Deno.test("success1", function () {
assert(true);
});

Deno.test(function fail3() {
Deno.test("fail3", function () {
assert(false, "fail3 assertion");
});
8 changes: 5 additions & 3 deletions std/archive/tar_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Tar, Untar } from "./tar.ts";

const filePath = resolve("archive", "testdata", "example.txt");

Deno.test(async function createTarArchive(): Promise<void> {
Deno.test("createTarArchive", async function (): Promise<void> {
// initialize
const tar = new Tar();

Expand All @@ -40,7 +40,7 @@ Deno.test(async function createTarArchive(): Promise<void> {
assertEquals(wrote, 3072);
});

Deno.test(async function deflateTarArchive(): Promise<void> {
Deno.test("deflateTarArchive", async function (): Promise<void> {
const fileName = "output.txt";
const text = "hello tar world!";

Expand All @@ -63,7 +63,9 @@ Deno.test(async function deflateTarArchive(): Promise<void> {
assertEquals(untarText, text);
});

Deno.test(async function appendFileWithLongNameToTarArchive(): Promise<void> {
Deno.test("appendFileWithLongNameToTarArchive", async function (): Promise<
void
> {
// 9 * 15 + 13 = 148 bytes
const fileName = new Array(10).join("long-file-name/") + "file-name.txt";
const text = "hello tar world!";
Expand Down
12 changes: 6 additions & 6 deletions std/datetime/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import * as datetime from "./mod.ts";

Deno.test(function parseDateTime(): void {
Deno.test("parseDateTime", function (): void {
assertEquals(
datetime.parseDateTime("01-03-2019 16:30", "mm-dd-yyyy hh:mm"),
new Date(2019, 0, 3, 16, 30)
Expand All @@ -29,7 +29,7 @@ Deno.test(function parseDateTime(): void {
);
});

Deno.test(function invalidParseDateTimeFormatThrows(): void {
Deno.test("invalidParseDateTimeFormatThrows", function (): void {
assertThrows(
(): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -40,7 +40,7 @@ Deno.test(function invalidParseDateTimeFormatThrows(): void {
);
});

Deno.test(function parseDate(): void {
Deno.test("parseDate", function (): void {
assertEquals(
datetime.parseDate("01-03-2019", "mm-dd-yyyy"),
new Date(2019, 0, 3)
Expand All @@ -55,7 +55,7 @@ Deno.test(function parseDate(): void {
);
});

Deno.test(function invalidParseDateFormatThrows(): void {
Deno.test("invalidParseDateFormatThrows", function (): void {
assertThrows(
(): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -66,13 +66,13 @@ Deno.test(function invalidParseDateFormatThrows(): void {
);
});

Deno.test(function DayOfYear(): void {
Deno.test("DayOfYear", function (): void {
assertEquals(1, datetime.dayOfYear(new Date("2019-01-01T03:24:00")));
assertEquals(70, datetime.dayOfYear(new Date("2019-03-11T03:24:00")));
assertEquals(365, datetime.dayOfYear(new Date("2019-12-31T03:24:00")));
});

Deno.test(function currentDayOfYear(): void {
Deno.test("currentDayOfYear", function (): void {
assertEquals(datetime.currentDayOfYear(), datetime.dayOfYear(new Date()));
});

Expand Down
Loading

0 comments on commit 8feb30e

Please sign in to comment.