generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjest_config_template.mjs
184 lines (163 loc) · 6.18 KB
/
jest_config_template.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// jest.config.js template for jest_test rule
import { existsSync, readFileSync } from "fs";
import * as path from "path";
const updateSnapshots = !!process.env.JEST_TEST__UPDATE_SNAPSHOTS;
const coverageEnabled = !!process.env.COVERAGE_DIR;
const autoConfReporters = !!"{{AUTO_CONF_REPORTERS}}";
const autoConfTestSequencer = !!"{{AUTO_CONF_TEST_SEQUENCER}}";
const userConfigShortPath = "{{USER_CONFIG_SHORT_PATH}}";
const userConfigPath = "{{USER_CONFIG_PATH}}";
const generatedConfigShortPath = "{{GENERATED_CONFIG_SHORT_PATH}}";
const bazelSequencerPath = _resolveRunfilesPath(
"{{BAZEL_SEQUENCER_SHORT_PATH}}"
);
const bazelSnapshotReporterPath = _resolveRunfilesPath(
"{{BAZEL_SNAPSHOT_REPORTER_SHORT_PATH}}"
);
const bazelSnapshotResolverPath = _resolveRunfilesPath(
"{{BAZEL_SNAPSHOT_RESOLVER_SHORT_PATH}}"
);
if (
!updateSnapshots &&
process.env.JEST_JUNIT_OUTPUT_FILE != process.env.XML_OUTPUT_FILE
) {
console.error(
`WARNING: aspect_rules_jest[jest_test]: expected JEST_JUNIT_OUTPUT_FILE environment variable to be set to ${process.env.XML_OUTPUT_FILE} in jest_test target ${process.env.TEST_TARGET}`
);
}
function _resolveRunfilesPath(rootpath) {
return path.join(
process.env.RUNFILES,
process.env.JS_BINARY__WORKSPACE,
rootpath
);
}
function _resolveExecrootPath(execpath) {
return path.join(process.env.JS_BINARY__EXECROOT, execpath);
}
function _hasReporter(config, name) {
if (!config.reporters) {
config.reporters = [];
}
for (const r of config.reporters) {
if (Array.isArray(r)) {
return r.length > 0 && r[0] == name;
} else {
return r == name;
}
}
}
function _addReporter(config, name, reporter = undefined) {
if (!config.reporters) {
config.reporters = [];
}
if (!_hasReporter(config, name)) {
config.reporters.push(reporter ? reporter : name);
}
}
let config = {};
if (userConfigShortPath) {
if (path.extname(userConfigShortPath).toLowerCase() == ".json") {
// On Windows, import does not like paths that start with the drive letter such as
// `c:\...` so we prepend the with `file://` so node is happy.
config = (
await import("file://" + _resolveRunfilesPath(userConfigShortPath), {
assert: { type: "json" },
})
).default;
} else {
// On Windows, import does not like paths that start with the drive letter such as
// `c:\...` so we prepend the with `file://` so node is happy.
const userConfigModule = (
await import("file://" + _resolveRunfilesPath(userConfigShortPath))
).default;
if (typeof userConfigModule === "function") {
config = await userConfigModule();
} else {
config = userConfigModule;
}
}
}
// Needed for Jest to walk the filesystem to find inputs.
// See https://github.com/facebook/jest/pull/9351
config.haste = { enableSymlinks: true };
// https://jestjs.io/docs/cli#--watchman. Whether to use watchman for file crawling. Defaults
// to true. Disable using --no-watchman. Watching is ibazel's job
config.watchman = false;
// Auto configure reporters
if (autoConfReporters) {
// Default reporter should always be configured
_addReporter(config, "default");
// jest-junit reporter is only auto-configured if this is a test target
if (!updateSnapshots) {
_addReporter(config, "jest-junit", [
"jest-junit",
{ outputFile: process.env.XML_OUTPUT_FILE },
]);
}
}
if (!updateSnapshots) {
// The Bazel snapshot reporter is always configured if this is a test target
_addReporter(config, bazelSnapshotReporterPath);
}
// Auto configure the Bazel test sequencer (if this is a test target)
if (autoConfTestSequencer) {
if (config.testSequencer) {
console.error(`WARNING: aspect_rules_jest[jest_test]: user supplied Jest config testSequencer value '${config.testSequencer}' will be overridden by jest_test in target ${process.env.TEST_TARGET}.
See https://jestjs.io/docs/configuration#testsequencer-string for more information on Jest testSequencer config option.
Set auto_configure_test_sequencer to False to disable this override.`);
}
config.testSequencer = bazelSequencerPath;
}
// If this is an update snapshot target the configure the Bazel snapshot resolver
if (updateSnapshots) {
if (config.snapshotResolver) {
const snapshotResolverPath = path.isAbsolute(config.snapshotResolver)
? config.snapshotResolver
: path.resolve(
_resolveExecrootPath(userConfigPath),
"..",
config.snapshotResolver
);
if (!existsSync(snapshotResolverPath)) {
throw new Error(
`configured snapshotResolver '${config.snapshotResolver}' not found at ${snapshotResolverPath}`
);
}
process.env.JEST_TEST__USER_SNAPSHOT_RESOLVER = snapshotResolverPath;
}
config.snapshotResolver = bazelSnapshotResolverPath;
}
if (coverageEnabled) {
config.collectCoverage = true;
let coverageFile = path.basename(process.env.COVERAGE_OUTPUT_FILE);
let coverageDirectory = path.dirname(process.env.COVERAGE_OUTPUT_FILE);
if (process.env.SPLIT_COVERAGE_POST_PROCESSING == "1") {
// in split coverage post processing mode bazel assumes that the COVERAGE_OUTPUT_FILE
// will be created by lcov_merger which runs as a separate action with everything in
// COVERAGE_DIR provided as inputs. so we'll just create the final coverage at
// `COVERAGE_DIR/coverage.dat` which then later moved by merger.sh to final location.
coverageDirectory = process.env.COVERAGE_DIR;
coverageFile = "coverage.dat";
}
config.coverageDirectory = coverageDirectory;
config.coverageReporters = ["text", ["lcovonly", { file: coverageFile }]];
// Glob pattern paths for which files to cover must be relative to this
// jest config file in runfiles.
const jestConfigDir = path.dirname(
_resolveRunfilesPath(generatedConfigShortPath)
);
// Only generate coverage for files declared in the COVERAGE_MANIFEST
config.collectCoverageFrom = readFileSync(process.env.COVERAGE_MANIFEST)
.toString("utf8")
.split("\n")
.filter((f) => f != "")
.map((f) => path.relative(jestConfigDir, f));
}
if (process.env.JS_BINARY__LOG_DEBUG) {
console.error(
"DEBUG: aspect_rules_jest[jest_test]: config:",
JSON.stringify(config, null, 2)
);
}
export default config;