mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
add assertResources sanitizer to cli/js/ unit tests (#4161)
This commit is contained in:
parent
199fb195f3
commit
f55b22e195
11 changed files with 95 additions and 36 deletions
|
@ -106,6 +106,22 @@ function normalizeTestPermissions(perms: TestPermissions): Permissions {
|
|||
};
|
||||
}
|
||||
|
||||
// Wrap `TestFunction` in additional assertion that makes sure
|
||||
// the test case does not "leak" resources - ie. resource table after
|
||||
// the test has exactly the same contents as before the test.
|
||||
function assertResources(fn: Deno.TestFunction): Deno.TestFunction {
|
||||
return async function(): Promise<void> {
|
||||
const preResources = Deno.resources();
|
||||
await fn();
|
||||
const postResources = Deno.resources();
|
||||
const msg = `Test case is leaking resources.
|
||||
Before: ${JSON.stringify(preResources, null, 2)}
|
||||
After: ${JSON.stringify(postResources, null, 2)}`;
|
||||
|
||||
assertEquals(preResources, postResources, msg);
|
||||
};
|
||||
}
|
||||
|
||||
export function testPerm(perms: TestPermissions, fn: Deno.TestFunction): void {
|
||||
const normalizedPerms = normalizeTestPermissions(perms);
|
||||
|
||||
|
@ -115,7 +131,7 @@ export function testPerm(perms: TestPermissions, fn: Deno.TestFunction): void {
|
|||
return;
|
||||
}
|
||||
|
||||
Deno.test(fn);
|
||||
Deno.test(fn.name, assertResources(fn));
|
||||
}
|
||||
|
||||
export function test(fn: Deno.TestFunction): void {
|
||||
|
@ -171,6 +187,24 @@ export async function parseUnitTestOutput(
|
|||
return { actual, expected, resultOutput: result };
|
||||
}
|
||||
|
||||
export interface ResolvableMethods<T> {
|
||||
resolve: (value?: T | PromiseLike<T>) => void;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
reject: (reason?: any) => void;
|
||||
}
|
||||
|
||||
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
||||
|
||||
export function createResolvable<T>(): Resolvable<T> {
|
||||
let methods: ResolvableMethods<T>;
|
||||
const promise = new Promise<T>((resolve, reject): void => {
|
||||
methods = { resolve, reject };
|
||||
});
|
||||
// TypeScript doesn't know that the Promise callback occurs synchronously
|
||||
// therefore use of not null assertion (`!`)
|
||||
return Object.assign(promise, methods!) as Resolvable<T>;
|
||||
}
|
||||
|
||||
test(function permissionsMatches(): void {
|
||||
assert(
|
||||
permissionsMatch(
|
||||
|
@ -265,28 +299,25 @@ testPerm({ read: true }, async function parsingUnitTestOutput(): Promise<void> {
|
|||
let result;
|
||||
|
||||
// This is an example of a successful unit test output.
|
||||
result = await parseUnitTestOutput(
|
||||
await Deno.open(`${testDataPath}/unit_test_output1.txt`),
|
||||
false
|
||||
);
|
||||
const f1 = await Deno.open(`${testDataPath}/unit_test_output1.txt`);
|
||||
result = await parseUnitTestOutput(f1, false);
|
||||
assertEquals(result.actual, 96);
|
||||
assertEquals(result.expected, 96);
|
||||
f1.close();
|
||||
|
||||
// This is an example of a silently dying unit test.
|
||||
result = await parseUnitTestOutput(
|
||||
await Deno.open(`${testDataPath}/unit_test_output2.txt`),
|
||||
false
|
||||
);
|
||||
const f2 = await Deno.open(`${testDataPath}/unit_test_output2.txt`);
|
||||
result = await parseUnitTestOutput(f2, false);
|
||||
assertEquals(result.actual, undefined);
|
||||
assertEquals(result.expected, 96);
|
||||
f2.close();
|
||||
|
||||
// This is an example of compiling before successful unit tests.
|
||||
result = await parseUnitTestOutput(
|
||||
await Deno.open(`${testDataPath}/unit_test_output3.txt`),
|
||||
false
|
||||
);
|
||||
const f3 = await Deno.open(`${testDataPath}/unit_test_output3.txt`);
|
||||
result = await parseUnitTestOutput(f3, false);
|
||||
assertEquals(result.actual, 96);
|
||||
assertEquals(result.expected, 96);
|
||||
f3.close();
|
||||
|
||||
// Check what happens on empty output.
|
||||
const f = new Deno.Buffer(new TextEncoder().encode("\n\n\n"));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue