|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; |
| 12 | + |
| 13 | +// Must not be a substring of this test's own bundle URL, which appears in |
| 14 | +// every stack trace. |
| 15 | +const SOURCE_URL = 'split-chunk-under-test.bundle'; |
| 16 | + |
| 17 | +function getHelper(): (code: string, sourceUrl?: string) => unknown { |
| 18 | + // $FlowFixMe[prop-missing] |
| 19 | + const helper = global.globalEvalWithSourceUrl; |
| 20 | + if (typeof helper !== 'function') { |
| 21 | + throw new Error( |
| 22 | + `Expected global.globalEvalWithSourceUrl to be a function, got ${typeof helper}`, |
| 23 | + ); |
| 24 | + } |
| 25 | + return helper; |
| 26 | +} |
| 27 | + |
| 28 | +function getStack(fn: () => unknown): string { |
| 29 | + try { |
| 30 | + fn(); |
| 31 | + } catch (e) { |
| 32 | + return String(e?.stack ?? ''); |
| 33 | + } |
| 34 | + throw new Error('Expected the evaluated code to throw'); |
| 35 | +} |
| 36 | + |
| 37 | +describe('globalEvalWithSourceUrl', () => { |
| 38 | + afterEach(() => { |
| 39 | + // $FlowFixMe[prop-missing] |
| 40 | + delete globalThis.__fantomEvalMarker; |
| 41 | + }); |
| 42 | + |
| 43 | + it('is installed on the bridgeless runtime', () => { |
| 44 | + // $FlowFixMe[prop-missing] |
| 45 | + expect(typeof global.globalEvalWithSourceUrl).toBe('function'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('evaluates the same source as JS eval() in the global scope', () => { |
| 49 | + const source = 'globalThis.__fantomEvalMarker = 17; 17'; |
| 50 | + |
| 51 | + // eslint-disable-next-line no-eval |
| 52 | + expect(eval(source)).toBe(17); |
| 53 | + // $FlowFixMe[prop-missing] |
| 54 | + expect(globalThis.__fantomEvalMarker).toBe(17); |
| 55 | + |
| 56 | + // $FlowFixMe[prop-missing] |
| 57 | + delete globalThis.__fantomEvalMarker; |
| 58 | + |
| 59 | + expect(getHelper()(source, SOURCE_URL)).toBe(17); |
| 60 | + // $FlowFixMe[prop-missing] |
| 61 | + expect(globalThis.__fantomEvalMarker).toBe(17); |
| 62 | + }); |
| 63 | + |
| 64 | + it('attributes evaluated code to the given source URL, unlike eval()', () => { |
| 65 | + const source = 'throw new Error("thrown from evaluated source")'; |
| 66 | + |
| 67 | + // eslint-disable-next-line no-eval |
| 68 | + const evalStack = getStack(() => eval(source)); |
| 69 | + const helperStack = getStack(() => getHelper()(source, SOURCE_URL)); |
| 70 | + |
| 71 | + expect(evalStack).not.toContain(SOURCE_URL); |
| 72 | + expect(helperStack).toContain(SOURCE_URL); |
| 73 | + }); |
| 74 | + |
| 75 | + it('rejects an invalid argument count', () => { |
| 76 | + const helper = getHelper(); |
| 77 | + // $FlowFixMe[incompatible-type] |
| 78 | + expect(() => helper()).toThrow( |
| 79 | + 'globalEvalWithSourceUrl arg count must be 1 or 2', |
| 80 | + ); |
| 81 | + }); |
| 82 | +}); |
0 commit comments