@@ -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 ;
3031const BAZEL_EXIT_TESTS_FAILED = 3 ;
3132const 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
162191function 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