|
1 | 1 | import assert from 'node:assert/strict' |
2 | | -import { execSync } from 'node:child_process' |
3 | 2 | import { test } from 'node:test' |
| 3 | +import * as solution from './index.ts' |
4 | 4 |
|
5 | | -const output = execSync('npm start --silent', { encoding: 'utf8' }) |
6 | | -const jsonLine = output.split('\n').find((line) => line.startsWith('Results:')) |
7 | | -assert.ok(jsonLine, '🚨 Missing "Results:" output line') |
8 | | -const { user, orders } = JSON.parse(jsonLine.replace('Results:', '').trim()) |
9 | | - |
10 | | -await test('fetchUser resolves to a User object', async () => { |
11 | | - assert.ok( |
12 | | - 'id' in user, |
13 | | - '🚨 user should have an id property - make sure fetchUser resolves correctly', |
14 | | - ) |
| 5 | +await test('fetchUser and fetchOrders are exported', () => { |
15 | 6 | assert.ok( |
16 | | - 'name' in user, |
17 | | - '🚨 user should have a name property - make sure fetchUser resolves correctly', |
| 7 | + 'fetchUser' in solution, |
| 8 | + '🚨 Make sure you export "fetchUser" - add: export { fetchUser, fetchOrders }', |
18 | 9 | ) |
19 | 10 | assert.ok( |
20 | | - 'email' in user, |
21 | | - '🚨 user should have an email property - make sure fetchUser resolves correctly', |
22 | | - ) |
23 | | - assert.strictEqual( |
24 | | - user.id, |
25 | | - '1', |
26 | | - '🚨 user.id should be "1" - check your Promise resolve value', |
27 | | - ) |
28 | | - assert.strictEqual( |
29 | | - user.name, |
30 | | - 'Alice', |
31 | | - '🚨 user.name should be "Alice" - check your Promise resolve value', |
32 | | - ) |
33 | | - assert.strictEqual( |
34 | | - user.email, |
35 | | - |
36 | | - '🚨 user.email should be "[email protected]" - check your Promise resolve value', |
| 11 | + 'fetchOrders' in solution, |
| 12 | + '🚨 Make sure you export "fetchOrders" - add: export { fetchUser, fetchOrders }', |
37 | 13 | ) |
38 | 14 | }) |
39 | 15 |
|
40 | | -await test('fetchOrders resolves to an array of Order objects', async () => { |
41 | | - assert.strictEqual( |
42 | | - Array.isArray(orders), |
43 | | - true, |
44 | | - '🚨 orders should be an array - make sure fetchOrders returns an array', |
45 | | - ) |
46 | | - assert.ok( |
47 | | - orders.length > 0, |
48 | | - '🚨 orders should have at least one item - check your Promise resolve value', |
49 | | - ) |
50 | | - assert.ok( |
51 | | - 'id' in orders[0], |
52 | | - '🚨 orders[0] should have an id property - check your Promise resolve value', |
53 | | - ) |
54 | | - assert.ok( |
55 | | - 'userId' in orders[0], |
56 | | - '🚨 orders[0] should have a userId property - check your Promise resolve value', |
57 | | - ) |
58 | | - assert.ok( |
59 | | - 'items' in orders[0], |
60 | | - '🚨 orders[0] should have an items property - check your Promise resolve value', |
61 | | - ) |
62 | | - assert.ok( |
63 | | - 'total' in orders[0], |
64 | | - '🚨 orders[0] should have a total property - check your Promise resolve value', |
65 | | - ) |
66 | | - assert.strictEqual( |
67 | | - orders[0].userId, |
68 | | - '1', |
69 | | - '🚨 orders[0].userId should be "1" - make sure you use the userId parameter', |
70 | | - ) |
71 | | -}) |
| 16 | +await test( |
| 17 | + 'fetchUser resolves to a User object', |
| 18 | + { timeout: 5000 }, |
| 19 | + async () => { |
| 20 | + const user = await solution.fetchUser() |
| 21 | + |
| 22 | + assert.ok( |
| 23 | + 'id' in user, |
| 24 | + '🚨 user should have an id property - make sure fetchUser resolves correctly', |
| 25 | + ) |
| 26 | + assert.ok( |
| 27 | + 'name' in user, |
| 28 | + '🚨 user should have a name property - make sure fetchUser resolves correctly', |
| 29 | + ) |
| 30 | + assert.ok( |
| 31 | + 'email' in user, |
| 32 | + '🚨 user should have an email property - make sure fetchUser resolves correctly', |
| 33 | + ) |
| 34 | + assert.strictEqual( |
| 35 | + user.id, |
| 36 | + '1', |
| 37 | + '🚨 user.id should be "1" - check your Promise resolve value', |
| 38 | + ) |
| 39 | + assert.strictEqual( |
| 40 | + user.name, |
| 41 | + 'Alice', |
| 42 | + '🚨 user.name should be "Alice" - check your Promise resolve value', |
| 43 | + ) |
| 44 | + assert.strictEqual( |
| 45 | + user.email, |
| 46 | + |
| 47 | + '🚨 user.email should be "[email protected]" - check your Promise resolve value', |
| 48 | + ) |
| 49 | + }, |
| 50 | +) |
| 51 | + |
| 52 | +await test( |
| 53 | + 'fetchOrders resolves to an array of Order objects', |
| 54 | + { timeout: 5000 }, |
| 55 | + async () => { |
| 56 | + const orders = await solution.fetchOrders('1') |
| 57 | + |
| 58 | + assert.strictEqual( |
| 59 | + Array.isArray(orders), |
| 60 | + true, |
| 61 | + '🚨 orders should be an array - make sure fetchOrders returns an array', |
| 62 | + ) |
| 63 | + assert.ok( |
| 64 | + orders.length > 0, |
| 65 | + '🚨 orders should have at least one item - check your Promise resolve value', |
| 66 | + ) |
| 67 | + assert.ok( |
| 68 | + 'id' in orders[0], |
| 69 | + '🚨 orders[0] should have an id property - check your Promise resolve value', |
| 70 | + ) |
| 71 | + assert.ok( |
| 72 | + 'userId' in orders[0], |
| 73 | + '🚨 orders[0] should have a userId property - check your Promise resolve value', |
| 74 | + ) |
| 75 | + assert.ok( |
| 76 | + 'items' in orders[0], |
| 77 | + '🚨 orders[0] should have an items property - check your Promise resolve value', |
| 78 | + ) |
| 79 | + assert.ok( |
| 80 | + 'total' in orders[0], |
| 81 | + '🚨 orders[0] should have a total property - check your Promise resolve value', |
| 82 | + ) |
| 83 | + assert.strictEqual( |
| 84 | + orders[0].userId, |
| 85 | + '1', |
| 86 | + '🚨 orders[0].userId should be "1" - make sure you use the userId parameter', |
| 87 | + ) |
| 88 | + }, |
| 89 | +) |
| 90 | + |
| 91 | +await test('promise chaining works correctly', { timeout: 5000 }, async () => { |
| 92 | + const user = await solution.fetchUser() |
| 93 | + const orders = await solution.fetchOrders(user.id) |
72 | 94 |
|
73 | | -await test('promise chaining works correctly', async () => { |
74 | 95 | assert.strictEqual( |
75 | 96 | user.id, |
76 | 97 | '1', |
|
0 commit comments