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

Commit d95f5b7

Browse files
committed
feat(test): add zone-testing typing
1 parent f584945 commit d95f5b7

18 files changed

+365
-113
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ parsed-idl/
88
.vscode
99
npm-debug.log
1010
build-esm/
11+
yarn-error.log

gulpfile.js

+6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ gulp.task('build/zone.js.d.ts', ['compile-esm'], function() {
106106
.pipe(gulp.dest('./dist'));
107107
});
108108

109+
gulp.task('build/zone-testing.d.ts', ['compile-esm'], function() {
110+
return gulp.src('./build-esm/lib/testing/zone-testing.typing.d.ts')
111+
.pipe(rename('zone-testing.d.ts'))
112+
.pipe(gulp.dest('./dist'));
113+
});
114+
109115
// Zone for Node.js environment.
110116
gulp.task('build/zone-node.js', ['compile-esm-node'], function(cb) {
111117
return generateScript('./lib/node/rollup-main.ts', 'zone-node.js', false, cb);

lib/jasmine/jasmine.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import './jasmine-patch';
9-
import './mocha-bridge/mocha-bridge';
9+
// TODO: @JiaLiPassion, add mocha/jest bridge for jasmine later
10+
// import './mocha-bridge/mocha-bridge';

lib/mocha/jasmine-bridge/jasmine.expect.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ function buildCustomMatchers(jasmine: any, actual: any) {
4848
if (matcher.hasOwnProperty(key)) {
4949
const customMatcher = matcher[key](util, customEqualityTesters);
5050
matchers[key] = function(...expects: any[]) {
51-
const args = expects ? expects : [];
51+
const args = expects ? [...expects] : [];
5252
args.unshift(actual);
5353
const result = customMatcher.compare.apply(null, args);
5454
if (!result.pass) {
55+
console.log('compare ', args);
5556
const message = result.messge || util.buildFailureMessage(key, false, actual, expects);
5657
throw new Error(message);
5758
}
5859
};
5960
matchers['not'][key] = function(...expects: any[]) {
60-
const args = expects ? expects : [];
61+
const args = expects ? [...expects] : [];
6162
args.unshift(actual);
6263
const result = customMatcher.compare.apply(null, args);
6364
if (result.pass) {
@@ -133,10 +134,10 @@ function getMatchers() {
133134
if (typeof actual === 'function') {
134135
actual();
135136
} else {
136-
pass = eq(actual, expected);
137+
pass = (!expected && actual instanceof Error) || eq(actual, expected);
137138
}
138139
} catch (error) {
139-
pass = eq(error, expected);
140+
pass = !expected || eq(error, expected);
140141
}
141142
return {pass};
142143
}

lib/mocha/jasmine-bridge/jasmine.ts

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {mappingBDD} from './jasmine.bdd';
99
import {addJasmineClock} from './jasmine.clock';
1010
import {addJasmineExpect} from './jasmine.expect';
1111
import {addJasmineSpy} from './jasmine.spy';
12+
import { formatObject } from './jasmine.util';
1213

1314
Zone.__load_patch('jasmine2mocha', (global: any) => {
1415
if (typeof global.Mocha === 'undefined') {
@@ -48,4 +49,8 @@ Zone.__load_patch('jasmine2mocha', (global: any) => {
4849
global.Mocha.__zone_symbol__TIMEOUT = newValue;
4950
}
5051
});
52+
53+
jasmine.pp = function (obj: any): string {
54+
return formatObject(obj);
55+
};
5156
});

lib/mocha/jasmine-bridge/jasmine.util.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export function addCustomEqualityTester(jasmine: any) {
9797
};
9898
}
9999

100-
export function eq(a: any, b: any) {
100+
function getErrorMessage(error: any) {
101+
return error.message || error.description;
102+
}
103+
104+
export function eq(a: any, b: any): boolean {
101105
for (let i = 0; i < customEqualityTesters.length; i++) {
102106
const result = customEqualityTesters[i](a, b);
103107
if (result === true || result === false) {
@@ -132,12 +136,13 @@ export function eq(a: any, b: any) {
132136
if (b instanceof ObjectContaining) {
133137
return b.match(a);
134138
}
139+
if (a instanceof Error && b instanceof Error) {
140+
return getErrorMessage(a) === getErrorMessage(b) ||
141+
toMatch(getErrorMessage(a), getErrorMessage(b));
142+
}
135143
if (Object.keys(a).length !== Object.keys(b).length) {
136144
return false;
137145
}
138-
if (a instanceof Error && b instanceof Error) {
139-
return a.message === b.message;
140-
}
141146
let isEqual = true;
142147

143148
for (let prop in a) {
@@ -156,11 +161,11 @@ export function eq(a: any, b: any) {
156161
return b.eq(a);
157162
}
158163

159-
if (a instanceof Error && typeof b === 'string') {
160-
return a.message === b;
164+
if (a instanceof Error) {
165+
return eq(getErrorMessage(a), b) || toMatch(getErrorMessage(a), b);
161166
}
162-
if (b instanceof Error && typeof a === 'string') {
163-
return a === b.message;
167+
if (b instanceof Error) {
168+
return eq(a, getErrorMessage(b)) || toMatch(a, getErrorMessage(b));
164169
}
165170

166171
return false;
@@ -184,10 +189,10 @@ export function buildFailureMessage(
184189
return ' ' + s.toLowerCase();
185190
});
186191

187-
var message = 'Expected ' + formatObject(actual) + (isNot ? ' not ' : ' ') + englishyPredicate;
192+
let message = 'Expected ' + formatObject(actual) + (isNot ? ' not ' : ' ') + englishyPredicate;
188193

189194
if (expected.length > 0) {
190-
for (var i = 0; i < expected.length; i++) {
195+
for (let i = 0; i < expected.length; i++) {
191196
if (i > 0) {
192197
message += ',';
193198
}

lib/mocha/jest-bridge/jest.clock.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ export function addJestTimer(jest: any, global: any) {
7474
if (zs) {
7575
return;
7676
}
77-
const fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec()
77+
const fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
7878
const proxyZoneSpec = ProxyZoneSpec.get();
7979
jest.__zone_symbol__last_delegate_spec = proxyZoneSpec.getDelegate();
8080
proxyZoneSpec.setDelegate(fakeAsyncTestZoneSpec);
8181
fakeAsyncTestZoneSpec.lockDatePatch();
8282
};
8383

84-
jest.useRealTimers = function() {
84+
jest.useRealTimers = function () {
8585
const zs = getFakeAsyncTestZoneSpec();
8686
if (!zs) {
8787
throw new Error('Must use real timers in the same block with useFakeTimers');
@@ -91,5 +91,5 @@ export function addJestTimer(jest: any, global: any) {
9191
jest.__zone_symbol__last_delegate_spec = null;
9292
proxyZoneSpec.setDelegate(lastDelegate);
9393
zs.unlockDatePatch();
94-
}
94+
};
9595
}

lib/mocha/jest-bridge/jest.expect.ts

+1-26
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import {Any, eq, toMatch} from '../jasmine-bridge/jasmine.util';
9-
declare namespace jasmine {
10-
interface Expect {
11-
anything: () => any;
12-
any: (expectedObject: any) => any;
13-
arrayContaining: (expectedArray: string[]) => any;
14-
objectContaining: (expectedObject: any) => any;
15-
stringContaining: (expectedString: string) => any;
16-
stringMatching: (expectedMatcher: RegExp|string) => any;
17-
extend: (extendedMatchers: any) => any;
18-
assertions: (numbers: number) => void;
19-
hasAssertions: () => void;
20-
}
21-
22-
interface Matchers {
23-
toHaveBeenCalledTimes: (expected: number) => boolean;
24-
lastCalledWith: (...params: any[]) => boolean;
25-
toHaveBeenLastCalledWith: (...params: any[]) => boolean;
26-
toBeInstanceOf: (expected: any) => boolean;
27-
toContainEqual: (expected: any) => boolean;
28-
toHaveLength: (expected: number) => boolean;
29-
toHaveProperty: (expected: any, value: any) => boolean;
30-
toMatchObject: (expected: any) => boolean;
31-
}
32-
}
33-
349
export function expandExpect(global: any) {
3510
const jasmine = global.jasmine;
36-
const expect: jasmine.Expect = global.expect;
11+
const expect: any = global.expect;
3712

3813
class Anything {}
3914

lib/mocha/jest-bridge/jest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Zone.__load_patch('jest2mocha', (global: any) => {
1818
}
1919
// TODO: @JiaLiPassion, now we only support jest in Mocha runner
2020
// support jasmine later.
21-
if (global.Mocha['__zone_symbol__isBridge']) {
21+
if (!global.Mocha || global.Mocha['__zone_symbol__isBridge']) {
2222
return;
2323
}
2424
// create a jasmine global object

lib/mocha/mocha-patch.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@
88

99
'use strict';
1010

11-
declare function suite(description: string, suiteFn: () => void): void;
12-
declare function test(description: string, testFn: () => void): void;
13-
declare function specify(description: string, testFn: () => void): void;
14-
declare function setup(fn: () => void): void;
15-
declare function teardown(fn: () => void): void;
16-
declare function suiteSetup(fn: () => void): void;
17-
declare function suiteTeardown(fn: () => void): void;
18-
declare function before(fn: () => void): void;
19-
declare function after(fn: () => void): void;
20-
2111
Zone.__load_patch('Mocha', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2212
const Mocha = global.Mocha;
2313

@@ -148,8 +138,12 @@ Zone.__load_patch('Mocha', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
148138
}
149139

150140
function wrapTestInZone(args: IArguments): any[] {
141+
const timeoutArgs = args.length > 0 ? args[args.length - 1] : null;
151142
const asyncTest = function(fn: Function) {
152143
return function(done: Function) {
144+
if (this && typeof this.timeout === 'function' && typeof timeoutArgs === 'number') {
145+
this.timeout(timeoutArgs);
146+
}
153147
fn = beforeTest(this, fn);
154148
return testZone.run(fn, this, [done]);
155149
};

0 commit comments

Comments
 (0)