From d95f5b76b7d1be9bb065e9b11aebd3d77c5c2bf3 Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Sat, 12 May 2018 00:50:22 +0900 Subject: [PATCH] feat(test): add zone-testing typing --- .gitignore | 1 + gulpfile.js | 6 + lib/jasmine/jasmine.ts | 3 +- lib/mocha/jasmine-bridge/jasmine.expect.ts | 9 +- lib/mocha/jasmine-bridge/jasmine.ts | 5 + lib/mocha/jasmine-bridge/jasmine.util.ts | 25 +- lib/mocha/jest-bridge/jest.clock.ts | 6 +- lib/mocha/jest-bridge/jest.expect.ts | 27 +- lib/mocha/jest-bridge/jest.ts | 2 +- lib/mocha/mocha-patch.ts | 14 +- lib/testing/zone-testing.typing.ts | 271 +++++++++++++++++++++ package.json | 1 - test/spec/browser/WebSocket.spec.ts | 1 - test/spec/browser/browser.spec.ts | 1 - test/spec/jasmine/jasmine-patch.spec.ts | 2 +- test/spec/mocha/jasmine-bridge.spec.ts | 33 ++- test/spec/mocha/jest-bridge.spec.ts | 67 ++--- yarn.lock | 4 - 18 files changed, 365 insertions(+), 113 deletions(-) create mode 100644 lib/testing/zone-testing.typing.ts diff --git a/.gitignore b/.gitignore index 6a642d111..1c0011dee 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ parsed-idl/ .vscode npm-debug.log build-esm/ +yarn-error.log diff --git a/gulpfile.js b/gulpfile.js index ee0956804..ccd9459a3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -106,6 +106,12 @@ gulp.task('build/zone.js.d.ts', ['compile-esm'], function() { .pipe(gulp.dest('./dist')); }); +gulp.task('build/zone-testing.d.ts', ['compile-esm'], function() { + return gulp.src('./build-esm/lib/testing/zone-testing.typing.d.ts') + .pipe(rename('zone-testing.d.ts')) + .pipe(gulp.dest('./dist')); +}); + // Zone for Node.js environment. gulp.task('build/zone-node.js', ['compile-esm-node'], function(cb) { return generateScript('./lib/node/rollup-main.ts', 'zone-node.js', false, cb); diff --git a/lib/jasmine/jasmine.ts b/lib/jasmine/jasmine.ts index 65d515de2..197905f52 100644 --- a/lib/jasmine/jasmine.ts +++ b/lib/jasmine/jasmine.ts @@ -6,4 +6,5 @@ * found in the LICENSE file at https://angular.io/license */ import './jasmine-patch'; -import './mocha-bridge/mocha-bridge'; \ No newline at end of file +// TODO: @JiaLiPassion, add mocha/jest bridge for jasmine later +// import './mocha-bridge/mocha-bridge'; \ No newline at end of file diff --git a/lib/mocha/jasmine-bridge/jasmine.expect.ts b/lib/mocha/jasmine-bridge/jasmine.expect.ts index c876bf37a..a24178f76 100644 --- a/lib/mocha/jasmine-bridge/jasmine.expect.ts +++ b/lib/mocha/jasmine-bridge/jasmine.expect.ts @@ -48,16 +48,17 @@ function buildCustomMatchers(jasmine: any, actual: any) { if (matcher.hasOwnProperty(key)) { const customMatcher = matcher[key](util, customEqualityTesters); matchers[key] = function(...expects: any[]) { - const args = expects ? expects : []; + const args = expects ? [...expects] : []; args.unshift(actual); const result = customMatcher.compare.apply(null, args); if (!result.pass) { + console.log('compare ', args); const message = result.messge || util.buildFailureMessage(key, false, actual, expects); throw new Error(message); } }; matchers['not'][key] = function(...expects: any[]) { - const args = expects ? expects : []; + const args = expects ? [...expects] : []; args.unshift(actual); const result = customMatcher.compare.apply(null, args); if (result.pass) { @@ -133,10 +134,10 @@ function getMatchers() { if (typeof actual === 'function') { actual(); } else { - pass = eq(actual, expected); + pass = (!expected && actual instanceof Error) || eq(actual, expected); } } catch (error) { - pass = eq(error, expected); + pass = !expected || eq(error, expected); } return {pass}; } diff --git a/lib/mocha/jasmine-bridge/jasmine.ts b/lib/mocha/jasmine-bridge/jasmine.ts index 65b35d5d7..7f3348cc5 100644 --- a/lib/mocha/jasmine-bridge/jasmine.ts +++ b/lib/mocha/jasmine-bridge/jasmine.ts @@ -9,6 +9,7 @@ import {mappingBDD} from './jasmine.bdd'; import {addJasmineClock} from './jasmine.clock'; import {addJasmineExpect} from './jasmine.expect'; import {addJasmineSpy} from './jasmine.spy'; +import { formatObject } from './jasmine.util'; Zone.__load_patch('jasmine2mocha', (global: any) => { if (typeof global.Mocha === 'undefined') { @@ -48,4 +49,8 @@ Zone.__load_patch('jasmine2mocha', (global: any) => { global.Mocha.__zone_symbol__TIMEOUT = newValue; } }); + + jasmine.pp = function (obj: any): string { + return formatObject(obj); + }; }); \ No newline at end of file diff --git a/lib/mocha/jasmine-bridge/jasmine.util.ts b/lib/mocha/jasmine-bridge/jasmine.util.ts index 25475340b..81a04d52c 100644 --- a/lib/mocha/jasmine-bridge/jasmine.util.ts +++ b/lib/mocha/jasmine-bridge/jasmine.util.ts @@ -97,7 +97,11 @@ export function addCustomEqualityTester(jasmine: any) { }; } -export function eq(a: any, b: any) { +function getErrorMessage(error: any) { + return error.message || error.description; +} + +export function eq(a: any, b: any): boolean { for (let i = 0; i < customEqualityTesters.length; i++) { const result = customEqualityTesters[i](a, b); if (result === true || result === false) { @@ -132,12 +136,13 @@ export function eq(a: any, b: any) { if (b instanceof ObjectContaining) { return b.match(a); } + if (a instanceof Error && b instanceof Error) { + return getErrorMessage(a) === getErrorMessage(b) || + toMatch(getErrorMessage(a), getErrorMessage(b)); + } if (Object.keys(a).length !== Object.keys(b).length) { return false; } - if (a instanceof Error && b instanceof Error) { - return a.message === b.message; - } let isEqual = true; for (let prop in a) { @@ -156,11 +161,11 @@ export function eq(a: any, b: any) { return b.eq(a); } - if (a instanceof Error && typeof b === 'string') { - return a.message === b; + if (a instanceof Error) { + return eq(getErrorMessage(a), b) || toMatch(getErrorMessage(a), b); } - if (b instanceof Error && typeof a === 'string') { - return a === b.message; + if (b instanceof Error) { + return eq(a, getErrorMessage(b)) || toMatch(a, getErrorMessage(b)); } return false; @@ -184,10 +189,10 @@ export function buildFailureMessage( return ' ' + s.toLowerCase(); }); - var message = 'Expected ' + formatObject(actual) + (isNot ? ' not ' : ' ') + englishyPredicate; + let message = 'Expected ' + formatObject(actual) + (isNot ? ' not ' : ' ') + englishyPredicate; if (expected.length > 0) { - for (var i = 0; i < expected.length; i++) { + for (let i = 0; i < expected.length; i++) { if (i > 0) { message += ','; } diff --git a/lib/mocha/jest-bridge/jest.clock.ts b/lib/mocha/jest-bridge/jest.clock.ts index 89953a3c2..0ac34733b 100644 --- a/lib/mocha/jest-bridge/jest.clock.ts +++ b/lib/mocha/jest-bridge/jest.clock.ts @@ -74,14 +74,14 @@ export function addJestTimer(jest: any, global: any) { if (zs) { return; } - const fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec() + const fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec(); const proxyZoneSpec = ProxyZoneSpec.get(); jest.__zone_symbol__last_delegate_spec = proxyZoneSpec.getDelegate(); proxyZoneSpec.setDelegate(fakeAsyncTestZoneSpec); fakeAsyncTestZoneSpec.lockDatePatch(); }; - jest.useRealTimers = function() { + jest.useRealTimers = function () { const zs = getFakeAsyncTestZoneSpec(); if (!zs) { throw new Error('Must use real timers in the same block with useFakeTimers'); @@ -91,5 +91,5 @@ export function addJestTimer(jest: any, global: any) { jest.__zone_symbol__last_delegate_spec = null; proxyZoneSpec.setDelegate(lastDelegate); zs.unlockDatePatch(); - } + }; } \ No newline at end of file diff --git a/lib/mocha/jest-bridge/jest.expect.ts b/lib/mocha/jest-bridge/jest.expect.ts index fbfec67bb..e34c13198 100644 --- a/lib/mocha/jest-bridge/jest.expect.ts +++ b/lib/mocha/jest-bridge/jest.expect.ts @@ -6,34 +6,9 @@ * found in the LICENSE file at https://angular.io/license */ import {Any, eq, toMatch} from '../jasmine-bridge/jasmine.util'; -declare namespace jasmine { - interface Expect { - anything: () => any; - any: (expectedObject: any) => any; - arrayContaining: (expectedArray: string[]) => any; - objectContaining: (expectedObject: any) => any; - stringContaining: (expectedString: string) => any; - stringMatching: (expectedMatcher: RegExp|string) => any; - extend: (extendedMatchers: any) => any; - assertions: (numbers: number) => void; - hasAssertions: () => void; - } - - interface Matchers { - toHaveBeenCalledTimes: (expected: number) => boolean; - lastCalledWith: (...params: any[]) => boolean; - toHaveBeenLastCalledWith: (...params: any[]) => boolean; - toBeInstanceOf: (expected: any) => boolean; - toContainEqual: (expected: any) => boolean; - toHaveLength: (expected: number) => boolean; - toHaveProperty: (expected: any, value: any) => boolean; - toMatchObject: (expected: any) => boolean; - } -} - export function expandExpect(global: any) { const jasmine = global.jasmine; - const expect: jasmine.Expect = global.expect; + const expect: any = global.expect; class Anything {} diff --git a/lib/mocha/jest-bridge/jest.ts b/lib/mocha/jest-bridge/jest.ts index b65e0a1e3..10ec36781 100644 --- a/lib/mocha/jest-bridge/jest.ts +++ b/lib/mocha/jest-bridge/jest.ts @@ -18,7 +18,7 @@ Zone.__load_patch('jest2mocha', (global: any) => { } // TODO: @JiaLiPassion, now we only support jest in Mocha runner // support jasmine later. - if (global.Mocha['__zone_symbol__isBridge']) { + if (!global.Mocha || global.Mocha['__zone_symbol__isBridge']) { return; } // create a jasmine global object diff --git a/lib/mocha/mocha-patch.ts b/lib/mocha/mocha-patch.ts index bc52a0b84..0d70f9697 100644 --- a/lib/mocha/mocha-patch.ts +++ b/lib/mocha/mocha-patch.ts @@ -8,16 +8,6 @@ 'use strict'; -declare function suite(description: string, suiteFn: () => void): void; -declare function test(description: string, testFn: () => void): void; -declare function specify(description: string, testFn: () => void): void; -declare function setup(fn: () => void): void; -declare function teardown(fn: () => void): void; -declare function suiteSetup(fn: () => void): void; -declare function suiteTeardown(fn: () => void): void; -declare function before(fn: () => void): void; -declare function after(fn: () => void): void; - Zone.__load_patch('Mocha', (global: any, Zone: ZoneType, api: _ZonePrivate) => { const Mocha = global.Mocha; @@ -148,8 +138,12 @@ Zone.__load_patch('Mocha', (global: any, Zone: ZoneType, api: _ZonePrivate) => { } function wrapTestInZone(args: IArguments): any[] { + const timeoutArgs = args.length > 0 ? args[args.length - 1] : null; const asyncTest = function(fn: Function) { return function(done: Function) { + if (this && typeof this.timeout === 'function' && typeof timeoutArgs === 'number') { + this.timeout(timeoutArgs); + } fn = beforeTest(this, fn); return testZone.run(fn, this, [done]); }; diff --git a/lib/testing/zone-testing.typing.ts b/lib/testing/zone-testing.typing.ts new file mode 100644 index 000000000..670622357 --- /dev/null +++ b/lib/testing/zone-testing.typing.ts @@ -0,0 +1,271 @@ +/** + * @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 + */ +/** + * Zone testing type definition, mix jasmine/mocha/jest + */ +declare const describe: ZoneTest.Describe; +declare const xdescribe: ZoneTest.Describe; +declare const fdescribe: ZoneTest.Describe; +// alias for `describe` +declare const context: ZoneTest.Describe; +// alias for `describe` +declare const suite: ZoneTest.Describe; +declare const it: ZoneTest.Test; +declare const xit: ZoneTest.Test; +// alias for `it` +declare const test: ZoneTest.Test; +declare const specify: ZoneTest.Test; + +declare const before: ZoneTest.BeforeAndAfter; +declare const beforeAll: ZoneTest.BeforeAndAfter; +declare const setup: ZoneTest.BeforeAndAfter; + +declare const after: ZoneTest.BeforeAndAfter; +declare const afterAll: ZoneTest.BeforeAndAfter; +declare const teardown: ZoneTest.BeforeAndAfter; + +declare const beforeEach: ZoneTest.BeforeAndAfter; +declare const suiteSetup: ZoneTest.BeforeAndAfter; + +declare const afterEach: ZoneTest.BeforeAndAfter; +declare const suiteTeardown: ZoneTest.BeforeAndAfter; + +declare const expect: ZoneTest.Expect; +declare function fail(error?: any): void; + +declare namespace ZoneTest { + interface DoneCallback extends Function { + (...args: any[]): void; + fail(error?: string | { message: string }): any; + } + + interface TestSpecContext { + skip(): this; + timeout(ms: number | string): this; + [index: string]: any; + } + + interface Describe { + (description: string, spec: (this: DescribeSpecContext) => void): void; + only(description: string, spec: (this: DescribeSpecContext) => void): void; + skip(description: string, spec: (this: DescribeSpecContext) => void): void; + } + + interface Test { + (description: string, callback?: (this: TestSpecContext, done?: DoneCallback) => PromiseLike | void, timeout?: number): void; + only(description: string, callback?: (this: TestSpecContext, done?: DoneCallback) => PromiseLike | void): void; + skip(description: string, callback?: (this: TestSpecContext, done?: DoneCallback) => PromiseLike | void): void; + } + + interface DescribeSpecContext { + timeout(ms: number | string): this; + } + + interface BeforeAndAfter { + (callback?: (this: TestSpecContext, done?: DoneCallback) => PromiseLike | void, timeout?: number): void; + (description: string, callback?: (this: TestSpecContext, done?: DoneCallback) => PromiseLike | void, timeout?: number): void; + } + + type CustomEqualityTester = (first: any, second: any) => boolean | void; + + interface CustomMatcher { + compare(actual: T, expected: T, ...args: any[]): CustomMatcherResult; + compare(actual: any, ...expected: any[]): CustomMatcherResult; + } + + interface MatchersUtil { + equals(a: any, b: any, customTesters?: CustomEqualityTester[]): boolean; + contains(haystack: ArrayLike | string, needle: any, customTesters?: CustomEqualityTester[]): boolean; + buildFailureMessage(matcherName: string, isNot: boolean, actual: any, ...expected: any[]): string; + } + + type CustomMatcherFactory = (util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]) => CustomMatcher; + + interface CustomMatcherFactories { + [index: string]: CustomMatcherFactory; + } + + interface CustomMatcherResult { + pass: boolean; + message?: string; + } + + interface Expect { + (actual: any): Matchers; + anything(): any; + any(classType: any): any; + arrayContaining(arr: any[]): any; + assertions(num: number): void; + hasAssertions(): void; + extend(obj: any): void; + objectContaining(obj: any): any; + stringMatching(str: string | RegExp): any; + stringContaining(str: string): any; + } + + interface Matchers { + not: Matchers; + resolves: Matchers>; + rejects: Matchers>; + lastCalledWith(...args: any[]): R; + toBe(expected: any): R; + toBeCalled(): R; + toBeCalledWith(...args: any[]): R; + toBeCloseTo(expected: number, numDigits?: number): R; + toBeDefined(): R; + toBeFalsy(): R; + toBeGreaterThan(expected: number): R; + toBeGreaterThanOrEqual(expected: number): R; + toBeInstanceOf(expected: any): R; + toBeLessThan(expected: number): R; + toBeLessThanOrEqual(expected: number): R; + toBeNull(): R; + toBeTruthy(): R; + toBeUndefined(): R; + toBeNaN(): R; + toContain(expected: any): R; + toContainEqual(expected: any): R; + toEqual(expected: any): R; + toHaveBeenCalled(): R; + toHaveBeenCalledTimes(expected: number): R; + toHaveBeenCalledWith(...params: any[]): R; + toHaveBeenLastCalledWith(...params: any[]): R; + toHaveLength(expected: number): R; + toHaveProperty(propertyPath: string | any[], value?: any): R; + toMatch(expected: string | RegExp): R; + toMatchObject(expected: {} | any[]): R; + toThrow(error?: string | RegExp | Error): R; + toThrowError(message?: string | RegExp): boolean; + toThrowError(expected?: new (...args: any[]) => Error, message?: string | RegExp): boolean; + } +} + +interface DoneFn extends Function { + (): void; + fail: (message?: Error | string) => void; +} +declare namespace jasmine { + type SpyObjMethodNames = string[] | {[methodName: string]: any}; + let clock: () => Clock; + function any(aclass: any): any; + function anything(): any; + function arrayContaining(sample: ArrayLike): any; + function arrayWithExactContents(sample: ArrayLike): any; + function objectContaining(sample: Partial): any; + function createSpy(name?: string, originalFn?: Function): Spy; + function createSpyObj(baseName: string, methodNames: SpyObjMethodNames): any; + function createSpyObj(baseName: string, methodNames: SpyObjMethodNames): SpyObj; + function createSpyObj(methodNames: SpyObjMethodNames): any; + function createSpyObj(methodNames: SpyObjMethodNames): SpyObj; + function addCustomEqualityTester(equalityTester: ZoneTest.CustomEqualityTester): void; + function addMatchers(matchers: ZoneTest.CustomMatcherFactories): void; + function pp(value: any): string; + + function getEnv(): any; + let DEFAULT_TIMEOUT_INTERVAL: number; + + interface Clock { + install(): void; + uninstall(): void; + tick(ms: number): void; + mockDate(date?: Date): void; + } + + interface Spy { + (...params: any[]): any; + + identity: string; + and: SpyAnd; + calls: Calls; + mostRecentCall: { args: any[]; }; + argsForCall: any[]; + } + + type SpyObj = T & { + [k in keyof T]: Spy; + }; + + interface SpyAnd { + callThrough(): Spy; + returnValue(val: any): Spy; + returnValues(...values: any[]): Spy; + callFake(fn: Function): Spy; + throwError(msg: string): Spy; + stub(): Spy; + } + + interface Calls { + any(): boolean; + count(): number; + argsFor(index: number): any[]; + allArgs(): any[]; + all(): CallInfo[]; + mostRecent(): CallInfo; + first(): CallInfo; + reset(): void; + } + + interface CallInfo { + object: any; + args: any[]; + returnValue: any; + } +} + +declare function spyOn(object: T, method: keyof T): jasmine.Spy; +declare function spyOnProperty(object: T, property: keyof T, accessType?: 'get' | 'set'): jasmine.Spy; + +declare namespace jest { + function addMatchers(matchers: ZoneTest.CustomMatcherFactories): typeof jest; + function clearAllMocks(): typeof jest; + function resetAllMocks(): typeof jest; + function restoreAllMocks(): typeof jest; + function clearAllTimers(): typeof jest; + function fn(implementation: (...args: any[]) => T): Mock; + function fn(implementation?: (...args: any[]) => any): Mock; + function isMockFunction(fn: any): fn is Mock; + function runAllImmediates(): typeof jest; + function runAllTicks(): typeof jest; + function runAllTimers(): typeof jest; + function runOnlyPendingTimers(): typeof jest; + function runTimersToTime(msToRun: number): typeof jest; + function advanceTimersByTime(msToRun: number): typeof jest; + function setTimeout(timeout: number): typeof jest; + function spyOn(object: T, method: M, accessType?: 'get' | 'set'): jasmine.Spy; + function useFakeTimers(): typeof jest; + function useRealTimers(): typeof jest; + + interface MockInstance { + getMockName(): string; + mock: MockContext; + mockClear(): void; + mockReset(): void; + mockImplementation(fn: (...args: any[]) => any): Mock; + mockImplementationOnce(fn: (...args: any[]) => any): Mock; + mockName(name: string): Mock; + mockReturnThis(): Mock; + mockReturnValue(value: any): Mock; + mockReturnValueOnce(value: any): Mock; + mockResolvedValue(value: any): Mock; + mockResolvedValueOnce(value: any): Mock; + mockRejectedValue(value: any): Mock; + mockRejectedValueOnce(value: any): Mock; + } + + interface MockContext { + calls: any[][]; + instances: T[]; + } + + interface Mock extends Function, MockInstance { + new (...args: any[]): T; + (...args: any[]): any; + } +} + +declare function pending(reason?: string): void; diff --git a/package.json b/package.json index 0511c2cd6..0493dd433 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,6 @@ }, "dependencies": {}, "devDependencies": { - "@types/jasmine": "2.2.33", "@types/node": "^6.0.96", "@types/systemjs": "^0.19.30", "assert": "^1.4.1", diff --git a/test/spec/browser/WebSocket.spec.ts b/test/spec/browser/WebSocket.spec.ts index 32aeb12d4..3f4aa1f19 100644 --- a/test/spec/browser/WebSocket.spec.ts +++ b/test/spec/browser/WebSocket.spec.ts @@ -19,7 +19,6 @@ if (!window['saucelabs']) { const TEST_SERVER_URL = 'ws://localhost:8001'; const testZone = Zone.current.fork({name: 'test'}); - beforeEach(function(done) { socket = new WebSocket(TEST_SERVER_URL); socket.addEventListener('open', function() { diff --git a/test/spec/browser/browser.spec.ts b/test/spec/browser/browser.spec.ts index 72f462f69..37db17f40 100644 --- a/test/spec/browser/browser.spec.ts +++ b/test/spec/browser/browser.spec.ts @@ -122,7 +122,6 @@ describe('Zone', function() { return parentZoneDelegate.scheduleTask(targetZone, task); } }); - beforeEach(function() { mouseEvent.initEvent('mousedown', true, true); hookSpy = jasmine.createSpy('hook'); diff --git a/test/spec/jasmine/jasmine-patch.spec.ts b/test/spec/jasmine/jasmine-patch.spec.ts index 3df23f3fc..9f5e83cb0 100644 --- a/test/spec/jasmine/jasmine-patch.spec.ts +++ b/test/spec/jasmine/jasmine-patch.spec.ts @@ -45,7 +45,7 @@ ifEnvSupports(supportJasmineSpec, () => { expect(beforeAllCalledCount).toBe(1); }); - beforeEach(() => beforeEachZone = Zone.current); + beforeEach(() => { beforeEachZone = Zone.current; }); it('should throw on async in describe', () => { expect(throwOnAsync).toBe(true); diff --git a/test/spec/mocha/jasmine-bridge.spec.ts b/test/spec/mocha/jasmine-bridge.spec.ts index c7cb56808..d93902f26 100644 --- a/test/spec/mocha/jasmine-bridge.spec.ts +++ b/test/spec/mocha/jasmine-bridge.spec.ts @@ -1251,9 +1251,30 @@ describe('Custom matcher: \'toBeGoofy\'', function() { }); describe('failed', () => { - try { - fail('error'); - } catch (error) { - expect(error).toEqual('error'); - } -}); \ No newline at end of file + it('should catch failed', () => { + try { + fail('error'); + } catch (error) { + expect(error).toEqual('error'); + } + }); +}); + +describe('timeout', () => { + beforeEach(() => { + setTimeout(() => { + expect(true).toBe(true); + }, 2500); + }, 3000); + + it('beforeEach timeout', () => { + expect(true).toBe(true); + }); + + it('test timeout', (done) => { + setTimeout(() => { + expect(true).toBe(true); + done(); + }, 3100); + }, 3500); +}); diff --git a/test/spec/mocha/jest-bridge.spec.ts b/test/spec/mocha/jest-bridge.spec.ts index ef1356366..79c7cd351 100644 --- a/test/spec/mocha/jest-bridge.spec.ts +++ b/test/spec/mocha/jest-bridge.spec.ts @@ -5,9 +5,6 @@ * 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 */ -declare let jest: any; -declare function test(description: string, testFn: () => void): void; - describe('extend', () => { (expect as any).extend({ toBeDivisibleBy(received: any, argument: any) { @@ -54,16 +51,12 @@ describe('expect', () => { describe('Beware of a misunderstanding! A sequence of dice rolls', () => { const expected = [1, 2, 3, 4, 5, 6]; it('matches even with an unexpected number 7', () => { - expect([4, 1, 6, 7, 3, 5, 2, 5, 4, 6]) - .toEqual( - (expect as any).arrayContaining(expected), - ); + expect([4, 1, 6, 7, 3, 5, 2, 5, 4, 6]).toEqual((expect as any).arrayContaining(expected), ); }); it('does not match without an expected number 2', () => { - expect([4, 1, 6, 7, 3, 5, 7, 5, 4, 6]) - .not.toEqual( - (expect as any).arrayContaining(expected), - ); + expect([ + 4, 1, 6, 7, 3, 5, 7, 5, 4, 6 + ]).not.toEqual((expect as any).arrayContaining(expected), ); }); }); @@ -92,13 +85,10 @@ describe('expect', () => { describe('objectContaining', () => { test('onPress should object containing with the right thing', () => { const onPress = {x: 100, y: 200, z: 300}; - (expect(onPress) as any) - .toEqual( - (expect as any).objectContaining({ - x: (expect as any).any(Number), - y: (expect as any).any(Number), - }), - ); + (expect(onPress) as any).toEqual((expect as any).objectContaining({ + x: (expect as any).any(Number), + y: (expect as any).any(Number), + }), ); }); }); @@ -114,16 +104,10 @@ describe('expect', () => { (expect as any).stringMatching(/^[BR]ob/), ]; it('matches even if received contains additional elements', () => { - expect(['Alicia', 'Roberto', 'Evelina']) - .toEqual( - (expect as any).arrayContaining(expected), - ); + expect(['Alicia', 'Roberto', 'Evelina']).toEqual((expect as any).arrayContaining(expected), ); }); it('does not match if received does not contain expected elements', () => { - expect(['Roberto', 'Evelina']) - .not.toEqual( - (expect as any).arrayContaining(expected), - ); + expect(['Roberto', 'Evelina']).not.toEqual((expect as any).arrayContaining(expected), ); }); }); @@ -133,21 +117,18 @@ describe('expect', () => { return (expect(Promise.resolve('lemon')) as any).resolves.toBe('lemon'); }); - test('resolves to lemon with await', async () => { - await (expect(Promise.resolve('lemon')) as any).resolves.toBe('lemon'); - await (expect(Promise.resolve('lemon')) as any).resolves.not.toBe('octopus'); + test('resolves to lemon with await', async() => { + await(expect(Promise.resolve('lemon')) as any).resolves.toBe('lemon'); + await(expect(Promise.resolve('lemon')) as any).resolves.not.toBe('octopus'); }); test('rejects to octopus', () => { // make sure to add a return statement - return (expect(Promise.reject(new Error('octopus'))) as any) - .rejects.toThrow( - 'octopus', - ); + return (expect(Promise.reject(new Error('octopus'))) as any).rejects.toThrow('octopus'); }); - test('rejects to octopus', async () => { - return await (expect(Promise.reject(new Error('octopus'))) as any).rejects.toThrow('octopus'); + test('rejects to octopus', async() => { + return await(expect(Promise.reject(new Error('octopus'))) as any).rejects.toThrow('octopus'); }); }); @@ -243,11 +224,9 @@ describe('expect', () => { // Deep referencing using an array containing the keyPath (expect(houseForSale) as any).toHaveProperty(['kitchen', 'area'], 20); - (expect(houseForSale) as any) - .toHaveProperty( - ['kitchen', 'amenities'], - ['oven', 'stove', 'washer'], - ); + (expect(houseForSale) as any).toHaveProperty(['kitchen', 'amenities'], [ + 'oven', 'stove', 'washer' + ], ); (expect(houseForSale) as any).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); (expect(houseForSale).not as any).toHaveProperty(['kitchen', 'open']); @@ -339,14 +318,14 @@ describe('expect', () => { expect(logs).toEqual(['first call', 'second call', 'default', 'default']); }); - test('mockResolvedValue', async () => { + test('mockResolvedValue', async() => { const asyncMock = jest.fn().mockResolvedValue(43); const result = await asyncMock(); // 43 expect(result).toBe(43); }); - test('mockResolvedValueOnce', async () => { + test('mockResolvedValueOnce', async() => { const asyncMock = jest.fn() .mockResolvedValue('default') .mockResolvedValueOnce('first call') @@ -360,7 +339,7 @@ describe('expect', () => { expect(logs).toEqual(['first call', 'second call', 'default', 'default']); }); - test('mockRejectedValue', async () => { + test('mockRejectedValue', async() => { const asyncMock = jest.fn().mockRejectedValue(new Error('Async error')); try { @@ -370,7 +349,7 @@ describe('expect', () => { } }); - test('mockRejectedValueOnce', async () => { + test('mockRejectedValueOnce', async() => { const asyncMock = jest.fn() .mockResolvedValueOnce('first call') .mockRejectedValueOnce(new Error('Async error')); diff --git a/yarn.lock b/yarn.lock index a26216695..dea39c55e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,10 +2,6 @@ # yarn lockfile v1 -"@types/jasmine@2.2.33": - version "2.2.33" - resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-2.2.33.tgz#4715cfd2ca7fbd632fc7f1784f13e637bed028c5" - "@types/node@^6.0.96": version "6.0.101" resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.101.tgz#0c5911cfb434af4a51c0a499931fe6423207d921"