-
Notifications
You must be signed in to change notification settings - Fork 0
/
Upload.test.mjs
59 lines (41 loc) · 1.61 KB
/
Upload.test.mjs
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
51
52
53
54
55
56
57
58
59
// @ts-check
import { ok, rejects, strictEqual } from "node:assert";
import Upload from "./Upload.mjs";
/**
* Adds `Upload` tests.
* @param {import("test-director").default} tests Test director.
*/
export default (tests) => {
tests.add("`Upload` class resolving a file.", async () => {
const upload = new Upload();
ok(upload.promise instanceof Promise);
strictEqual(typeof upload.resolve, "function");
/** @type {any} */
const file = {};
upload.resolve(file);
const resolved = await upload.promise;
strictEqual(resolved, file);
strictEqual(upload.file, file);
});
tests.add("`Upload` class with a handled rejection.", async () => {
const upload = new Upload();
ok(upload.promise instanceof Promise);
strictEqual(typeof upload.reject, "function");
const error = new Error("Message.");
upload.reject(error);
// This is the safe way to check the promise status, see:
// https://github.com/nodejs/node/issues/31392#issuecomment-575451230
await rejects(Promise.race([upload.promise, Promise.resolve()]), error);
});
tests.add("`Upload` class with an unhandled rejection.", async () => {
const upload = new Upload();
ok(upload.promise instanceof Promise);
strictEqual(typeof upload.reject, "function");
const error = new Error("Message.");
upload.reject(error);
// Node.js CLI flag `--unhandled-rejections=throw` must be used when these
// tests are run with Node.js v14 (it’s unnecessary for Node.js v15+) or the
// process won’t exit with an error if the unhandled rejection is’t silenced
// as intended.
});
};