MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testThrows, testDeep, summary } from './helpers.js';
2import assert from 'node:assert';
3
4console.log('assert.ok\n');
5
6let threw = false;
7try { assert(false); } catch (e) { threw = true; }
8test('assert(false) throws', threw, true);
9
10threw = false;
11try { assert(0); } catch (e) { threw = true; }
12test('assert(0) throws', threw, true);
13
14threw = false;
15try { assert(''); } catch (e) { threw = true; }
16test('assert("") throws', threw, true);
17
18threw = false;
19try { assert(null); } catch (e) { threw = true; }
20test('assert(null) throws', threw, true);
21
22threw = false;
23try { assert(1); } catch (e) { threw = true; }
24test('assert(1) does not throw', threw, false);
25
26threw = false;
27try { assert('hello'); } catch (e) { threw = true; }
28test('assert("hello") does not throw', threw, false);
29
30threw = false;
31try { assert.ok(false, 'custom message'); } catch (e) { threw = true; }
32test('assert.ok with custom message throws', threw, true);
33
34console.log('\nassert.equal / assert.notEqual\n');
35
36threw = false;
37try { assert.equal(1, 1); } catch (e) { threw = true; }
38test('equal(1, 1) passes', threw, false);
39
40threw = false;
41try { assert.equal(1, '1'); } catch (e) { threw = true; }
42test('equal(1, "1") passes (loose)', threw, false);
43
44threw = false;
45try { assert.equal(1, 2); } catch (e) { threw = true; }
46test('equal(1, 2) throws', threw, true);
47
48threw = false;
49try { assert.equal(null, undefined); } catch (e) { threw = true; }
50test('equal(null, undefined) passes (loose)', threw, false);
51
52threw = false;
53try { assert.notEqual(1, 2); } catch (e) { threw = true; }
54test('notEqual(1, 2) passes', threw, false);
55
56threw = false;
57try { assert.notEqual(1, 1); } catch (e) { threw = true; }
58test('notEqual(1, 1) throws', threw, true);
59
60console.log('\nassert.strictEqual / assert.notStrictEqual\n');
61
62threw = false;
63try { assert.strictEqual(1, 1); } catch (e) { threw = true; }
64test('strictEqual(1, 1) passes', threw, false);
65
66threw = false;
67try { assert.strictEqual('a', 'a'); } catch (e) { threw = true; }
68test('strictEqual("a", "a") passes', threw, false);
69
70threw = false;
71try { assert.strictEqual(1, '1'); } catch (e) { threw = true; }
72test('strictEqual(1, "1") throws', threw, true);
73
74threw = false;
75try { assert.strictEqual(null, undefined); } catch (e) { threw = true; }
76test('strictEqual(null, undefined) throws', threw, true);
77
78threw = false;
79try { assert.strictEqual(NaN, NaN); } catch (e) { threw = true; }
80test('strictEqual(NaN, NaN) passes', threw, false);
81
82threw = false;
83try { assert.notStrictEqual(1, 2); } catch (e) { threw = true; }
84test('notStrictEqual(1, 2) passes', threw, false);
85
86threw = false;
87try { assert.notStrictEqual(1, 1); } catch (e) { threw = true; }
88test('notStrictEqual(1, 1) throws', threw, true);
89
90console.log('\nassert.deepEqual / assert.deepStrictEqual\n');
91
92threw = false;
93try { assert.deepEqual({ a: 1 }, { a: 1 }); } catch (e) { threw = true; }
94test('deepEqual({a:1}, {a:1}) passes', threw, false);
95
96threw = false;
97try { assert.deepEqual([1, 2, 3], [1, 2, 3]); } catch (e) { threw = true; }
98test('deepEqual arrays passes', threw, false);
99
100threw = false;
101try { assert.deepEqual({ a: 1 }, { a: 2 }); } catch (e) { threw = true; }
102test('deepEqual({a:1}, {a:2}) throws', threw, true);
103
104threw = false;
105try { assert.deepEqual([1, 2], [1, 2, 3]); } catch (e) { threw = true; }
106test('deepEqual different length arrays throws', threw, true);
107
108threw = false;
109try { assert.deepStrictEqual({ a: 1 }, { a: 1 }); } catch (e) { threw = true; }
110test('deepStrictEqual({a:1}, {a:1}) passes', threw, false);
111
112threw = false;
113try { assert.deepStrictEqual({ a: 1 }, { a: '1' }); } catch (e) { threw = true; }
114test('deepStrictEqual({a:1}, {a:"1"}) throws', threw, true);
115
116threw = false;
117try { assert.deepStrictEqual({ a: { b: { c: 42 } } }, { a: { b: { c: 42 } } }); } catch (e) { threw = true; }
118test('deepStrictEqual nested objects passes', threw, false);
119
120threw = false;
121try { assert.notDeepEqual({ a: 1 }, { a: 2 }); } catch (e) { threw = true; }
122test('notDeepEqual({a:1}, {a:2}) passes', threw, false);
123
124threw = false;
125try { assert.notDeepEqual({ a: 1 }, { a: 1 }); } catch (e) { threw = true; }
126test('notDeepEqual({a:1}, {a:1}) throws', threw, true);
127
128threw = false;
129try { assert.notDeepStrictEqual({ a: 1 }, { a: '1' }); } catch (e) { threw = true; }
130test('notDeepStrictEqual({a:1}, {a:"1"}) passes', threw, false);
131
132console.log('\nassert.fail\n');
133
134threw = false;
135try { assert.fail('boom'); } catch (e) { threw = true; }
136test('fail("boom") throws', threw, true);
137
138threw = false;
139try { assert.fail(); } catch (e) { threw = true; }
140test('fail() throws', threw, true);
141
142let failMsg = '';
143try { assert.fail('specific message'); } catch (e) { failMsg = e.message; }
144test('fail message is preserved', failMsg, 'specific message');
145
146console.log('\nassert.ifError\n');
147
148threw = false;
149try { assert.ifError(null); } catch (e) { threw = true; }
150test('ifError(null) does not throw', threw, false);
151
152threw = false;
153try { assert.ifError(undefined); } catch (e) { threw = true; }
154test('ifError(undefined) does not throw', threw, false);
155
156threw = false;
157try { assert.ifError(new Error('oops')); } catch (e) { threw = true; }
158test('ifError(Error) throws', threw, true);
159
160threw = false;
161try { assert.ifError('some error'); } catch (e) { threw = true; }
162test('ifError(string) throws', threw, true);
163
164console.log('\nassert.throws / assert.doesNotThrow\n');
165
166threw = false;
167try { assert.throws(() => { throw new Error('boom'); }); } catch (e) { threw = true; }
168test('throws with throwing fn passes', threw, false);
169
170threw = false;
171try { assert.throws(() => {}); } catch (e) { threw = true; }
172test('throws with non-throwing fn throws', threw, true);
173
174threw = false;
175try { assert.doesNotThrow(() => {}); } catch (e) { threw = true; }
176test('doesNotThrow with non-throwing fn passes', threw, false);
177
178threw = false;
179try { assert.doesNotThrow(() => { throw new Error('oops'); }); } catch (e) { threw = true; }
180test('doesNotThrow with throwing fn throws', threw, true);
181
182console.log('\nassert.match / assert.doesNotMatch\n');
183
184threw = false;
185try { assert.match('hello world', /hello/); } catch (e) { threw = true; }
186test('match passes when pattern matches', threw, false);
187
188threw = false;
189try { assert.match('hello world', /xyz/); } catch (e) { threw = true; }
190test('match throws when pattern does not match', threw, true);
191
192threw = false;
193try { assert.doesNotMatch('hello world', /xyz/); } catch (e) { threw = true; }
194test('doesNotMatch passes when pattern does not match', threw, false);
195
196threw = false;
197try { assert.doesNotMatch('hello world', /hello/); } catch (e) { threw = true; }
198test('doesNotMatch throws when pattern matches', threw, true);
199
200console.log('\nassert.rejects / assert.doesNotReject\n');
201
202let resolved = false;
203await assert.rejects(async () => { throw new Error('boom'); });
204resolved = true;
205test('rejects resolves when fn rejects', resolved, true);
206
207resolved = false;
208try { await assert.rejects(async () => {}); } catch (e) { resolved = false; }
209test('rejects rejects when fn does not reject', resolved, false);
210
211resolved = false;
212await assert.doesNotReject(async () => {});
213resolved = true;
214test('doesNotReject resolves when fn does not reject', resolved, true);
215
216console.log('\nAssertionError\n');
217
218test('AssertionError is a constructor', typeof assert.AssertionError, 'function');
219
220const ae = new assert.AssertionError({ message: 'test', actual: 1, expected: 2, operator: 'strictEqual' });
221test('AssertionError has name', ae.name, 'AssertionError');
222test('AssertionError has message', ae.message, 'test');
223test('AssertionError has actual', ae.actual, 1);
224test('AssertionError has expected', ae.expected, 2);
225test('AssertionError has operator', ae.operator, 'strictEqual');
226
227summary();