Skip to content

Commit

Permalink
refactor(test): updated test helpers to be strongly typed (#3265)
Browse files Browse the repository at this point in the history
this allows tests to pick more on variation in type definitions when tests are being built
updated danger script to change how it finds cold and hot signatures
allow clrf files to be transparently converted to lf
  • Loading branch information
david-driscoll authored and benlesh committed Feb 14, 2018
1 parent c2b00f4 commit 4a86541
Show file tree
Hide file tree
Showing 134 changed files with 697 additions and 1,135 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# no eol conversions!
* -text
1 change: 1 addition & 0 deletions .markdown-doctest-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
}
}
},
emptySubs: marbleTesting.emptySubs,
hot: marbleTesting.hot,
cold: marbleTesting.cold,
expectObservable: marbleTesting.expectObservable,
Expand Down
4 changes: 2 additions & 2 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ var validateMessage = require('validate-commit-msg');

//simple regex matcher to detect usage of helper function and its type signature
var hotMatch = /\bhot\(/gi;
var hotSignatureMatch = /\bdeclare const hot: typeof/gi;
var hotSignatureMatch = /\bimport \{.*?hot.*?\} from '.*?\/helpers\/marble-testing'/gi;

var coldMatch = /\bcold\(/gi;
var coldSignatureMatch = /\bdeclare const cold: typeof/gi;
var coldSignatureMatch = /\bimport \{.*?cold.*?\} from '.*?\/helpers\/marble-testing'/gi;

var errorCount = 0;

Expand Down
3 changes: 1 addition & 2 deletions spec/Notification-spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { expect } from 'chai';
import * as Rx from '../src/Rx';
import marbleTestingSignature = require('./helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expectObservable } from './helpers/marble-testing';

declare const expectObservable: typeof marbleTestingSignature.expectObservable;
const Notification = Rx.Notification;

/** @test {Notification} */
Expand Down
5 changes: 1 addition & 4 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import * as sinon from 'sinon';
import * as Rx from '../src/Rx';
import { Observer } from './../src/internal/Observer';
import { TeardownLogic } from '../src/internal/Subscription';
import marbleTestingSignature = require('./helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { cold, expectObservable, expectSubscriptions } from './helpers/marble-testing';
import { map } from '../src/internal/operators/map';
//tslint:disable-next-line
require('./helpers/test-helper');

declare const asDiagram: any, rxTestScheduler: any;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;

const Observable = Rx.Observable;

Expand Down
6 changes: 1 addition & 5 deletions spec/Subject-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { expect } from 'chai';
import * as Rx from '../src/Rx';
import marbleTestingSignature = require('./helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { time };
declare const hot: typeof marbleTestingSignature.hot;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
import { hot, expectObservable } from './helpers/marble-testing';

const Subject = Rx.Subject;
const Observable = Rx.Observable;
Expand Down
8 changes: 7 additions & 1 deletion spec/helpers/marble-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ declare const global: any;

export const rxTestScheduler: TestScheduler = global.rxTestScheduler;

export function hot(marbles: string, values?: any, error?: any): HotObservable<any> {
export const emptySubs: any[] = [];

export function hot(marbles: string, values?: void, error?: any): HotObservable<string>;
export function hot<V>(marbles: string, values?: { [index: string]: V; }, error?: any): HotObservable<V>;
export function hot<V>(marbles: string, values?: { [index: string]: V; } | void, error?: any): HotObservable<any> {
if (!global.rxTestScheduler) {
throw 'tried to use hot() in async test';
}
return global.rxTestScheduler.createHotObservable.apply(global.rxTestScheduler, arguments);
}

export function cold(marbles: string, values?: void, error?: any): ColdObservable<string>;
export function cold<V>(marbles: string, values?: { [index: string]: V; }, error?: any): ColdObservable<V>;
export function cold(marbles: string, values?: any, error?: any): ColdObservable<any> {
if (!global.rxTestScheduler) {
throw 'tried to use cold() in async test';
Expand Down
5 changes: 1 addition & 4 deletions spec/observables/SubscribeOnObservable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../src/Rx';
import { SubscribeOnObservable } from '../../src/internal/observable/SubscribeOnObservable';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { hot, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare const hot: typeof marbleTestingSignature.hot;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
declare const rxTestScheduler: Rx.TestScheduler;

describe('SubscribeOnObservable', () => {
Expand Down
12 changes: 4 additions & 8 deletions spec/observables/combineLatest-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare const { type };
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
declare const type: Function;

const Observable = Rx.Observable;
const queueScheduler = Rx.Scheduler.queue;
Expand Down Expand Up @@ -516,7 +512,7 @@ describe('Observable.combineLatest', () => {
let a: Promise<number>[];
let o1: Rx.Observable<number[]> = Observable.combineLatest(a);
let o2: Rx.Observable<number[]> = Observable.combineLatest(...a);
let o3: Rx.Observable<number> = Observable.combineLatest(a, (...x) => x.length);
let o3: Rx.Observable<number> = Observable.combineLatest(a, (...x: any[]) => x.length);
/* tslint:enable:no-unused-variable */
});

Expand All @@ -525,7 +521,7 @@ describe('Observable.combineLatest', () => {
let a: Rx.Observable<number>[];
let o1: Rx.Observable<number[]> = Observable.combineLatest(a);
let o2: Rx.Observable<number[]> = Observable.combineLatest(...a);
let o3: Rx.Observable<number> = Observable.combineLatest(a, (...x) => x.length);
let o3: Rx.Observable<number> = Observable.combineLatest(a, (...x: any[]) => x.length);
/* tslint:enable:no-unused-variable */
});

Expand Down
19 changes: 7 additions & 12 deletions spec/observables/concat-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import { lowerCaseO } from '../helpers/test-helper';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
import { hot, cold, emptySubs, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

const Observable = Rx.Observable;
const queueScheduler = Rx.Scheduler.queue;
Expand Down Expand Up @@ -88,7 +83,7 @@ describe('Observable.concat', () => {
const e1 = cold('-');
const e1subs = '^';
const e2 = cold('--|');
const e2subs = [];
const e2subs = emptySubs;
const expected = '-';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand All @@ -112,7 +107,7 @@ describe('Observable.concat', () => {
const e1 = cold('-');
const e1subs = '^';
const e2 = cold('-');
const e2subs = [];
const e2subs = emptySubs;
const expected = '-';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand All @@ -136,7 +131,7 @@ describe('Observable.concat', () => {
const e1 = cold('---#');
const e1subs = '^ !';
const e2 = cold('----|');
const e2subs = [];
const e2subs = emptySubs;
const expected = '---#';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand All @@ -148,7 +143,7 @@ describe('Observable.concat', () => {
const e1 = cold('---#');
const e1subs = '^ !';
const e2 = cold('------#');
const e2subs = [];
const e2subs = emptySubs;
const expected = '---#';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand Down Expand Up @@ -197,7 +192,7 @@ describe('Observable.concat', () => {
const e1 = cold('-');
const e1subs = '^';
const e2 = cold('--a--|');
const e2subs = [];
const e2subs = emptySubs;
const expected = '-';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand Down Expand Up @@ -234,7 +229,7 @@ describe('Observable.concat', () => {
const e1 = cold('--#');
const e1subs = '^ !';
const e2 = cold('----a--|');
const e2subs = [];
const e2subs = emptySubs;
const expected = '--#';

expectObservable(Observable.concat(e1, e2)).toBe(expected);
Expand Down
8 changes: 2 additions & 6 deletions spec/observables/defer-spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { expect } from 'chai';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { defer } from '../../src/';
import { Observable } from '../../src';
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare const asDiagram: any;
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
declare function asDiagram(arg: string): Function;

/** @test {defer} */
describe('defer', () => {
Expand Down
2 changes: 1 addition & 1 deletion spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ describe('Observable.ajax', () => {
});

it('should fail on 404', () => {
let error;
let error: any;
const obj = {
url: '/flibbertyJibbet',
normalizeError: (e: any, xhr: any, type: any) => {
Expand Down
3 changes: 1 addition & 2 deletions spec/observables/empty-spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expectObservable } from '../helpers/marble-testing';
import { empty } from '../../src/';
import { EMPTY } from '../../src';
import { expect } from 'chai';

declare const asDiagram: any;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const rxTestScheduler: any;

/** @test {empty} */
Expand Down
9 changes: 3 additions & 6 deletions spec/observables/forkJoin-spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { expect } from 'chai';
import { Observable, forkJoin, of } from '../../src';
import { lowerCaseO } from '../helpers/test-helper';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { hot, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare const type: any;
declare const asDiagram: any;
declare const hot: typeof marbleTestingSignature.hot;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
declare const type: Function;
declare const asDiagram: Function;

/** @test {forkJoin} */
describe('forkJoin', () => {
Expand Down
5 changes: 2 additions & 3 deletions spec/observables/fromEvent-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expectObservable } from '../helpers/marble-testing';

declare const { asDiagram };
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare function asDiagram(arg: string): Function;
declare const rxTestScheduler: Rx.TestScheduler;

const Observable = Rx.Observable;
Expand Down
6 changes: 2 additions & 4 deletions spec/observables/fromEventPattern-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { expect } from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../src/Rx';
import { noop } from '../../src/internal/util/noop';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const { asDiagram };
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
import { expectObservable } from '../helpers/marble-testing';

declare function asDiagram(arg: string): Function;
declare const rxTestScheduler: Rx.TestScheduler;
const Observable = Rx.Observable;

Expand Down
3 changes: 2 additions & 1 deletion spec/observables/generate-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as Rx from '../../src/Rx';
import '../../src/add/observable/generate';
import { TestScheduler } from '../../src/internal/testing/TestScheduler';
import { expect } from 'chai';
declare const {asDiagram, expectObservable};
import { expectObservable } from '../helpers/marble-testing';
declare function asDiagram(arg: string): Function;
declare const rxTestScheduler: TestScheduler;

const Observable = Rx.Observable;
Expand Down
4 changes: 1 addition & 3 deletions spec/observables/if-spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const expectObservable: typeof marbleTestingSignature.expectObservable;
import { expectObservable } from '../helpers/marble-testing';

const Observable = Rx.Observable;

Expand Down
3 changes: 1 addition & 2 deletions spec/observables/interval-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expectObservable } from '../helpers/marble-testing';
import { asapScheduler, Observable, animationFrameScheduler, queueScheduler } from '../../src';
import { TestScheduler } from '../../src/testing';
import { interval } from '../../src/';

declare const asDiagram: any;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const rxTestScheduler: TestScheduler;

/** @test {interval} */
Expand Down
7 changes: 1 addition & 6 deletions spec/observables/merge-spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import { lowerCaseO } from '../helpers/test-helper';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing';

declare const rxTestScheduler: Rx.TestScheduler;
const Observable = Rx.Observable;
Expand Down
3 changes: 1 addition & 2 deletions spec/observables/never-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { never } from '../../src/';
import { expect } from 'chai';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
import { expectObservable } from '../helpers/marble-testing';

declare const asDiagram: any;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;

/** @test {never} */
describe('never', () => {
Expand Down
7 changes: 4 additions & 3 deletions spec/observables/of-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import { empty } from '../../src/internal/observable/empty';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports
declare const asDiagram: any;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
import { expectObservable } from '../helpers/marble-testing';

declare function asDiagram(arg: string): Function;

declare const rxTestScheduler: Rx.TestScheduler;
const Observable = Rx.Observable;

Expand Down
Loading

0 comments on commit 4a86541

Please sign in to comment.