|
| 1 | +/* |
| 2 | +Wrapper for snapshot/build testing suite. The wrapper is so we can capture the |
| 3 | +result of the test and act on it accordingly. |
| 4 | +*/ |
| 5 | + |
| 6 | +import { execSync } from 'child_process' |
| 7 | +import { existsSync, readFileSync } from 'fs' |
| 8 | +import { TEST_SCRIPT_FOLDER, SNAPSHOT_FOLDER } from '../../../../constants' |
| 9 | + |
| 10 | +const isManualTest = process.argv[2] === '--test' |
| 11 | + |
| 12 | +const lastArg = process.argv.pop() as string |
| 13 | + |
| 14 | +const inputTest = lastArg.match(/\.ts$/) || lastArg === '--test' ? null : lastArg |
| 15 | + |
| 16 | +if (inputTest) console.log('Requested test suite', inputTest) |
| 17 | + |
| 18 | +let testScriptFile: string | undefined |
| 19 | +let snapshotName: string | undefined |
| 20 | + |
| 21 | +// Check for test script in either (in this order): |
| 22 | +// - "snapshot_test_scripts" folder -- match {inputTest}.json |
| 23 | +// - snapshot named ${inputTest} -- look for "tests.json" within |
| 24 | +// - .env file: BUILD_TEST_SNAPSHOT |
| 25 | +if (!inputTest) { |
| 26 | + console.log('No test file or snapshot specified, checking .env file...') |
| 27 | + snapshotName = process.env.BUILD_TEST_SNAPSHOT |
| 28 | + testScriptFile = `${SNAPSHOT_FOLDER}/${process.env.BUILD_TEST_SNAPSHOT}/tests.json` |
| 29 | + if (!snapshotName) { |
| 30 | + console.log('NO SNAPSHOT DEFINED IN .env') |
| 31 | + process.exit(0) |
| 32 | + } |
| 33 | +} else { |
| 34 | + if (existsSync(`${TEST_SCRIPT_FOLDER}/${inputTest}.json`)) { |
| 35 | + testScriptFile = `${TEST_SCRIPT_FOLDER}/${inputTest}.json` |
| 36 | + const testScript = JSON.parse(readFileSync(testScriptFile, 'utf-8')) |
| 37 | + snapshotName = testScript.snapshot |
| 38 | + console.log('Test script found:', `${inputTest}.json`) |
| 39 | + } else { |
| 40 | + console.log(`${inputTest} not found in "snapshot_test_scripts". Checking snapshots...`) |
| 41 | + if (existsSync(`${SNAPSHOT_FOLDER}/${inputTest}/tests.json`)) { |
| 42 | + testScriptFile = `${SNAPSHOT_FOLDER}/${inputTest}/tests.json` |
| 43 | + const testScript = JSON.parse(readFileSync(testScriptFile, 'utf-8')) |
| 44 | + snapshotName = testScript.snapshot |
| 45 | + console.log('Test script found in snapshot:', snapshotName) |
| 46 | + } else { |
| 47 | + console.log(`Test file not found in snapshot ${inputTest}`) |
| 48 | + process.exit(0) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +export const runTests = async () => { |
| 54 | + try { |
| 55 | + execSync(`yarn test --forceExit triggerEvents ${testScriptFile} ${snapshotName}`, { |
| 56 | + stdio: 'inherit', |
| 57 | + }) |
| 58 | + return true |
| 59 | + } catch { |
| 60 | + console.error('FAILED TEST :(') |
| 61 | + return false |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +if (isManualTest) runTests() |
0 commit comments