-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathrunTests.ts
108 lines (91 loc) · 3.68 KB
/
runTests.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
import { isCI as isCi } from "ci-info"
import { createHash } from "crypto"
import { readJson, realpathSync } from "fs-extra"
import * as fs from "fs/promises"
import { tmpdir } from "os"
import * as path from "path"
import { deleteOldElectronVersion, downloadAllRequiredElectronVersions } from "./downloadElectron"
const baseDir = process.env.APP_BUILDER_TMP_DIR || realpathSync(tmpdir())
const APP_BUILDER_TMP_DIR = path.join(baseDir, `et-${createHash("md5").update(__dirname).digest("hex")}`)
runTests().catch(error => {
console.error(error.stack || error)
process.exit(1)
})
async function runTests() {
await fs.rm(APP_BUILDER_TMP_DIR, { recursive: true, force: true })
await fs.mkdir(APP_BUILDER_TMP_DIR, { recursive: true })
if (!process.env.CIRCLECI) {
await Promise.all([deleteOldElectronVersion(), downloadAllRequiredElectronVersions()])
}
const testFiles = process.env.TEST_FILES
const testPatterns: Array<string> = []
if (testFiles != null && testFiles.length !== 0) {
console.log(`Test files: ${testFiles}`)
testPatterns.push(...testFiles.split(","))
}
process.env.APP_BUILDER_TMP_DIR = APP_BUILDER_TMP_DIR
const rootDir = path.join(__dirname, "..", "..")
process.chdir(rootDir)
const config = (await readJson(path.join(rootDir, "package.json"))).jest
// use custom cache dir to avoid https://github.com/facebook/jest/issues/1903#issuecomment-261212137
config.cacheDirectory = process.env.JEST_CACHE_DIR || "/tmp/jest-electron-builder-tests"
config.bail = process.env.TEST_BAIL === "true"
let runInBand = false
const scriptArgs = process.argv.slice(2)
const testPathIgnorePatterns = config.testPathIgnorePatterns
if (scriptArgs.length > 0) {
for (const scriptArg of scriptArgs) {
console.log(`custom opt: ${scriptArg}`)
if ("runInBand" === scriptArg) {
runInBand = true
} else if (scriptArg.includes("=")) {
const equalIndex = scriptArg.indexOf("=")
const envName = scriptArg.substring(0, equalIndex)
let envValue = scriptArg.substring(equalIndex + 1)
if (envValue === "isCi") {
envValue = isCi ? "true" : "false"
}
process.env[envName] = envValue
console.log(`Custom env ${envName}=${envValue}`)
if (envName === "ALL_TESTS" && envValue === "false") {
config.cacheDirectory += "-basic"
}
} else if (scriptArg.startsWith("skip")) {
if (!isCi) {
const suffix = scriptArg.substring("skip".length)
if (scriptArg === "skipArtifactPublisher") {
testPathIgnorePatterns.push("[\\/]{1}ArtifactPublisherTest.js$")
config.cacheDirectory += `-${suffix}`
} else {
throw new Error(`Unknown opt ${scriptArg}`)
}
}
} else {
config[scriptArg] = true
}
}
}
const jestOptions: any = {
verbose: true,
updateSnapshot: process.env.UPDATE_SNAPSHOT === "true",
config,
runInBand,
projects: [rootDir],
}
if (testPatterns.length > 0) {
jestOptions.testPathPattern = testPatterns.map(it => (it.endsWith(".ts") || it.endsWith("*") ? it : `${it}\\.ts$`))
}
if (process.env.CIRCLECI != null || process.env.TEST_JUNIT_REPORT === "true") {
jestOptions.reporters = ["default", "jest-junit"]
}
const testResult = await require("@jest/core").runCLI(jestOptions, jestOptions.projects)
const exitCode = testResult.results == null || testResult.results.success ? 0 : testResult.globalConfig.testFailureExitCode
if (isCi) {
process.exit(exitCode)
}
await fs.rm(APP_BUILDER_TMP_DIR, { recursive: true, force: true })
process.exitCode = exitCode
if (testResult.globalConfig.forceExit) {
process.exit(exitCode)
}
}