-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (65 loc) · 1.99 KB
/
index.js
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
const NodeEnvironment = require("jest-environment-node")
const r = require("testenv-recycler")
const path = require("path")
const isInteger = (str) => /^\+?(0|[1-9]\d*)$/.test(str)
class TestEnvRecyclerEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context)
this.docblockPragmas = context.docblockPragmas
this.testPath = context.testPath
this.name = path.basename(this.testPath)
}
async setup() {
await super.setup()
this.recycler = new r.Recycler({
name: this.name,
hostname: this.global.process.env.RECYCLER_HOSTNAME,
username: this.global.process.env.RECYCLER_USERNAME,
password: this.global.process.env.RECYCLER_PASSWORD,
})
const countStr = this.docblockPragmas["testenv-recycler-count"] || "1"
if (!isInteger(countStr)) {
console.log(
this.name + " - @testenv-recycler-count must be a positive integer",
)
throw new Error(`@testenv-recycler-count must be a positive integer`)
}
const count = parseInt(countStr, 10)
try {
await this.recycler.login()
} catch (e) {
console.log(this.name + " - Failed to log in", e)
throw e
}
console.log(this.name + " - Logged in")
try {
this.global.reservation = await this.recycler.createReservation({
name: this.name,
count,
})
} catch (e) {
console.log(this.name + " - Failed to create the reservation", e)
throw e
}
}
async teardown() {
if (this.global.reservation) {
try {
await this.recycler.releaseReservation(this.global.reservation.id)
console.log(
this.name + " - Released reservation " + this.global.reservation.id,
)
} catch (e) {
console.log(
this.name +
" - Failed to release the reservation " +
this.global.reservation.id,
e,
)
throw e
}
}
await super.teardown()
}
}
module.exports = TestEnvRecyclerEnvironment