Skip to content

Commit 3c4ef58

Browse files
alan-agius4alexeagle
authored andcommitted
fix(jasmine): unhanded promise rejection causes tests suit to pass
Currently unhandled promise rejection caused tests suit to pass. Example: ``` yarn bazel test //packages/angular_devkit/core:core_test --test_summary=detailed --test_output=all INFO: Analyzed target //packages/angular_devkit/core:core_test (2 packages loaded, 148 targets configured). INFO: Found 1 test target... Target //packages/angular_devkit/core:core_test up-to-date: dist/bin/packages/angular_devkit/core/core_test.sh dist/bin/packages/angular_devkit/core/core_test_loader.js dist/bin/packages/angular_devkit/core/core_test_require_patch.js INFO: Elapsed time: 0.357s, Critical Path: 0.02s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action PASSED: //packages/angular_devkit/core:core_test (see /private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/bazel-out/darwin-fastbuild/testlogs/packages/angular_devkit/core/core_test/test.log) INFO: From Testing //packages/angular_devkit/core:core_test ==================== Test output for //packages/angular_devkit/core:core_test: (node:11987) UnhandledPromiseRejectionWarning: Error: Cannot find module '/private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/node_modules/@angular-devkit/core/src/index.js'. Please verify that the package.json has a valid "main" entry at tryPackage (internal/modules/cjs/loader.js:303:19) at Function.Module._findPath (internal/modules/cjs/loader.js:516:18) at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27) at Function.Module._load (internal/modules/cjs/loader.js:725:27) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (packages/angular_devkit/core/src/virtual-fs/host/memory_spec.ts:10:1) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Loader.requireShim [as require_] (node_modules/jasmine/lib/loader.js:35:3) at node_modules/jasmine/lib/loader.js:28:12 at new Promise (<anonymous>) at Loader.load (node_modules/jasmine/lib/loader.js:27:12) at Jasmine.loadSpecs (node_modules/jasmine/lib/jasmine.js:91:23) at Jasmine.execute (node_modules/jasmine/lib/jasmine.js:267:3) (Use `node --trace-warnings ...` to show where the warning was created) (node:11987) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:11987) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ================================================================================ Test cases: finished with 1 passing and 0 failing out of 1 test cases Executed 0 out of 1 test: 1 test passes. ``` The reason for this is because Jasmine `execute` method returns a `Promise`, which was not handled properly. See: https://unpkg.com/browse/[email protected]/lib/jasmine.js#L267 Without this fix ``` yarn bazel test //packages/angular_devkit/core:core_test yarn run v1.22.10 INFO: Build options --action_env and --enable_runfiles have changed, discarding analysis cache. INFO: Analyzed target //packages/angular_devkit/core:core_test (3 packages loaded, 6322 targets configured). INFO: Found 1 test target... Target //packages/angular_devkit/core:core_test up-to-date: dist/bin/packages/angular_devkit/core/core_test.sh dist/bin/packages/angular_devkit/core/core_test_loader.js dist/bin/packages/angular_devkit/core/core_test_require_patch.js INFO: Elapsed time: 7.172s, Critical Path: 6.58s INFO: 4 processes: 2 local, 2 worker. INFO: Build completed successfully, 4 total actions //packages/angular_devkit/core:core_test PASSED in 0.5s Executed 1 out of 1 test: 1 test passes. INFO: Build completed successfully, 4 total actions ``` With this fix: ``` yarn bazel test //packages/angular_devkit/core:core_test yarn run v1.22.10 $ /Users/alanagius/git/angular-cli/node_modules/.bin/bazel test //packages/angular_devkit/core:core_test INFO: Analyzed target //packages/angular_devkit/core:core_test (1 packages loaded, 10 targets configured). INFO: Found 1 test target... FAIL: //packages/angular_devkit/core:core_test (see /private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/bazel-out/darwin-fastbuild/testlogs/packages/angular_devkit/core/core_test/test.log) INFO: From Testing //packages/angular_devkit/core:core_test: ==================== Test output for //packages/angular_devkit/core:core_test: [jasmine_runner.js] An error has been reported: Error: Cannot find module '/private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/node_modules/@angular-devkit/core/src/index.js'. Please verify that the package.json has a valid "main" entry at tryPackage (internal/modules/cjs/loader.js:303:19) at Function.Module._findPath (internal/modules/cjs/loader.js:516:18) at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27) at Function.Module._load (internal/modules/cjs/loader.js:725:27) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Object.<anonymous> (packages/angular_devkit/core/src/virtual-fs/host/memory_spec.ts:10:1) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) at require (internal/modules/cjs/helpers.js:88:18) at Loader.requireShim [as require_] (node_modules/jasmine/lib/loader.js:35:3) at node_modules/jasmine/lib/loader.js:28:12 at new Promise (<anonymous>) at Loader.load (node_modules/jasmine/lib/loader.js:27:12) at Jasmine.loadSpecs (node_modules/jasmine/lib/jasmine.js:91:23) at Jasmine.execute (node_modules/jasmine/lib/jasmine.js:267:3) at main (node_modules/@bazel/jasmine/jasmine_runner.js:157:3) at node_modules/@bazel/jasmine/jasmine_runner.js:182:26 { code: 'MODULE_NOT_FOUND', path: '/private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/node_modules/@angular-devkit/core/package.json', requestPath: '@angular-devkit/core' } ================================================================================ Target //packages/angular_devkit/core:core_test up-to-date: dist/bin/packages/angular_devkit/core/core_test.sh dist/bin/packages/angular_devkit/core/core_test_loader.js dist/bin/packages/angular_devkit/core/core_test_require_patch.js INFO: Elapsed time: 0.761s, Critical Path: 0.42s INFO: 2 processes: 2 local. INFO: Build completed, 1 test FAILED, 2 total actions //packages/angular_devkit/core:core_test FAILED in 0.4s /private/var/tmp/_bazel_alanagius/5168427e57f204ca069c602aa7ed1931/execroot/angular_cli/bazel-out/darwin-fastbuild/testlogs/packages/angular_devkit/core/core_test/test.log ``` Closes #2688
1 parent c8f2621 commit 3c4ef58

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

packages/jasmine/jasmine_runner.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function readArg() {
6161
return process.argv.splice(2, 1)[0];
6262
}
6363

64-
function main(args) {
64+
async function main(args) {
6565
if (args.length < 2) {
6666
throw new Error('expected argument missing');
6767
}
@@ -133,7 +133,7 @@ function main(args) {
133133
// Patch the inner execute function to do our filtering first.
134134
const env = jasmine.getEnv();
135135
const originalExecute = env.execute.bind(env);
136-
env.execute = () => {
136+
env.execute = async () => {
137137
const allSpecs = getAllSpecs(env);
138138
// Partition the specs among the shards.
139139
// This ensures that the specs are evenly divided over the shards.
@@ -144,7 +144,7 @@ function main(args) {
144144
const end = allSpecs.length * (SHARD_INDEX + 1) / TOTAL_SHARDS;
145145
const enabledSpecs = allSpecs.slice(start, end);
146146
env.configure({specFilter: (s) => enabledSpecs.includes(s.id)});
147-
originalExecute();
147+
await originalExecute();
148148
};
149149
// Special case!
150150
// To allow us to test sharding, always run the specs in the order they are declared
@@ -154,7 +154,8 @@ function main(args) {
154154
}
155155
}
156156

157-
jrunner.execute();
157+
await jrunner.execute();
158+
158159
return 0;
159160
}
160161

@@ -176,5 +177,12 @@ function getAllSpecs(jasmineEnv) {
176177
}
177178

178179
if (require.main === module) {
179-
process.exitCode = main(process.argv.slice(2));
180+
(async () => {
181+
try {
182+
process.exitCode = await main(process.argv.slice(2));
183+
} catch (error) {
184+
console.error('[jasmine_runner.js] An error has been reported:', error);
185+
process.exitCode = 1;
186+
}
187+
})();
180188
}

0 commit comments

Comments
 (0)