|
| 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-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const injectDebuggerStatementsInTests = require('../babel-plugins/inject-debugger-statements-in-tests'); |
| 14 | +const {getFantomTestName} = require('../fantomTestFiles'); |
| 15 | +const fantomJestConfig = require('../jest.config'); |
| 16 | +const {transformSync} = require('@babel/core'); |
| 17 | +const path = require('node:path'); |
| 18 | + |
| 19 | +const FANTOM_TEST_PATHS = [ |
| 20 | + 'Libraries/Components/View/__tests__/View-itest.js', |
| 21 | + 'Libraries/Components/View/__tests__/View-itest.fb.js', |
| 22 | + 'my-library/__tests__/Example-itest.ts', |
| 23 | + 'my-library/__tests__/Example-itest.tsx', |
| 24 | + 'my-library/__tests__/Example-itest.fb.ts', |
| 25 | + 'my-library/__tests__/Example-itest.fb.tsx', |
| 26 | +]; |
| 27 | + |
| 28 | +const GENERATED_ENTRYPOINT_PATH = |
| 29 | + 'private/react-native-fantom/.out/js-builds/run-1/1700000000000-View-itest.js'; |
| 30 | + |
| 31 | +const OTHER_PATHS = [ |
| 32 | + 'Libraries/Components/View/__tests__/View-test.js', |
| 33 | + 'my-library/__tests__/Example-itest.jsx', |
| 34 | + 'my-library/__tests__/Example-itest.d.ts', |
| 35 | + 'Libraries/Components/View/View-itest.js', |
| 36 | + GENERATED_ENTRYPOINT_PATH, |
| 37 | +]; |
| 38 | + |
| 39 | +const testRegex = new RegExp(fantomJestConfig.testRegex); |
| 40 | + |
| 41 | +function isRunByJest(testPath: string): boolean { |
| 42 | + return ( |
| 43 | + fantomJestConfig.moduleFileExtensions.includes( |
| 44 | + path.extname(testPath).slice(1), |
| 45 | + ) && testRegex.test(testPath) |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +function onWindows(testPath: string): string { |
| 50 | + return `C:\\repository\\${testPath.replaceAll('/', '\\')}`; |
| 51 | +} |
| 52 | + |
| 53 | +function transformTestFile(filename: string, code: string): ?string { |
| 54 | + return transformSync(code, { |
| 55 | + babelrc: false, |
| 56 | + configFile: false, |
| 57 | + filename, |
| 58 | + plugins: [injectDebuggerStatementsInTests], |
| 59 | + })?.code; |
| 60 | +} |
| 61 | + |
| 62 | +describe('Fantom test files', () => { |
| 63 | + it.each(FANTOM_TEST_PATHS)('Jest runs %s', testPath => { |
| 64 | + expect(isRunByJest(`/repository/${testPath}`)).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it.each(OTHER_PATHS)('Jest skips %s', testPath => { |
| 68 | + expect(isRunByJest(`/repository/${testPath}`)).toBe(false); |
| 69 | + }); |
| 70 | + |
| 71 | + it.each(FANTOM_TEST_PATHS)( |
| 72 | + 'starts %s with a debugger statement, once', |
| 73 | + testPath => { |
| 74 | + const transformed = transformTestFile( |
| 75 | + `/repository/${testPath}`, |
| 76 | + 'run();', |
| 77 | + ); |
| 78 | + |
| 79 | + expect(transformed).toBe('debugger;\nrun();'); |
| 80 | + expect( |
| 81 | + transformTestFile(`/repository/${testPath}`, transformed ?? ''), |
| 82 | + ).toBe(transformed); |
| 83 | + }, |
| 84 | + ); |
| 85 | + |
| 86 | + it.each(OTHER_PATHS)('leaves %s without a debugger statement', testPath => { |
| 87 | + expect(transformTestFile(`/repository/${testPath}`, 'run();')).toBe( |
| 88 | + 'run();', |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it.each(FANTOM_TEST_PATHS)( |
| 93 | + 'starts %s with a debugger statement on a Windows path', |
| 94 | + testPath => { |
| 95 | + expect(transformTestFile(onWindows(testPath), 'run();')).toBe( |
| 96 | + 'debugger;\nrun();', |
| 97 | + ); |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + it.each(OTHER_PATHS)( |
| 102 | + 'leaves %s without a debugger statement on a Windows path', |
| 103 | + testPath => { |
| 104 | + expect(transformTestFile(onWindows(testPath), 'run();')).toBe('run();'); |
| 105 | + }, |
| 106 | + ); |
| 107 | + |
| 108 | + it.each([ |
| 109 | + `/home/__tests__/react-native/${GENERATED_ENTRYPOINT_PATH}`, |
| 110 | + `C:\\__tests__\\react-native\\${GENERATED_ENTRYPOINT_PATH.replaceAll('/', '\\')}`, |
| 111 | + ])( |
| 112 | + 'leaves the generated entrypoint %s of a checkout in a __tests__ folder without a debugger statement', |
| 113 | + filename => { |
| 114 | + expect(transformTestFile(filename, 'run();')).toBe('run();'); |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + it.each([ |
| 119 | + ['Libraries/Components/View/__tests__/View-itest.js', 'View'], |
| 120 | + ['my-library/__tests__/Example-itest.ts', 'Example'], |
| 121 | + ['my-library/__tests__/Example-itest.tsx', 'Example'], |
| 122 | + [ |
| 123 | + 'Libraries/Components/View/__tests__/View-itest.fb.js', |
| 124 | + 'View-itest.fb.js', |
| 125 | + ], |
| 126 | + ['my-library/__tests__/Example-itest.fb.tsx', 'Example-itest.fb.tsx'], |
| 127 | + ])('names %s as %s in titles', (testPath, name) => { |
| 128 | + expect(getFantomTestName(`/repository/${testPath}`)).toBe(name); |
| 129 | + }); |
| 130 | +}); |
0 commit comments