Skip to content

Commit

Permalink
Add setup and teardown options to test function
Browse files Browse the repository at this point in the history
  • Loading branch information
hayd committed Jan 17, 2019
1 parent ddff85a commit 987ef51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
36 changes: 15 additions & 21 deletions http/file_server_test.ts
Expand Up @@ -21,9 +21,8 @@ function killFileServer() {
fileServer.stdout.close();
}

test(async function serveFile() {
await startFileServer();
try {
test(
async function serveFile() {
const res = await fetch("http://localhost:4500/azure-pipelines.yml");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
Expand All @@ -33,32 +32,27 @@ test(async function serveFile() {
await readFile("./azure-pipelines.yml")
);
assertEqual(downloadedFile, localFile);
} finally {
killFileServer();
}
});
},
{ setup: startFileServer, teardown: killFileServer }
);

test(async function serveDirectory() {
await startFileServer();
try {
test(
async function serveDirectory() {
const res = await fetch("http://localhost:4500/");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
const page = await res.text();
assert(page.includes("azure-pipelines.yml"));
} finally {
killFileServer();
}
});
},
{ setup: startFileServer, teardown: killFileServer }
);

test(async function serveFallback() {
await startFileServer();
try {
test(
async function serveFallback() {
const res = await fetch("http://localhost:4500/badfile.txt");
assert(res.headers.has("access-control-allow-origin"));
assert(res.headers.has("access-control-allow-headers"));
assertEqual(res.status, 404);
} finally {
killFileServer();
}
});
},
{ setup: startFileServer, teardown: killFileServer }
);
22 changes: 19 additions & 3 deletions testing/mod.ts
Expand Up @@ -151,6 +151,8 @@ export type TestFunction = () => void | Promise<void>;
export interface TestDefinition {
fn: TestFunction;
name: string;
setup: TestFunction;
teardown: TestFunction;
}

export const exitOnFail = true;
Expand All @@ -167,15 +169,26 @@ export function setFilter(s: string): void {
filterRegExp = new RegExp(s, "i");
}

export function test(t: TestDefinition | TestFunction): void {
export interface TestOptions {
setup: TestFunction;
teardown: TestFunction;
}

export function test(
t: TestDefinition | TestFunction,
options: TestOptions = { setup: function() {}, teardown: function() {} }
): void {
const fn: TestFunction = typeof t === "function" ? t : t.fn;
const name: string = t.name;
const setup: TestFunction = typeof t === "function" ? options.setup : t.setup;
const teardown: TestFunction =
typeof t === "function" ? options.teardown : t.teardown;

if (!name) {
throw new Error("Test function may not be anonymous");
}
if (filter(name)) {
tests.push({ fn, name });
tests.push({ fn, name, setup, teardown });
} else {
filtered++;
}
Expand Down Expand Up @@ -207,12 +220,13 @@ async function runTests() {

console.log("running", tests.length, "tests");
for (let i = 0; i < tests.length; i++) {
const { fn, name } = tests[i];
const { fn, name, setup, teardown } = tests[i];
let result = green_ok();
// See https://github.com/denoland/deno/pull/1452
// about this usage of groupCollapsed
console.groupCollapsed(`test ${name} `);
try {
await setup();
await fn();
passed++;
console.log("...", result);
Expand All @@ -226,6 +240,8 @@ async function runTests() {
if (exitOnFail) {
break;
}
} finally {
await teardown();
}
}

Expand Down

0 comments on commit 987ef51

Please sign in to comment.