-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
hooks.ts
123 lines (110 loc) · 3.37 KB
/
hooks.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { After, Before, formatterHelpers, ITestCaseHookParameter } from '../../'
import fs from 'fs'
import fsExtra from 'fs-extra'
import path from 'path'
import tmp from 'tmp'
import { doesHaveValue } from '../../src/value_checker'
import { World } from './world'
import { warnUserAboutEnablingDeveloperMode } from './warn_user_about_enabling_developer_mode'
const projectPath = path.join(__dirname, '..', '..')
Before('@debug', function (this: World) {
this.debug = true
})
Before('@spawn or @esm', function (this: World) {
this.spawn = true
})
Before(function (
this: World,
{ gherkinDocument, pickle }: ITestCaseHookParameter
) {
const { line } = formatterHelpers.PickleParser.getPickleLocation({
gherkinDocument,
pickle,
})
this.tmpDir = path.join(
projectPath,
'tmp',
`${path.basename(pickle.uri)}_${line.toString()}`
)
fsExtra.removeSync(this.tmpDir)
const tmpDirNodeModulesPath = path.join(this.tmpDir, 'node_modules')
const tmpDirCucumberPath = path.join(
tmpDirNodeModulesPath,
'@cucumber',
'cucumber'
)
try {
fsExtra.ensureSymlinkSync(projectPath, tmpDirCucumberPath)
} catch (error) {
warnUserAboutEnablingDeveloperMode(error)
}
this.localExecutablePath = path.join(projectPath, 'bin', 'cucumber.js')
})
Before('@esm', function (this: World) {
fsExtra.writeJSONSync(path.join(this.tmpDir, 'package.json'), {
name: 'feature-test-pickle',
type: 'module',
})
})
Before('@global-install', function (this: World) {
const tmpObject = tmp.dirSync({ unsafeCleanup: true })
// Symlink everything in node_modules so the fake global install has all the dependencies it needs
const projectNodeModulesPath = path.join(projectPath, 'node_modules')
const projectNodeModulesDirs = fs.readdirSync(projectNodeModulesPath)
const globalInstallNodeModulesPath = path.join(tmpObject.name, 'node_modules')
projectNodeModulesDirs.forEach((nodeModuleDir) => {
let pathsToLink = [nodeModuleDir]
if (nodeModuleDir[0] === '@') {
const scopeNodeModuleDirs = fs.readdirSync(
path.join(projectNodeModulesPath, nodeModuleDir)
)
pathsToLink = scopeNodeModuleDirs.map((x) => path.join(nodeModuleDir, x))
}
pathsToLink.forEach((pathToLink) => {
const globalInstallNodeModulePath = path.join(
globalInstallNodeModulesPath,
pathToLink
)
const projectNodeModulePath = path.join(
projectNodeModulesPath,
pathToLink
)
fsExtra.ensureSymlinkSync(
projectNodeModulePath,
globalInstallNodeModulePath
)
})
})
const globalInstallCucumberPath = path.join(
globalInstallNodeModulesPath,
'@cucumber',
'cucumber'
)
const itemsToCopy = ['bin', 'lib', 'package.json']
itemsToCopy.forEach((item) => {
fsExtra.copySync(
path.join(projectPath, item),
path.join(globalInstallCucumberPath, item)
)
})
this.globalExecutablePath = path.join(
globalInstallCucumberPath,
'bin',
'cucumber.js'
)
})
After(async function (this: World) {
if (this.reportServer?.started) {
await this.reportServer.stop()
}
if (
doesHaveValue(this.lastRun) &&
doesHaveValue(this.lastRun.error) &&
!this.verifiedLastRunError
) {
throw new Error(
`Last run errored unexpectedly. Output:\n\n${this.lastRun.output}\n\n` +
`Error Output:\n\n${this.lastRun.errorOutput}`
)
}
})