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

Commit 7c4f1aa

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

17 files changed

+328
-106
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,9 @@ function getMatchers() {
132132
try {
133133
if (typeof actual === 'function') {
134134
actual();
135-
} else {
136-
pass = eq(actual, expected);
137135
}
138136
} catch (error) {
139-
pass = eq(error, expected);
137+
pass = !expected || eq(error, expected);
140138
}
141139
return {pass};
142140
}
@@ -149,8 +147,6 @@ function getMatchers() {
149147
try {
150148
if (typeof actual === 'function') {
151149
actual();
152-
} else {
153-
pass = actual instanceof Error;
154150
}
155151
} catch (error) {
156152
pass = true;

lib/mocha/jasmine-bridge/jasmine.ts

Lines changed: 5 additions & 0 deletions
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

Lines changed: 13 additions & 8 deletions
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) {
@@ -136,7 +140,8 @@ export function eq(a: any, b: any) {
136140
return false;
137141
}
138142
if (a instanceof Error && b instanceof Error) {
139-
return a.message === b.message;
143+
return getErrorMessage(a) === getErrorMessage(b) ||
144+
toMatch(getErrorMessage(a), getErrorMessage(b));
140145
}
141146
let isEqual = true;
142147

@@ -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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 26 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 10 deletions
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

0 commit comments

Comments
 (0)