-
Notifications
You must be signed in to change notification settings - Fork 71
/
test.ts
68 lines (58 loc) · 1.85 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env node
import childProcess, { SpawnOptions } from 'node:child_process'
import path from 'node:path'
const { DOCKER, USE_UPSTREAM_BUNDLER } = process.env
const root = path.join(__dirname, '..', '..')
const docker = DOCKER || 'docker'
const bundler = USE_UPSTREAM_BUNDLER === '1' ? 'bundler-upstream' : 'bundler'
async function exec(command: string, args: string[], options: Omit<SpawnOptions, 'stdio'> = {}) {
const process = childProcess.spawn(command, args, { ...options, stdio: 'inherit' })
await new Promise((resolve, reject) => {
process.on('exit', (code) => {
if (code === 0) {
resolve(undefined)
} else {
reject(new Error(`'${command}' process exited with code ${code}`))
}
})
})
}
async function checkRpc(...urls: string[]) {
const statuses = await Promise.all(
urls.map(async (url) => {
try {
const response = await fetch(url)
return response.ok
} catch (err) {
return false
}
}),
)
return statuses.every((ok) => ok)
}
async function main() {
console.log('==> Starting docker containers...')
await exec(docker, ['compose', 'up', '-d', 'geth', bundler], { cwd: root })
console.log('==> Waiting for RPC endpoints')
const start = Date.now()
const timeout = 60 * 1000
while (!(await checkRpc('http://localhost:8545', 'http://localhost:3000'))) {
if (Date.now() - start > timeout) {
throw new Error('timeout waiting for local node and bundler to start')
}
}
try {
console.log('==> Running tests')
await exec('hardhat', ['test', '--network', 'localhost', '--grep', '@4337'])
} finally {
console.log('==> Shutting down')
await exec(docker, ['compose', 'down'], { cwd: root })
}
}
main().catch((err) => {
console.error('ERROR: ', err)
if (err.stderr) {
console.log(err.stderr)
}
process.exitCode = 1
})