|
2 | 2 | import assert from 'node:assert/strict'; |
3 | 3 | import {jest} from '@jest/globals'; |
4 | 4 | import util from 'node:util'; |
| 5 | +import {style} from './styles.js'; |
5 | 6 | util.inspect.defaultOptions.depth = null; // show full objects |
6 | 7 | // util.inspect.defaultOptions.depth = 0; // show truncated objects |
| 8 | +// util.inspect.defaultOptions.compact = true; // dont break objects to new lines |
| 9 | +// util.inspect.defaultOptions.compact = false; // break objects to new lines |
| 10 | + |
7 | 11 | // suppress jests tracing console logs |
8 | 12 | import console from 'console'; |
9 | 13 | const jestConsole = console; |
10 | | -beforeEach(() => { global.console = console; }); |
11 | | -afterEach(() => { global.console = jestConsole; }); |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + global.console = console; |
| 17 | + console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset,'\n'); }); |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + global.console = jestConsole; |
| 21 | + console.log(style.color(99), style.hr.double, style.reset); |
| 22 | +}); |
| 23 | + |
| 24 | +const logItDescription = (description, callback) => { |
| 25 | + console.log(style.green, ' ☞ ', style.reset,description); |
| 26 | + it(description, callback); |
| 27 | +}; |
| 28 | + |
| 29 | +const logItDescriptionLogs = (description, callback) => { |
| 30 | + it(description, async () => { |
| 31 | + // Array to store captured log messages for this specific test |
| 32 | + const capturedLogs = []; |
| 33 | + |
| 34 | + // Spy on console.log and push messages to our array |
| 35 | + const logSpy = jest.spyOn(console, 'log').mockImplementation((...args) => { |
| 36 | + capturedLogs.push(args); |
| 37 | + }); |
| 38 | + |
| 39 | + // Capture console.log output during the test's execution |
| 40 | + try { |
| 41 | + await callback(); // Assuming the callback might be async |
| 42 | + } finally { |
| 43 | + // Restore the original console.log behavior |
| 44 | + logSpy.mockRestore(); |
| 45 | + |
| 46 | + // Print the custom description and then the captured logs |
| 47 | + // console.log(style.green, ' ☞ ', style.reset, description); |
| 48 | + capturedLogs.forEach(logArgs => { |
| 49 | + console.log(' ↳ ', ...logArgs); |
| 50 | + }); |
| 51 | + } |
| 52 | + }); |
| 53 | +}; |
| 54 | + |
| 55 | +const logCategory = (description, callback) => { |
| 56 | + console.log(style.color(255,0,255),'▷',style.reset,style.color(39),description,style.reset); |
| 57 | + describe(description, callback); |
| 58 | +}; |
| 59 | + |
| 60 | +const logDescribe = (description, callback) => { |
| 61 | + console.log('\n', style.orange,' ▷',style.reset,style.color(39),description,style.reset); |
| 62 | + describe(description, callback); |
| 63 | +}; |
| 64 | + |
12 | 65 | import {inventory, restock, sufficient} from './map.js'; |
13 | 66 |
|
14 | 67 | describe('Map', () => { |
15 | | - it('should filter inventory using `Map.groupBy()`', () => { |
16 | | - console.log(expect.getState().currentTestName); |
17 | | - |
18 | | - const result = Map.groupBy(inventory, ({ quantity }) => |
19 | | - quantity < 10 ? restock : sufficient, |
20 | | - ); |
21 | | - console.log(result.get(restock)); |
22 | | - console.log('result', result); |
23 | | - // [{ name: "bananas", type: "fruit", quantity: 5 }] |
24 | | - expect(result.get(restock)).toBeTruthy(); |
25 | | - }); |
| 68 | + it('should...', () => { |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('Map.groupBy()', () => { |
| 73 | + |
| 74 | + it('should describe the grouping process', () => { |
| 75 | + |
| 76 | + const map = Map.groupBy(inventory, ({ quantity }) => |
| 77 | + quantity < 10 ? restock : sufficient, |
| 78 | + ); |
| 79 | + console.log(style.h1('The Map.groupBy() static method groups the elements of a given iterable using the values returned by a provided callback function. The final returned Map uses the unique values from the test function as keys, which can be used to get the array of elements in each group\n')); |
| 80 | + |
| 81 | + console.log(style.h2('Given an iterable (inventory):\n\n'), |
| 82 | + inventory); |
26 | 83 |
|
27 | | - it('should filter inventory using `Map.groupBy()`', () => { |
28 | | - console.log(expect.getState().currentTestName); |
| 84 | + console.log(style.h2('\n..And grouping values (restock, sufficient):\n\n'), restock, '\n', sufficient); |
29 | 85 |
|
30 | | - const result = Map.groupBy([[1,3],[2,3]], ([a,]) => { |
31 | | - return a === 1 ? a : 2; |
32 | | - }); |
33 | | - console.log('result', result); |
34 | | - expect(result.get(1)).toBeTruthy(); |
| 86 | + console.log(style.h2( |
| 87 | + `\n..Using \`Map.groupBy()\` with a cb that conditionally returns |
| 88 | +the grouping values for each inventory item: |
| 89 | +${style.green} |
| 90 | + Map.groupBy(inventory, ({ quantity }) => |
| 91 | + quantity < 10 ? restock : sufficient, |
| 92 | + ); |
| 93 | +${style.reset}`), ); |
| 94 | + |
| 95 | + console.log(style.h2( |
| 96 | + `..Will give use the result (Map):\n`)); |
| 97 | + console.log(map); |
| 98 | + |
| 99 | + }); |
| 100 | + |
| 101 | + it('should filter inventory using `Map.groupBy()`', () => { |
| 102 | + |
| 103 | + const result = Map.groupBy(inventory, ({ quantity }) => |
| 104 | + quantity < 10 ? restock : sufficient, |
| 105 | + ); |
| 106 | + console.log('result', result); |
| 107 | + expect(result.get(restock)).toBeTruthy(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should filter inventory using `Map.groupBy()`', () => { |
| 111 | + |
| 112 | + const result = Map.groupBy([[1,3],[2,3]], ([a,]) => { |
| 113 | + return a === 1 ? a : 2; |
35 | 114 | }); |
| 115 | + console.log('result', result); |
| 116 | + expect(result.get(1)).toBeTruthy(); |
| 117 | + }); |
36 | 118 |
|
37 | 119 | }); |
38 | 120 |
|
| 121 | + |
0 commit comments