refactor: infer type for `it()` assertion functions (#19904)

PR Close #19904
This commit is contained in:
George Kalpakas 2018-07-05 18:10:09 +03:00 committed by Miško Hevery
parent 809e8f742e
commit e01b539ee5
12 changed files with 74 additions and 74 deletions

View File

@ -595,7 +595,7 @@ describe('DocViewerComponent', () => {
describe(`(.${NO_ANIMATIONS}: ${noAnimations})`, () => {
beforeEach(() => docViewerEl.classList[noAnimations ? 'add' : 'remove'](NO_ANIMATIONS));
it('should return an observable', (done: DoneFn) => {
it('should return an observable', done => {
docViewer.swapViews().subscribe(done, done.fail);
});

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {AsyncTestCompleter, describe, expect, iit, inject, it} from '@angular/core/testing/src/testing_internal';
import {AsyncTestCompleter, describe, expect, fit, inject, it} from '@angular/core/testing/src/testing_internal';
import {ChromeDriverExtension, Injector, Options, WebDriverAdapter, WebDriverExtension} from '../../index';
import {TraceEventFactory} from '../trace_event_factory';

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
import {ddescribe, describe, fit, it} from '@angular/core/testing/src/testing_internal';
import {toArray} from 'rxjs/operators';
import {HttpClient} from '../src/client';
@ -23,31 +23,31 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
afterEach(() => { backend.verify(); });
describe('makes a basic request', () => {
it('for JSON data', (done: DoneFn) => {
it('for JSON data', done => {
client.get('/test').subscribe(res => {
expect((res as any)['data']).toEqual('hello world');
done();
});
backend.expectOne('/test').flush({'data': 'hello world'});
});
it('for text data', (done: DoneFn) => {
it('for text data', done => {
client.get('/test', {responseType: 'text'}).subscribe(res => {
expect(res).toEqual('hello world');
done();
});
backend.expectOne('/test').flush('hello world');
});
it('with headers', (done: DoneFn) => {
it('with headers', done => {
client.get('/test', {headers: {'X-Option': 'true'}}).subscribe(() => done());
const req = backend.expectOne('/test');
expect(req.request.headers.get('X-Option')).toEqual('true');
req.flush({});
});
it('with params', (done: DoneFn) => {
it('with params', done => {
client.get('/test', {params: {'test': 'true'}}).subscribe(() => done());
backend.expectOne('/test?test=true').flush({});
});
it('for an arraybuffer', (done: DoneFn) => {
it('for an arraybuffer', done => {
const body = new ArrayBuffer(4);
client.get('/test', {responseType: 'arraybuffer'}).subscribe(res => {
expect(res).toBe(body);
@ -56,7 +56,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
backend.expectOne('/test').flush(body);
});
if (typeof Blob !== 'undefined') {
it('for a blob', (done: DoneFn) => {
it('for a blob', done => {
const body = new Blob([new ArrayBuffer(4)]);
client.get('/test', {responseType: 'blob'}).subscribe(res => {
expect(res).toBe(body);
@ -65,7 +65,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
backend.expectOne('/test').flush(body);
});
}
it('that returns a response', (done: DoneFn) => {
it('that returns a response', done => {
const body = {'data': 'hello world'};
client.get('/test', {observe: 'response'}).subscribe(res => {
expect(res instanceof HttpResponse).toBe(true);
@ -74,7 +74,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
backend.expectOne('/test').flush(body);
});
it('that returns a stream of events', (done: DoneFn) => {
it('that returns a stream of events', done => {
client.get('/test', {observe: 'events'}).pipe(toArray()).toPromise().then(events => {
expect(events.length).toBe(2);
let x = HttpResponse;
@ -85,7 +85,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
backend.expectOne('/test').flush({'data': 'hello world'});
});
it('with progress events enabled', (done: DoneFn) => {
it('with progress events enabled', done => {
client.get('/test', {reportProgress: true}).subscribe(() => done());
const req = backend.expectOne('/test');
expect(req.request.reportProgress).toEqual(true);
@ -93,7 +93,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
});
describe('makes a POST request', () => {
it('with text data', (done: DoneFn) => {
it('with text data', done => {
client.post('/test', 'text body', {observe: 'response', responseType: 'text'})
.subscribe(res => {
expect(res.ok).toBeTruthy();
@ -102,7 +102,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
backend.expectOne('/test').flush('hello world');
});
it('with json data', (done: DoneFn) => {
it('with json data', done => {
const body = {data: 'json body'};
client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => {
expect(res.ok).toBeTruthy();
@ -113,7 +113,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
expect(testReq.request.body).toBe(body);
testReq.flush('hello world');
});
it('with a json body of false', (done: DoneFn) => {
it('with a json body of false', done => {
client.post('/test', false, {observe: 'response', responseType: 'text'}).subscribe(res => {
expect(res.ok).toBeTruthy();
expect(res.status).toBe(200);
@ -123,7 +123,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
expect(testReq.request.body).toBe(false);
testReq.flush('hello world');
});
it('with a json body of 0', (done: DoneFn) => {
it('with a json body of 0', done => {
client.post('/test', 0, {observe: 'response', responseType: 'text'}).subscribe(res => {
expect(res.ok).toBeTruthy();
expect(res.status).toBe(200);
@ -133,7 +133,7 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
expect(testReq.request.body).toBe(0);
testReq.flush('hello world');
});
it('with an arraybuffer', (done: DoneFn) => {
it('with an arraybuffer', done => {
const body = new ArrayBuffer(4);
client.post('/test', body, {observe: 'response', responseType: 'text'}).subscribe(res => {
expect(res.ok).toBeTruthy();
@ -146,14 +146,14 @@ import {HttpClientTestingBackend} from '../testing/src/backend';
});
});
describe('makes a JSONP request', () => {
it('with properly set method and callback', (done: DoneFn) => {
it('with properly set method and callback', done => {
client.jsonp('/test', 'myCallback').subscribe(() => done());
backend.expectOne({method: 'JSONP', url: '/test?myCallback=JSONP_CALLBACK'})
.flush('hello world');
});
});
describe('makes a request for an error response', () => {
it('with a JSON body', (done: DoneFn) => {
it('with a JSON body', done => {
client.get('/test').subscribe(() => {}, (res: HttpErrorResponse) => {
expect(res.error.data).toEqual('hello world');
done();

View File

@ -35,7 +35,7 @@ const SAMPLE_REQ = new HttpRequest<never>('JSONP', '/test');
document = new MockDocument();
backend = new JsonpClientBackend(home, document);
});
it('handles a basic request', (done: DoneFn) => {
it('handles a basic request', done => {
backend.handle(SAMPLE_REQ).pipe(toArray()).subscribe(events => {
expect(events.map(event => event.type)).toEqual([
HttpEventType.Sent,
@ -46,7 +46,7 @@ const SAMPLE_REQ = new HttpRequest<never>('JSONP', '/test');
runOnlyCallback(home, {data: 'This is a test'});
document.mockLoad();
});
it('handles an error response properly', (done: DoneFn) => {
it('handles an error response properly', done => {
const error = new Error('This is a test error');
backend.handle(SAMPLE_REQ).pipe(toArray()).subscribe(undefined, (err: HttpErrorResponse) => {
expect(err.status).toBe(0);
@ -62,7 +62,7 @@ const SAMPLE_REQ = new HttpRequest<never>('JSONP', '/test');
it('when response type is not json',
() => expect(() => backend.handle(SAMPLE_REQ.clone<never>({responseType: 'text'})))
.toThrowError(JSONP_ERR_WRONG_RESPONSE_TYPE));
it('when callback is never called', (done: DoneFn) => {
it('when callback is never called', done => {
backend.handle(SAMPLE_REQ).subscribe(undefined, (err: HttpErrorResponse) => {
expect(err.status).toBe(0);
expect(err.error instanceof Error).toEqual(true);

View File

@ -67,23 +67,22 @@ class ReentrantInterceptor implements HttpInterceptor {
],
});
});
it('initializes HttpClient properly', (done: DoneFn) => {
it('initializes HttpClient properly', done => {
injector.get(HttpClient).get('/test', {responseType: 'text'}).subscribe(value => {
expect(value).toBe('ok!');
done();
});
injector.get(HttpTestingController).expectOne('/test').flush('ok!');
});
it('intercepts outbound responses in the order in which interceptors were bound',
(done: DoneFn) => {
injector.get(HttpClient)
.get('/test', {observe: 'response', responseType: 'text'})
.subscribe(value => done());
const req = injector.get(HttpTestingController).expectOne('/test') as TestRequest;
expect(req.request.headers.get('Intercepted')).toEqual('A,B');
req.flush('ok!');
});
it('intercepts inbound responses in the right (reverse binding) order', (done: DoneFn) => {
it('intercepts outbound responses in the order in which interceptors were bound', done => {
injector.get(HttpClient)
.get('/test', {observe: 'response', responseType: 'text'})
.subscribe(value => done());
const req = injector.get(HttpTestingController).expectOne('/test') as TestRequest;
expect(req.request.headers.get('Intercepted')).toEqual('A,B');
req.flush('ok!');
});
it('intercepts inbound responses in the right (reverse binding) order', done => {
injector.get(HttpClient)
.get('/test', {observe: 'response', responseType: 'text'})
.subscribe(value => {
@ -92,7 +91,7 @@ class ReentrantInterceptor implements HttpInterceptor {
});
injector.get(HttpTestingController).expectOne('/test').flush('ok!');
});
it('allows interceptors to inject HttpClient', (done: DoneFn) => {
it('allows interceptors to inject HttpClient', done => {
TestBed.resetTestingModule();
injector = TestBed.configureTestingModule({
imports: [HttpClientTestingModule],

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
import {ddescribe, describe, fit, it} from '@angular/core/testing/src/testing_internal';
import {Observable} from 'rxjs';
import {toArray} from 'rxjs/operators';
@ -131,7 +131,7 @@ const XSSI_PREFIX = ')]}\'\n';
const res = events[1] as HttpResponse<{data: string}>;
expect(res.body !.data).toBe('some data');
});
it('emits unsuccessful responses via the error path', (done: DoneFn) => {
it('emits unsuccessful responses via the error path', done => {
backend.handle(TEST_POST).subscribe(undefined, (err: HttpErrorResponse) => {
expect(err instanceof HttpErrorResponse).toBe(true);
expect(err.error).toBe('this is the error');
@ -139,7 +139,7 @@ const XSSI_PREFIX = ')]}\'\n';
});
factory.mock.mockFlush(400, 'Bad Request', 'this is the error');
});
it('emits real errors via the error path', (done: DoneFn) => {
it('emits real errors via the error path', done => {
backend.handle(TEST_POST).subscribe(undefined, (err: HttpErrorResponse) => {
expect(err instanceof HttpErrorResponse).toBe(true);
expect(err.error instanceof Error);
@ -148,7 +148,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockErrorEvent(new Error('blah'));
});
describe('progress events', () => {
it('are emitted for download progress', (done: DoneFn) => {
it('are emitted for download progress', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -178,7 +178,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockDownloadProgressEvent(200, 300);
factory.mock.mockFlush(200, 'OK', 'downloaded');
});
it('are emitted for upload progress', (done: DoneFn) => {
it('are emitted for upload progress', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -202,7 +202,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockUploadProgressEvent(200, 300);
factory.mock.mockFlush(200, 'OK', 'Done');
});
it('are emitted when both upload and download progress are available', (done: DoneFn) => {
it('are emitted when both upload and download progress are available', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -219,7 +219,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockDownloadProgressEvent(200, 300);
factory.mock.mockFlush(200, 'OK', 'Done');
});
it('are emitted even if length is not computable', (done: DoneFn) => {
it('are emitted even if length is not computable', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -236,7 +236,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockDownloadProgressEvent(200);
factory.mock.mockFlush(200, 'OK', 'Done');
});
it('include ResponseHeader with headers and status', (done: DoneFn) => {
it('include ResponseHeader with headers and status', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -261,7 +261,7 @@ const XSSI_PREFIX = ')]}\'\n';
sub.unsubscribe();
expect(factory.mock.listeners.progress).toBeUndefined();
});
it('do not cause headers to be re-parsed on main response', (done: DoneFn) => {
it('do not cause headers to be re-parsed on main response', done => {
backend.handle(TEST_POST.clone({reportProgress: true}))
.pipe(toArray())
.subscribe(events => {
@ -284,7 +284,7 @@ const XSSI_PREFIX = ')]}\'\n';
});
});
describe('gets response URL', () => {
it('from XHR.responsesURL', (done: DoneFn) => {
it('from XHR.responsesURL', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
@ -295,7 +295,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.responseURL = '/response/url';
factory.mock.mockFlush(200, 'OK', 'Test');
});
it('from X-Request-URL header if XHR.responseURL is not present', (done: DoneFn) => {
it('from X-Request-URL header if XHR.responseURL is not present', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
@ -306,7 +306,7 @@ const XSSI_PREFIX = ')]}\'\n';
factory.mock.mockResponseHeaders = 'X-Request-URL: /response/url\n';
factory.mock.mockFlush(200, 'OK', 'Test');
});
it('falls back on Request.url if neither are available', (done: DoneFn) => {
it('falls back on Request.url if neither are available', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
@ -318,7 +318,7 @@ const XSSI_PREFIX = ')]}\'\n';
});
});
describe('corrects for quirks', () => {
it('by normalizing 1223 status to 204', (done: DoneFn) => {
it('by normalizing 1223 status to 204', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
@ -328,7 +328,7 @@ const XSSI_PREFIX = ')]}\'\n';
});
factory.mock.mockFlush(1223, 'IE Special Status', 'Test');
});
it('by normalizing 0 status to 200 if a body is present', (done: DoneFn) => {
it('by normalizing 0 status to 200 if a body is present', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(events => {
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
@ -338,7 +338,7 @@ const XSSI_PREFIX = ')]}\'\n';
});
factory.mock.mockFlush(0, 'CORS 0 status', 'Test');
});
it('by leaving 0 status as 0 if a body is not present', (done: DoneFn) => {
it('by leaving 0 status as 0 if a body is not present', done => {
backend.handle(TEST_POST).pipe(toArray()).subscribe(
undefined, (error: HttpErrorResponse) => {
expect(error.status).toBe(0);

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
import {ddescribe, describe, fit, it} from '@angular/core/testing/src/testing_internal';
import {HttpClient} from '../../src/client';
import {HttpClientTestingBackend} from '../src/backend';
@ -24,4 +24,4 @@ describe('HttpClient TestRequest', () => {
expect(resp).toBeNull();
});
});
});

View File

@ -30,7 +30,7 @@ const jsmDescribe = _global.describe;
const jsmDDescribe = _global.fdescribe;
const jsmXDescribe = _global.xdescribe;
const jsmIt = _global.it;
const jsmIIt = _global.fit;
const jsmFIt = _global.fit;
const jsmXIt = _global.xit;
const runnerStack: BeforeEachRunner[] = [];
@ -112,16 +112,17 @@ export function beforeEachProviders(fn: Function): void {
}
function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: number): void {
function _it(
jsmFn: Function, testName: string, testFn: (done?: DoneFn) => any, testTimeout = 0): void {
if (runnerStack.length == 0) {
// This left here intentionally, as we should never get here, and it aids debugging.
debugger;
throw new Error('Empty Stack!');
}
const runner = runnerStack[runnerStack.length - 1];
const timeOut = Math.max(globalTimeOut, testTimeOut);
const timeout = Math.max(globalTimeOut, testTimeout);
jsmFn(name, (done: any) => {
jsmFn(testName, (done: DoneFn) => {
const completerProvider = {
provide: AsyncTestCompleter,
useFactory: () => {
@ -132,11 +133,11 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe
testBed.configureTestingModule({providers: [completerProvider]});
runner.run();
if (testFn.length == 0) {
if (testFn.length === 0) {
const retVal = testFn();
if (isPromise(retVal)) {
// Asynchronous test function that returns a Promise - wait for completion.
(<Promise<any>>retVal).then(done, done.fail);
retVal.then(done, done.fail);
} else {
// Synchronous test function - complete immediately.
done();
@ -145,19 +146,19 @@ function _it(jsmFn: Function, name: string, testFn: Function, testTimeOut: numbe
// Asynchronous test function that takes in 'done' parameter.
testFn(done);
}
}, timeOut);
}, timeout);
}
export function it(name: any, fn: any, timeOut: any = null): void {
return _it(jsmIt, name, fn, timeOut);
export function it(expectation: string, assertion: (done: DoneFn) => any, timeout?: number): void {
return _it(jsmIt, expectation, assertion, timeout);
}
export function xit(name: any, fn: any, timeOut: any = null): void {
return _it(jsmXIt, name, fn, timeOut);
export function fit(expectation: string, assertion: (done: DoneFn) => any, timeout?: number): void {
return _it(jsmFIt, expectation, assertion, timeout);
}
export function iit(name: any, fn: any, timeOut: any = null): void {
return _it(jsmIIt, name, fn, timeOut);
export function xit(expectation: string, assertion: (done: DoneFn) => any, timeout?: number): void {
return _it(jsmXIt, expectation, assertion, timeout);
}
export class SpyObject {

View File

@ -15,7 +15,7 @@ describe('testability example', () => {
describe('using task tracking', () => {
const URL = '/core/testability/ts/whenStable/';
it('times out with a list of tasks', (done: DoneFn) => {
it('times out with a list of tasks', done => {
browser.get(URL);
browser.ignoreSynchronization = true;

View File

@ -12,7 +12,7 @@ import {ApplicationRef, destroyPlatform} from '@angular/core/src/application_ref
import {Console} from '@angular/core/src/console';
import {ComponentRef} from '@angular/core/src/linker/component_factory';
import {Testability, TestabilityRegistry} from '@angular/core/src/testability/testability';
import {AsyncTestCompleter, Log, afterEach, beforeEach, beforeEachProviders, describe, iit, inject, it} from '@angular/core/testing/src/testing_internal';
import {AsyncTestCompleter, Log, afterEach, beforeEach, beforeEachProviders, describe, fit, inject, it} from '@angular/core/testing/src/testing_internal';
import {BrowserModule} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';

View File

@ -11,7 +11,7 @@ import {MockFilesystem} from '../testing/mock';
{
describe('Generator', () => {
it('generates a correct config', (done: DoneFn) => {
it('generates a correct config', done => {
const fs = new MockFilesystem({
'/index.html': 'This is a test',
'/test.txt': 'Another test',
@ -112,7 +112,7 @@ import {MockFilesystem} from '../testing/mock';
.catch(err => done.fail(err));
});
it('uses default `navigationUrls` if not provided', (done: DoneFn) => {
it('uses default `navigationUrls` if not provided', done => {
const fs = new MockFilesystem({
'/index.html': 'This is a test',
});

View File

@ -27,7 +27,7 @@ import {async_fit, async_it} from './async';
});
describe('NgswCommsChannel', () => {
it('can access the registration when it comes before subscription', (done: DoneFn) => {
it('can access the registration when it comes before subscription', done => {
const mock = new MockServiceWorkerContainer();
const comm = new NgswCommChannel(mock as any);
const regPromise = mock.getRegistration() as any as MockServiceWorkerRegistration;
@ -36,7 +36,7 @@ import {async_fit, async_it} from './async';
(comm as any).registration.subscribe((reg: any) => { done(); });
});
it('can access the registration when it comes after subscription', (done: DoneFn) => {
it('can access the registration when it comes after subscription', done => {
const mock = new MockServiceWorkerContainer();
const comm = new NgswCommChannel(mock as any);
const regPromise = mock.getRegistration() as any as MockServiceWorkerRegistration;
@ -385,7 +385,7 @@ import {async_fit, async_it} from './async';
update = new SwUpdate(comm);
mock.setupSw();
});
it('processes update availability notifications when sent', (done: DoneFn) => {
it('processes update availability notifications when sent', done => {
update.available.subscribe(event => {
expect(event.current).toEqual({hash: 'A'});
expect(event.available).toEqual({hash: 'B'});
@ -402,7 +402,7 @@ import {async_fit, async_it} from './async';
},
});
});
it('processes update activation notifications when sent', (done: DoneFn) => {
it('processes update activation notifications when sent', done => {
update.activated.subscribe(event => {
expect(event.previous).toEqual({hash: 'A'});
expect(event.current).toEqual({hash: 'B'});
@ -419,7 +419,7 @@ import {async_fit, async_it} from './async';
},
});
});
it('activates updates when requested', (done: DoneFn) => {
it('activates updates when requested', done => {
mock.messages.subscribe((msg: {action: string, statusNonce: number}) => {
expect(msg.action).toEqual('ACTIVATE_UPDATE');
mock.sendMessage({
@ -430,7 +430,7 @@ import {async_fit, async_it} from './async';
});
return update.activateUpdate().then(() => done()).catch(err => done.fail(err));
});
it('reports activation failure when requested', (done: DoneFn) => {
it('reports activation failure when requested', done => {
mock.messages.subscribe((msg: {action: string, statusNonce: number}) => {
expect(msg.action).toEqual('ACTIVATE_UPDATE');
mock.sendMessage({