Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat(test): add zone-testing typing
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaLiPassion committed May 12, 2018
1 parent f584945 commit 7c4f1aa
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ parsed-idl/
.vscode
npm-debug.log
build-esm/
yarn-error.log
6 changes: 6 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
* found in the LICENSE file at https://angular.io/license
*/
import './jasmine-patch';
import './mocha-bridge/mocha-bridge';
// TODO: @JiaLiPassion, add mocha/jest bridge for jasmine later
// import './mocha-bridge/mocha-bridge';
6 changes: 1 addition & 5 deletions lib/mocha/jasmine-bridge/jasmine.expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ function getMatchers() {
try {
if (typeof actual === 'function') {
actual();
} else {
pass = eq(actual, expected);
}
} catch (error) {
pass = eq(error, expected);
pass = !expected || eq(error, expected);
}
return {pass};
}
Expand All @@ -149,8 +147,6 @@ function getMatchers() {
try {
if (typeof actual === 'function') {
actual();
} else {
pass = actual instanceof Error;
}
} catch (error) {
pass = true;
Expand Down
5 changes: 5 additions & 0 deletions lib/mocha/jasmine-bridge/jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
};
});
21 changes: 13 additions & 8 deletions lib/mocha/jasmine-bridge/jasmine.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -136,7 +140,8 @@ export function eq(a: any, b: any) {
return false;
}
if (a instanceof Error && b instanceof Error) {
return a.message === b.message;
return getErrorMessage(a) === getErrorMessage(b) ||
toMatch(getErrorMessage(a), getErrorMessage(b));
}
let isEqual = true;

Expand All @@ -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;
Expand All @@ -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 += ',';
}
Expand Down
6 changes: 3 additions & 3 deletions lib/mocha/jest-bridge/jest.clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -91,5 +91,5 @@ export function addJestTimer(jest: any, global: any) {
jest.__zone_symbol__last_delegate_spec = null;
proxyZoneSpec.setDelegate(lastDelegate);
zs.unlockDatePatch();
}
};
}
27 changes: 1 addition & 26 deletions lib/mocha/jest-bridge/jest.expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/jest-bridge/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions lib/mocha/mocha-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 7c4f1aa

Please sign in to comment.