-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathutil.ts
More file actions
50 lines (46 loc) · 1.54 KB
/
util.ts
File metadata and controls
50 lines (46 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { assert } from 'chai';
import type { TestSuites } from '../types/tests';
export const TestsContext: TestSuites = {};
export const test = (
suiteName: string,
testName: string,
fn: () => void | Promise<void>,
): void => {
if (!TestsContext[suiteName]) {
TestsContext[suiteName] = { value: false, tests: {} };
}
TestsContext[suiteName].tests[testName] = fn;
};
export const assertThrowsAsync = async (
fn: () => Promise<unknown>,
expectedMessage: string,
) => {
try {
await fn();
} catch (error) {
const err = error as Error;
if (expectedMessage) {
// Match the snippet against either the message OR the error name. Spec-
// correct DOMException errors carry the type ('DataError',
// 'NotSupportedError', etc.) on `err.name`, not in the human-readable
// message — so existing tests that check for the type-name snippet
// continue to pass.
const found =
err.message.includes(expectedMessage) ||
err.name.includes(expectedMessage);
assert.isTrue(
found,
`Function failed as expected, but could not find snippet '${expectedMessage}' in message or name. Saw message='${err.message}' name='${err.name}' instead.`,
);
}
return;
}
assert.fail('function did not throw as expected');
};
export const decodeHex = (str: string): Uint8Array => {
const uint8array = new Uint8Array(Math.ceil(str.length / 2));
for (let i = 0; i < str.length; ) {
uint8array[i / 2] = Number.parseInt(str.slice(i, (i += 2)), 16);
}
return uint8array;
};