|
| 1 | +import { AssertionError } from '../errors/assertion.mjs' |
| 2 | +import { RunnerError } from '../errors/runner.mjs' |
| 3 | +import { indentLine } from '../utils/transform.mjs' |
| 4 | +import { TYPES } from '../core/constants.mjs' |
| 5 | +import { EOL } from 'os' |
| 6 | + |
| 7 | +export const toBeTypeOf = (actual, expected) => { |
| 8 | + if (typeof expected !== 'string') { |
| 9 | + throw new RunnerError( |
| 10 | + indentLine(`Provided type: ${typeof expected} is not a string`) |
| 11 | + ) |
| 12 | + } |
| 13 | + |
| 14 | + if (!(expected in TYPES)) { |
| 15 | + throw new RunnerError( |
| 16 | + indentLine(`Expect type: ${expected} is not a valid type`) |
| 17 | + ) |
| 18 | + } |
| 19 | + |
| 20 | + if (expected === 'array') { |
| 21 | + if (!Array.isArray(actual)) { |
| 22 | + throw new AssertionError( |
| 23 | + indentLine('Expected: <expected>') + |
| 24 | + ' has an array type' + |
| 25 | + EOL + |
| 26 | + indentLine('Received: <actual>') + |
| 27 | + ` has a ${Array.isArray(actual) ? 'array' : typeof actual} type`, |
| 28 | + { |
| 29 | + actual, |
| 30 | + expected, |
| 31 | + } |
| 32 | + ) |
| 33 | + } |
| 34 | + } else if (expected === 'null') { |
| 35 | + if (actual !== null) { |
| 36 | + throw new AssertionError( |
| 37 | + indentLine('Expected: <expected>') + |
| 38 | + ' has a null type' + |
| 39 | + EOL + |
| 40 | + indentLine('Received: <actual>') + |
| 41 | + ` has a ${ |
| 42 | + actual === undefined |
| 43 | + ? 'undefined' |
| 44 | + : actual === null |
| 45 | + ? 'null' |
| 46 | + : Array.isArray(actual) |
| 47 | + ? 'array' |
| 48 | + : typeof actual |
| 49 | + } type`, |
| 50 | + { |
| 51 | + actual, |
| 52 | + expected, |
| 53 | + } |
| 54 | + ) |
| 55 | + } |
| 56 | + } else if (expected === 'string') { |
| 57 | + if (typeof actual !== 'string') { |
| 58 | + throw new AssertionError( |
| 59 | + indentLine('Expected: <expected>') + |
| 60 | + ' has a string type' + |
| 61 | + EOL + |
| 62 | + indentLine('Received: <actual>') + |
| 63 | + ` has a ${typeof actual} type`, |
| 64 | + { |
| 65 | + actual, |
| 66 | + expected, |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | + } else if (expected === 'number') { |
| 71 | + if (typeof actual !== 'number') { |
| 72 | + throw new AssertionError( |
| 73 | + indentLine('Expected: <expected>') + |
| 74 | + ' has a number type' + |
| 75 | + EOL + |
| 76 | + indentLine('Received: <actual>') + |
| 77 | + ` has a ${typeof actual} type`, |
| 78 | + { |
| 79 | + actual, |
| 80 | + expected, |
| 81 | + } |
| 82 | + ) |
| 83 | + } |
| 84 | + } else if (expected === 'boolean') { |
| 85 | + if (typeof actual !== 'boolean') { |
| 86 | + throw new AssertionError( |
| 87 | + indentLine('Expected: <expected>') + |
| 88 | + ' has a boolean type' + |
| 89 | + EOL + |
| 90 | + indentLine('Received: <actual>') + |
| 91 | + ` has a ${typeof actual} type`, |
| 92 | + { |
| 93 | + actual, |
| 94 | + expected, |
| 95 | + } |
| 96 | + ) |
| 97 | + } |
| 98 | + } else if (expected === 'object') { |
| 99 | + if ( |
| 100 | + typeof actual !== 'object' || |
| 101 | + actual === null || |
| 102 | + Array.isArray(actual) |
| 103 | + ) { |
| 104 | + throw new AssertionError( |
| 105 | + indentLine('Expected: <expected>') + |
| 106 | + ' has an object type' + |
| 107 | + EOL + |
| 108 | + indentLine('Received: <actual>') + |
| 109 | + ` has a ${typeof actual} type`, |
| 110 | + { |
| 111 | + actual, |
| 112 | + expected, |
| 113 | + } |
| 114 | + ) |
| 115 | + } |
| 116 | + } else if (expected === 'function') { |
| 117 | + if (typeof actual !== 'function') { |
| 118 | + throw new AssertionError( |
| 119 | + indentLine('Expected: <expected>') + |
| 120 | + ' has a function type' + |
| 121 | + EOL + |
| 122 | + indentLine('Received: <actual>') + |
| 123 | + ` has a ${typeof actual} type`, |
| 124 | + { |
| 125 | + actual, |
| 126 | + expected, |
| 127 | + } |
| 128 | + ) |
| 129 | + } |
| 130 | + } else if (expected === 'undefined') { |
| 131 | + if (typeof actual !== 'undefined') { |
| 132 | + throw new AssertionError( |
| 133 | + indentLine('Expected: <expected>') + |
| 134 | + ' has an undefined type' + |
| 135 | + EOL + |
| 136 | + indentLine('Received: <actual>') + |
| 137 | + ` has a ${typeof actual} type`, |
| 138 | + { |
| 139 | + actual, |
| 140 | + expected, |
| 141 | + } |
| 142 | + ) |
| 143 | + } |
| 144 | + } else if (expected === 'symbol') { |
| 145 | + if (typeof actual !== 'symbol') { |
| 146 | + throw new AssertionError( |
| 147 | + indentLine('Expected: <expected>') + |
| 148 | + ' has a symbol type' + |
| 149 | + EOL + |
| 150 | + indentLine('Received: <actual>') + |
| 151 | + ` has a ${typeof actual} type`, |
| 152 | + { |
| 153 | + actual, |
| 154 | + expected, |
| 155 | + } |
| 156 | + ) |
| 157 | + } |
| 158 | + } else if (expected === 'bigint') { |
| 159 | + if (typeof actual !== 'bigint') { |
| 160 | + throw new AssertionError( |
| 161 | + indentLine('Expected: <expected>') + |
| 162 | + ' has a bigint type' + |
| 163 | + EOL + |
| 164 | + indentLine('Received: <actual>') + |
| 165 | + ` has a ${typeof actual} type`, |
| 166 | + { |
| 167 | + actual, |
| 168 | + expected, |
| 169 | + } |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments