|
| 1 | +// Listen for errors thrown directly by the callback |
| 2 | +function directErrorListener(callback) { |
| 3 | + try { |
| 4 | + callback(); |
| 5 | + } catch (error) { |
| 6 | + return error; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +// Listen for errors using window.addEventListener('error') |
| 11 | +function windowErrorListener(callback) { |
| 12 | + let error; |
| 13 | + function onError(event) { |
| 14 | + event.preventDefault(); // don't log the error |
| 15 | + error = event.error; |
| 16 | + } |
| 17 | + |
| 18 | + // Prevent jasmine from handling the global error. There doesn't seem to be another |
| 19 | + // way to disable this behavior: https://github.com/jasmine/jasmine/pull/1860 |
| 20 | + const originalOnError = window.onerror; |
| 21 | + window.onerror = null; |
| 22 | + window.addEventListener('error', onError); |
| 23 | + |
| 24 | + try { |
| 25 | + callback(); |
| 26 | + } finally { |
| 27 | + window.onerror = originalOnError; |
| 28 | + window.removeEventListener('error', onError); |
| 29 | + } |
| 30 | + return error; |
| 31 | +} |
| 32 | + |
| 33 | +// For errors we expect to be thrown in the connectedCallback() phase |
| 34 | +// of a custom element, there are two possibilities: |
| 35 | +// 1) We're using non-native lifecycle callbacks, so the error is thrown synchronously |
| 36 | +// 2) We're using native lifecycle callbacks, so the error is thrown asynchronously and can |
| 37 | +// only be caught with window.addEventListener('error') |
| 38 | +// - Note native lifecycle callbacks are all thrown asynchronously. |
| 39 | +function customElementCallbackReactionErrorListener(callback) { |
| 40 | + return lwcRuntimeFlags.DISABLE_NATIVE_CUSTOM_ELEMENT_LIFECYCLE |
| 41 | + ? directErrorListener(callback) |
| 42 | + : windowErrorListener(callback); |
| 43 | +} |
| 44 | + |
| 45 | +function matchError(error, expectedErrorCtor, expectedMessage) { |
| 46 | + if ((!error) instanceof expectedErrorCtor) { |
| 47 | + return false; |
| 48 | + } else if (typeof expectedMessage === 'undefined') { |
| 49 | + return true; |
| 50 | + } else if (typeof expectedMessage === 'string') { |
| 51 | + return error.message === expectedMessage; |
| 52 | + } else { |
| 53 | + return expectedMessage.test(error.message); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function throwDescription(thrown) { |
| 58 | + return `${thrown.name} with message "${thrown.message}"`; |
| 59 | +} |
| 60 | + |
| 61 | +function errorMatcherFactory(chai, utils, errorListener, expectInProd) { |
| 62 | + return function toThrowError(expectedErrorCtor, expectedMessage) { |
| 63 | + if (typeof expectedMessage === 'undefined') { |
| 64 | + if (typeof expectedErrorCtor === 'undefined') { |
| 65 | + // 0 arguments provided |
| 66 | + expectedMessage = undefined; |
| 67 | + expectedErrorCtor = Error; |
| 68 | + } else { |
| 69 | + // 1 argument provided |
| 70 | + expectedMessage = expectedErrorCtor; |
| 71 | + expectedErrorCtor = Error; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + const actual = utils.flag(this, 'object'); |
| 76 | + if (typeof actual !== 'function') { |
| 77 | + throw new Error('Expected function to throw error.'); |
| 78 | + } else if (expectedErrorCtor !== Error && !(expectedErrorCtor.prototype instanceof Error)) { |
| 79 | + throw new Error('Expected an error constructor.'); |
| 80 | + } else if ( |
| 81 | + typeof expectedMessage !== 'undefined' && |
| 82 | + typeof expectedMessage !== 'string' && |
| 83 | + !(expectedMessage instanceof RegExp) |
| 84 | + ) { |
| 85 | + throw new Error('Expected a string or a RegExp to compare the thrown error against.'); |
| 86 | + } |
| 87 | + |
| 88 | + const thrown = errorListener(actual); |
| 89 | + |
| 90 | + if (!expectInProd && process.env.NODE_ENV === 'production') { |
| 91 | + if (thrown !== undefined) { |
| 92 | + throw new chai.AssertionError( |
| 93 | + `Expected function not to throw an error in production mode, but it threw ${throwDescription( |
| 94 | + thrown |
| 95 | + )}.` |
| 96 | + ); |
| 97 | + } |
| 98 | + } else if (thrown === undefined) { |
| 99 | + throw new chai.AssertionError( |
| 100 | + `Expected function to throw an ${ |
| 101 | + expectedErrorCtor.name |
| 102 | + } error in development mode "${ |
| 103 | + expectedMessage ? 'with message ' + expectedMessage : '' |
| 104 | + }".` |
| 105 | + ); |
| 106 | + } else if (!matchError(thrown, expectedErrorCtor, expectedMessage)) { |
| 107 | + throw new chai.AssertionError( |
| 108 | + `Expected function to throw an ${ |
| 109 | + expectedErrorCtor.name |
| 110 | + } error in development mode "${ |
| 111 | + expectedMessage ? 'with message ' + expectedMessage : '' |
| 112 | + }", but it threw ${throwDescription(thrown)}.` |
| 113 | + ); |
| 114 | + } |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +/** @type {Chai.ChaiPlugin} */ |
| 119 | +export const registerErrorMatchers = (chai, utils) => { |
| 120 | + const matchers = { |
| 121 | + toThrowErrorDev: errorMatcherFactory(chai, utils, directErrorListener), |
| 122 | + toThrowCallbackReactionErrorDev: errorMatcherFactory( |
| 123 | + chai, |
| 124 | + utils, |
| 125 | + customElementCallbackReactionErrorListener |
| 126 | + ), |
| 127 | + toThrowCallbackReactionError: errorMatcherFactory( |
| 128 | + chai, |
| 129 | + utils, |
| 130 | + customElementCallbackReactionErrorListener, |
| 131 | + true |
| 132 | + ), |
| 133 | + toThrowCallbackReactionErrorEvenInSyntheticLifecycleMode: errorMatcherFactory( |
| 134 | + chai, |
| 135 | + utils, |
| 136 | + windowErrorListener, |
| 137 | + true |
| 138 | + ), |
| 139 | + }; |
| 140 | + |
| 141 | + for (const [name, impl] of Object.entries(matchers)) { |
| 142 | + utils.addMethod(chai.Assertion.prototype, name, impl); |
| 143 | + } |
| 144 | +}; |
0 commit comments