-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-e2e.mjs
85 lines (67 loc) · 2.07 KB
/
test-e2e.mjs
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import * as dotenv from 'dotenv'
import { type } from 'node:os'
import path, { dirname } from 'node:path'
import { GenericContainer } from 'testcontainers'
import { fileURLToPath } from 'url'
import { $ } from 'zx'
import 'zx/globals'
const __dirname = dirname(fileURLToPath(import.meta.url))
;(async function main() {
//disable automatic cleanup of test containers
process.env.TESTCONTAINERS_RYUK_DISABLED = true
let mongoContainer
let server
let hasError = false
try {
dotenv.config({
debug: true,
override: true,
path: path.resolve(__dirname, '../', '.env.test')
})
mongoContainer = await startMongo()
process.env.NODE_ENV = 'test'
process.env.E2E = 'true'
server = $`pnpm start`
await $`pnpm cypress run --browser chrome`
} catch (e) {
hasError = true
console.log(e)
} finally {
if (mongoContainer) {
console.log('stopping mongo')
await mongoContainer.stop()
console.log('mongo stopped')
}
if (server) {
console.log('stopping server')
await server.kill()
console.log('server stopped')
}
process.exit(hasError ? 1 : 0)
}
})()
async function startMongo() {
const remoteContainers =
Boolean(process.env.REMOTE_CONTAINERS) || Boolean(process.env.CODESPACES)
const newtworkAlias = 'mongo-test-db'
const mongoContainer = new GenericContainer('mongo:5.0.8').withExposedPorts(
27017
)
console.log('using remote containers: ', remoteContainers)
if (type() === 'Linux') {
console.log('mongo: using tmpfs mount')
mongoContainer.withTmpFs({ '/data/db': '' })
}
if (remoteContainers) {
mongoContainer
.withNetworkMode('development')
.withNetworkAliases(newtworkAlias)
}
const mongoStarted = await mongoContainer.start()
const host = remoteContainers ? newtworkAlias : 'localhost'
const port = remoteContainers ? '27017' : mongoStarted.getMappedPort(27017)
process.env.MONGO_DB_URI = `mongodb://${host}:${port}`
console.log('mongo db started')
console.log('MONGO_DB_URI: ', process.env.MONGO_DB_URI)
return mongoStarted
}