|
| 1 | +// Minimal assert version to avoid dependencies on node internals |
| 2 | +// Allows to verify that none of browserify version of node internals is included in resulting build |
| 3 | +function deepStrictEqual(actual, expected, message) { |
| 4 | + const [actualType, expectedType] = [typeof actual, typeof expected]; |
| 5 | + const err = new Error(`Non-equal values: actual=${actual} (type=${actualType}) expected=${expected} (type=${expectedType})${message ? `. Message: ${message}` : ''}`); |
| 6 | + if (actualType !== expectedType) { |
| 7 | + throw err; |
| 8 | + } |
| 9 | + // Primitive types |
| 10 | + if (['string', 'number', 'bigint', 'undefined', 'boolean'].includes(actualType)) { |
| 11 | + if (actual !== expected) { |
| 12 | + throw err; |
| 13 | + } |
| 14 | + return; |
| 15 | + } |
| 16 | + if (actual instanceof Uint8Array && expected instanceof Uint8Array) { |
| 17 | + if (actual.length !== expected.length) { |
| 18 | + throw err; |
| 19 | + } |
| 20 | + for (let i = 0; i < actual.length; i++) { |
| 21 | + if (actual[i] !== expected[i]) { |
| 22 | + throw err; |
| 23 | + } |
| 24 | + } |
| 25 | + return; |
| 26 | + } |
| 27 | + if (Array.isArray(actual) && Array.isArray(expected)) { |
| 28 | + if (actual.length !== expected.length) { |
| 29 | + throw err; |
| 30 | + } |
| 31 | + for (let i = 0; i < actual.length; i++) { |
| 32 | + deepStrictEqual(actual[i], expected[i], message); |
| 33 | + } |
| 34 | + return; |
| 35 | + } |
| 36 | + if (actual === null && expected === null) { |
| 37 | + return; |
| 38 | + } |
| 39 | + if (actualType === 'object') { |
| 40 | + const [actualKeys, expectedKeys] = [ |
| 41 | + Object.keys(actual), |
| 42 | + Object.keys(expected), |
| 43 | + ]; |
| 44 | + deepStrictEqual(actualKeys, expectedKeys, message); |
| 45 | + for (const key of actualKeys) { |
| 46 | + deepStrictEqual(actual[key], expected[key], message); |
| 47 | + } |
| 48 | + return; |
| 49 | + } |
| 50 | + throw err; |
| 51 | +} |
| 52 | +async function throws(cb) { |
| 53 | + try { |
| 54 | + cb(); |
| 55 | + } |
| 56 | + catch (e) { |
| 57 | + return; |
| 58 | + } |
| 59 | + throw new Error('Missing expected exception.'); |
| 60 | +} |
| 61 | +async function rejects(cb) { |
| 62 | + try { |
| 63 | + await cb(); |
| 64 | + } |
| 65 | + catch (e) { |
| 66 | + return; |
| 67 | + } |
| 68 | + throw new Error('Missing expected rejection.'); |
| 69 | +} |
| 70 | +// Run tests with node assert: |
| 71 | +// import { deepStrictEqual, throws } from "assert"; |
| 72 | +export { deepStrictEqual, throws, rejects }; |
0 commit comments