From 71100e6d72848a2d7ad2bb9adb9349eca6e8fa76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mis=CC=8Cko=20Hevery?= Date: Fri, 22 Jun 2018 18:26:28 -0700 Subject: [PATCH] feat(core): add support for using async/await with Jasmine (#24637) Example: ``` it('...', await(async() => { doSomething(); await asyncFn(); doSomethingAfter(); })); ``` PR Close #24637 --- .../test/testability/jasmine_await_spec.ts | 31 +++++++++++++++++ packages/core/testing/src/jasmine_await.ts | 34 +++++++++++++++++++ packages/core/testing/src/testing.ts | 1 + tools/public_api_guard/core/testing.d.ts | 5 +++ 4 files changed, 71 insertions(+) create mode 100644 packages/core/test/testability/jasmine_await_spec.ts create mode 100644 packages/core/testing/src/jasmine_await.ts diff --git a/packages/core/test/testability/jasmine_await_spec.ts b/packages/core/test/testability/jasmine_await_spec.ts new file mode 100644 index 0000000000..e4da8fb785 --- /dev/null +++ b/packages/core/test/testability/jasmine_await_spec.ts @@ -0,0 +1,31 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {jasmineAwait} from '../../testing'; + + +describe('await', () => { + let pass: boolean; + beforeEach(() => pass = false); + afterEach(() => expect(pass).toBe(true)); + + it('should convert passes', jasmineAwait(async() => { pass = await Promise.resolve(true); })); + + it('should convert failures', (done) => { + const error = new Error(); + const fakeDone: DoneFn = function() { fail('passed, but should have failed'); } as any; + fakeDone.fail = function(value: any) { + expect(value).toBe(error); + done(); + }; + jasmineAwait(async() => { + pass = await Promise.resolve(true); + throw error; + })(fakeDone); + }); +}); \ No newline at end of file diff --git a/packages/core/testing/src/jasmine_await.ts b/packages/core/testing/src/jasmine_await.ts new file mode 100644 index 0000000000..a863e62149 --- /dev/null +++ b/packages/core/testing/src/jasmine_await.ts @@ -0,0 +1,34 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +/** + * Converts an `async` function, with `await`, into a function which is compatible with Jasmine test + * framework. + * + * For asynchronous function blocks, Jasmine expects `it` (and friends) to take a function which + * takes a `done` callback. (Jasmine does not understand functions which return `Promise`.) The + * `jasmineAwait()` wrapper converts the test function returning `Promise` into a function which + * Jasmine understands. + * + * + * Example: + * ``` + * it('...', jasmineAwait(async() => { + * doSomething(); + * await asyncFn(); + * doSomethingAfter(); + * })); + * ``` + * + */ +export function jasmineAwait(fn: () => Promise): + (done: {(): void; fail: (message?: Error | string) => void;}) => void { + return function(done: {(): void; fail: (message?: Error | string) => void;}) { + fn().then(done, done.fail); + }; +} diff --git a/packages/core/testing/src/testing.ts b/packages/core/testing/src/testing.ts index d2d9476c80..c1ea067aa3 100644 --- a/packages/core/testing/src/testing.ts +++ b/packages/core/testing/src/testing.ts @@ -13,6 +13,7 @@ */ export * from './async'; +export * from './jasmine_await'; export * from './component_fixture'; export * from './fake_async'; export * from './test_bed'; diff --git a/tools/public_api_guard/core/testing.d.ts b/tools/public_api_guard/core/testing.d.ts index c7df004ea6..5759b364f6 100644 --- a/tools/public_api_guard/core/testing.d.ts +++ b/tools/public_api_guard/core/testing.d.ts @@ -53,6 +53,11 @@ export declare class InjectSetupWrapper { inject(tokens: any[], fn: Function): () => any; } +export declare function jasmineAwait(fn: () => Promise): (done: { + (): void; + fail: (message?: Error | string) => void; +}) => void; + /** @experimental */ export declare type MetadataOverride = { add?: Partial;