Skip to content

Commit bde750b

Browse files
authored
fix(jasmine): replace deprecated Jasmine APIs that have been removed in version 4 (#3283)
With this change we replace APIs that have been deprecated in version 4 and removed in version 4 ``` Jasmine#onComplete is deprecated. Instead of calling onComplete, set the Jasmine instance's exitOnCompletion property to false and use the promise returned from the execute method. ``` Also this addresses the breaking change in version 4 https://github.com/jasmine/jasmine/blob/main/release_notes/4.0.0.md#changes-that-affect-custom-reporters in a backward compatible manner. Closes #3289
1 parent 5dff941 commit bde750b

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed

e2e/BUILD.bazel

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,29 @@ e2e_integration_test(
5151
tags = ["no-bazelci-windows"],
5252
)
5353

54-
e2e_integration_test(
55-
name = "e2e_jasmine",
54+
[e2e_integration_test(
55+
name = "e2e_jasmine_%s" % jasmine_version.replace(".", "_"),
5656
npm_packages = {
5757
"//packages/jasmine:npm_package": "@bazel/jasmine",
5858
},
59+
# use these package.json packages instead
60+
package_json_substitutions = {
61+
"jasmine": jasmine_version,
62+
"jasmine-core": jasmine_version,
63+
},
5964
# TODO: figure out why this fails on Windows since setting
6065
# symlink_node_modules to False in the test WORKSPACE
6166
tags = ["no-bazelci-windows"],
62-
)
67+
workspace_root = "jasmine",
68+
) for jasmine_version in [
69+
# TODO(6.0): remove old API tests
70+
# Old API
71+
"2.99.x",
72+
"3.9.x",
73+
# New API
74+
"4.0.x",
75+
"3.10.x",
76+
]]
6377

6478
e2e_integration_test(
6579
name = "e2e_node_loader_no_preserve_symlinks",

packages/jasmine/jasmine_runner.js

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ const UTF8 = {
2727

2828
// These exit codes are handled specially by Bazel:
2929
// https://github.com/bazelbuild/bazel/blob/486206012a664ecb20bdb196a681efc9a9825049/src/main/java/com/google/devtools/build/lib/util/ExitCode.java#L44
30+
const BAZEL_EXIT_SUCCESS = 0;
3031
const BAZEL_EXIT_TESTS_FAILED = 3;
3132
const BAZEL_EXIT_NO_TESTS_FOUND = 4;
33+
const BAZEL_EXIT_INTERRUPTED = 8;
3234

3335
// Test sharding support
3436
// See https://docs.bazel.build/versions/main/test-encyclopedia.html#role-of-the-test-runner
@@ -91,13 +93,6 @@ async function main(args) {
9193
.filter(f => IS_TEST_FILE.test(f))
9294
.forEach(f => jrunner.addSpecFile(f));
9395

94-
var noSpecsFound = true;
95-
jrunner.addReporter({
96-
specDone: () => {
97-
noSpecsFound = false
98-
},
99-
});
100-
10196
if (JUnitXmlReporter) {
10297
const testOutputFile = process.env.XML_OUTPUT_FILE;
10398
if (testOutputFile) {
@@ -107,22 +102,15 @@ async function main(args) {
107102
consolidate: true,
108103
consolidateAll: true
109104
}));
105+
106+
// addReporter throws away the default console reporter
107+
// so we need to add it back
108+
jrunner.configureDefaultReporter({});
110109
} else {
111110
console.warn('Skipping XML Test Result: $XML_OUTPUT_FILE not found.')
112111
}
113112
}
114113

115-
// addReporter throws away the default console reporter
116-
// so we need to add it back
117-
jrunner.configureDefaultReporter({});
118-
119-
jrunner.onComplete((passed) => {
120-
let exitCode = passed ? 0 : BAZEL_EXIT_TESTS_FAILED;
121-
if (noSpecsFound) exitCode = BAZEL_EXIT_NO_TESTS_FOUND;
122-
123-
process.exit(exitCode);
124-
});
125-
126114
if (TOTAL_SHARDS) {
127115
// Since we want to collect all the loaded specs, we have to do this after
128116
// loadSpecs() in jasmine/lib/jasmine.js
@@ -133,7 +121,7 @@ async function main(args) {
133121
// Patch the inner execute function to do our filtering first.
134122
const env = jasmine.getEnv();
135123
const originalExecute = env.execute.bind(env);
136-
env.execute = async () => {
124+
env.execute = () => {
137125
const allSpecs = getAllSpecs(env);
138126
// Partition the specs among the shards.
139127
// This ensures that the specs are evenly divided over the shards.
@@ -144,8 +132,10 @@ async function main(args) {
144132
const end = allSpecs.length * (SHARD_INDEX + 1) / TOTAL_SHARDS;
145133
const enabledSpecs = allSpecs.slice(start, end);
146134
env.configure({specFilter: (s) => enabledSpecs.includes(s.id)});
147-
await originalExecute();
135+
136+
return originalExecute();
148137
};
138+
149139
// Special case!
150140
// To allow us to test sharding, always run the specs in the order they are declared
151141
if (process.env['TEST_WORKSPACE'] === 'build_bazel_rules_nodejs' &&
@@ -154,22 +144,68 @@ async function main(args) {
154144
}
155145
}
156146

157-
await jrunner.execute();
147+
// TODO(6.0): remove support for deprecated versions of Jasmine that use the old API &
148+
// remember to update the `peerDependencies` as well.
149+
// Jasmine versions prior to 3.10.0 should use the old API.
150+
if (/^3\.[1-9]\.|^2\./.test(jrunner.coreVersion())) {
151+
console.warn(`DEPRECATED: Support for Jasmine versions prior to '3.10.x' is deprecated in '@bazel/jasmine'.`);
152+
153+
// Old Jasmine API.
154+
let noSpecsFound = true;
155+
jrunner.addReporter({
156+
specDone: () => {
157+
noSpecsFound = false
158+
},
159+
});
158160

159-
return 0;
161+
jrunner.onComplete((passed) => {
162+
let exitCode = passed ? BAZEL_EXIT_SUCCESS : BAZEL_EXIT_TESTS_FAILED;
163+
if (noSpecsFound) exitCode = BAZEL_EXIT_NO_TESTS_FOUND;
164+
165+
process.exit(exitCode);
166+
});
167+
168+
// addReporter throws away the default console reporter
169+
// so we need to add it back
170+
jrunner.configureDefaultReporter({});
171+
await jrunner.execute();
172+
173+
return BAZEL_EXIT_SUCCESS;
174+
}
175+
176+
// New Jasmine API.
177+
jrunner.exitOnCompletion = false;
178+
const { overallStatus, incompleteReason } = await jrunner.execute();
179+
180+
switch (overallStatus) {
181+
case 'passed':
182+
return BAZEL_EXIT_SUCCESS;
183+
case 'incomplete':
184+
return incompleteReason === 'No specs found' ? BAZEL_EXIT_NO_TESTS_FOUND : BAZEL_EXIT_INTERRUPTED;
185+
case 'failed':
186+
default:
187+
return BAZEL_EXIT_TESTS_FAILED;
188+
}
160189
}
161190

162191
function getAllSpecs(jasmineEnv) {
163-
var specs = [];
192+
const specs = [];
164193

165194
// Walk the test suite tree depth first and collect all test specs
166-
var stack = [jasmineEnv.topSuite()];
167-
var currentNode;
195+
const stack = [jasmineEnv.topSuite()];
196+
let currentNode;
168197
while (currentNode = stack.pop()) {
169-
if (currentNode instanceof jasmine.Spec) {
198+
if (!currentNode) {
199+
continue;
200+
}
201+
202+
const { children, id } = currentNode;
203+
if (Array.isArray(children)) {
204+
// This is a suite.
205+
stack.push(...children);
206+
} else if (id) {
207+
// This is a spec.
170208
specs.unshift(currentNode);
171-
} else if (currentNode instanceof jasmine.Suite) {
172-
stack = stack.concat(currentNode.children);
173209
}
174210
}
175211

0 commit comments

Comments
 (0)