|
| 1 | +import * as assertImport from "@std/assert" |
| 2 | + |
| 3 | +export const expect = (actual: any) => ({ |
| 4 | + toBe: (expected: unknown) => assertImport.assertEquals(actual, expected), |
| 5 | + toEqual: (expected: unknown) => assertImport.assertEquals(actual, expected), |
| 6 | + toBeNull: () => assertImport.assertEquals(actual, null), |
| 7 | + toBeTruthy: () => assertImport.assert(actual), |
| 8 | + toBeUndefined: () => assertImport.assertEquals(actual, undefined), |
| 9 | + toBeGreaterThanOrEqual: (expected: any) => { |
| 10 | + assertImport.assert(typeof actual === "number" && typeof expected === "number") |
| 11 | + assertImport.assert(actual >= expected) |
| 12 | + }, |
| 13 | + toStrictEqual: (expected: unknown) => assertImport.assertEquals(actual, expected), |
| 14 | + toMatchObject: (expected: any) => { |
| 15 | + for (const key in expected) { |
| 16 | + const expectedValue = expected[key] |
| 17 | + const actualValue = actual[key] |
| 18 | + |
| 19 | + if (expectedValue instanceof Uint8Array) { |
| 20 | + if (actualValue && actualValue.type === "Buffer" && Array.isArray(actualValue.data)) { |
| 21 | + assertImport.assertEquals(actualValue.data, Array.from(expectedValue)) |
| 22 | + } else { |
| 23 | + assertImport.assertEquals(Array.from(actualValue), Array.from(expectedValue)) |
| 24 | + } |
| 25 | + } else if (typeof expectedValue === "object" && expectedValue !== null && !Array.isArray(expectedValue)) { |
| 26 | + if (expectedValue.type === "Buffer" && expectedValue.data instanceof Uint8Array) { |
| 27 | + assertImport.assertEquals(actualValue.type, "Buffer") |
| 28 | + assertImport.assertEquals(actualValue.data, Array.from(expectedValue.data)) |
| 29 | + } else { |
| 30 | + assertImport.assertEquals(actualValue, expectedValue) |
| 31 | + } |
| 32 | + } else { |
| 33 | + assertImport.assertEquals(actualValue, expectedValue) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +export const assert = { |
| 40 | + ...assertImport, |
| 41 | + rejects: async ( |
| 42 | + fn: () => Promise<unknown>, |
| 43 | + validator?: (error: unknown) => boolean |
| 44 | + ) => { |
| 45 | + try { |
| 46 | + await fn() |
| 47 | + throw new Error("Expected promise to reject") |
| 48 | + } catch (error) { |
| 49 | + if (validator && !validator(error)) { |
| 50 | + throw new Error("Validator returned false") |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export const fs = { |
| 57 | + openAsBlob: async (path: string) => { |
| 58 | + const file = await Deno.open(path, { read: true }) |
| 59 | + const fileInfo = await file.stat() |
| 60 | + const buffer = new Uint8Array(fileInfo.size) |
| 61 | + await file.read(buffer) |
| 62 | + file.close() |
| 63 | + return new Blob([buffer]) |
| 64 | + } |
| 65 | +} |
0 commit comments