-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Upload.test.mjs
62 lines (44 loc) · 1.59 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
60
61
62
// @ts-check
import { ok, rejects, strictEqual } from "node:assert";
import { describe, it } from "node:test";
import Upload from "./Upload.mjs";
describe(
"Class `Upload`.",
{
concurrency: true,
},
() => {
it("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);
});
it("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);
});
it("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.
});
},
);