Skip to content

Commit dad442c

Browse files
committed
Refactor to use test wrapper script
1 parent e753418 commit dad442c

File tree

6 files changed

+113
-81
lines changed

6 files changed

+113
-81
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"start": "yarn build && cd build && node server.js",
5050
"build": "./utils/build_all.sh",
5151
"test": "jest --runInBand",
52-
"test_snapshot": "yarn test --forceExit eventTesting",
52+
"test_snapshot": "ts-node src/components/actions/dev_tools/testing/testSnapshot.ts --test",
5353
"pg": "exec ./utils/postgraphile_launch.sh",
5454
"pg_permissions": "exec ./utils/postgraphile_launch_permissions.sh",
5555
"dev_pg": "concurrently --kill-others-on-fail \"yarn dev\" \"yarn pg\"",

src/components/actions/dev_tools/eventTesting.test.ts

-74
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './routes'
2+
export * from './testTrigger'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Configure a snapshot to be used with this test suite.
2+
import { readFileSync } from 'fs'
3+
import { loadActionPlugins } from '../../../pluginsConnect'
4+
import { RequestProps, testTrigger } from '../'
5+
import useSnapshot from '../../../../components/snapshots/useSnapshot'
6+
import { TEST_SCRIPT_FOLDER, SNAPSHOT_FOLDER } from '../../../../constants'
7+
8+
interface TestData {
9+
name: string
10+
input: RequestProps
11+
output: Partial<ReturnType<typeof testTrigger>>
12+
}
13+
14+
const snapshotName = process.argv.pop() as string
15+
const testScriptFile = process.argv.pop() as string
16+
17+
console.log(testScriptFile, snapshotName)
18+
19+
const testData: TestData[] = JSON.parse(readFileSync(testScriptFile, 'utf-8')).tests
20+
21+
beforeAll((done) => {
22+
if (!snapshotName) done('No valid snapshot')
23+
// Load snapshot
24+
useSnapshot({ snapshotName }).then(({ success }) => {
25+
if (success) done()
26+
else done('Failed to load snapshot')
27+
})
28+
loadActionPlugins()
29+
})
30+
31+
testData.forEach(({ name, input, output }) => {
32+
test(name, async () => {
33+
const result = await testTrigger(input)
34+
expect(result).toMatchObject(output)
35+
})
36+
})

utils/release.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ require('dotenv').config()
22
import readlineSync from 'readline-sync'
33
import { writeFileSync } from 'fs'
44
import { promisify } from 'util'
5-
import { exec as execCallback, execSync } from 'child_process'
5+
import { exec as execCallback } from 'child_process'
6+
import { runTests } from '../src/components/actions/dev_tools/testing/testSnapshot'
67

78
const exec = promisify(execCallback)
89
const FRONT_END_PATH = process.env.FRONT_END_PATH
@@ -31,11 +32,13 @@ const release = async () => {
3132

3233
console.log('Starting build tests...')
3334

34-
try {
35-
execSync(`yarn test_snapshot ${TEST_SUITE}`, { stdio: 'inherit' })
36-
} catch {
37-
console.log('Build test FAIL!\n')
38-
process.exit(1)
35+
const result = await runTests()
36+
37+
if (!result) {
38+
console.log(
39+
`CAUTION: The test suite in ${TEST_SUITE} did not pass. Are you sure you wish to proceed with this build?`
40+
)
41+
if (!(await userRespondsYes())) process.exit(0)
3942
}
4043

4144
const releaseType: ReleaseType = (process.argv[2] || '--prerelease') as ReleaseType

0 commit comments

Comments
 (0)