Merge pull request #14875 from lukas-zech-software/master

jasmine: Make Matchers generic
This commit is contained in:
Mine Starks
2017-03-15 11:37:01 -07:00
committed by GitHub
11 changed files with 345 additions and 309 deletions

View File

@@ -13,7 +13,7 @@ declare namespace JasminePromiseMatchers {
declare namespace jasmine {
interface Matchers {
interface Matchers<T> {
/**
* Verifies that a Promise is (or has been) rejected.
*/

View File

@@ -7,7 +7,7 @@
/// <reference types="jasmine" />
declare namespace jasmine {
interface Matchers {
interface Matchers<T> {
// toBe
toBeArray(): boolean;
toBeArrayOfBooleans(): boolean;

View File

@@ -81,7 +81,7 @@ declare namespace jasmine {
proxyCallTo_(methodName: string, passedArguments: any): any;
}
interface Matchers {
interface Matchers<T> {
/**
* Check if DOM element has class.
*
@@ -232,7 +232,7 @@ declare namespace jasmine {
*
*/
toHaveData(key : string, expectedValue : string): boolean;
toBe(selector: JQuery): boolean;
toBe(selector: T): boolean;
/**
* Check if DOM element is matched by the given selector.
@@ -241,7 +241,7 @@ declare namespace jasmine {
* // returns true
* expect($('<div><span class="some-class"></span></div>')).toContain('some-class')
*/
toContain(selector: JQuery): boolean;
toContain(selector: any): boolean;
/**
* Check if DOM element exists inside the given parent element.

View File

@@ -1,14 +1,15 @@
describe("Jasmine jQuery extension", () => {
it("Adds jQuery matchers", () => {
expect($('<div id="some-id"></div>')).toBe('div');
expect($('<div id="some-id"></div>')).toBe('div#some-id');
expect($('<div id="some-id"></div>')).toBe($('div'));
expect($('<div id="some-id"></div>')).toBe($('div#some-id'));
expect($('<input type="checkbox" checked="checked"/>')).toBeChecked();
expect($('<div id="some-id"></div>')).toBeHidden();
expect($('<div style="display: none; margin: 10px;"></div>')).toHaveCss({ display: "none", margin: "10px" });
expect($('<div style="display: none; margin: 10px;"></div>')).toHaveCss({ margin: "10px" });
expect($('<option selected="selected"></option>')).toBeSelected();
expect($('<div id="some-id"></div>')).toBeVisible();
expect($('<div><span class="some-class"></span></div>')).toContain('span.some-class');
// NOTE: It is now necessary to explicitly add the generic parameter when using `toContain`
expect<JQuery>($('<div><span class="some-class"></span></div>')).toContain('span.some-class');
expect($('<span></span>').addClass('js-something')).toBeMatchedBy('.js-something');
expect($('<span></span>')).toExist();
expect($('<div id="some-id"></div>')).toHaveAttr('id', 'some-id');

View File

@@ -23,7 +23,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
/// <reference types="jasmine" />
declare namespace jasmine {
interface Matchers {
interface Matchers<T> {
//toBe
toBeArray(): boolean;

View File

@@ -10,7 +10,7 @@ declare function installPromiseMatchers(): void;
declare namespace jasmine {
interface Matchers {
interface Matchers <T>{
/**
* Verifies that a Promise is (or has been) rejected.
*/

89
jasmine/index.d.ts vendored
View File

@@ -24,8 +24,9 @@ declare function afterEach(action: (done: DoneFn) => void, timeout?: number): vo
declare function beforeAll(action: (done: DoneFn) => void, timeout?: number): void;
declare function afterAll(action: (done: DoneFn) => void, timeout?: number): void;
declare function expect(spy: Function): jasmine.Matchers;
declare function expect(actual: any): jasmine.Matchers;
declare function expect(spy: Function): jasmine.Matchers<any>;
declare function expect<T>(actual: ArrayLike<T>): jasmine.ArrayLikeMatchers<T>;
declare function expect<T>(actual: T): jasmine.Matchers<T>;
declare function fail(e?: any): void;
/** Action method that should be called when the async work is complete */
@@ -45,23 +46,33 @@ declare function waitsFor(latchMethod: () => boolean, failureMessage?: string, t
declare function waits(timeout?: number): void;
declare namespace jasmine {
type Expected<T> = T | ObjectContaining<T> | Any | Spy;
var clock: () => Clock;
function any(aclass: any): Any;
function anything(): Any;
function arrayContaining(sample: any[]): ArrayContaining;
function objectContaining(sample: any): ObjectContaining;
function objectContaining<T>(sample: Partial<T>): ObjectContaining<T>;
function createSpy(name: string, originalFn?: Function): Spy;
function createSpyObj(baseName: string, methodNames: any[]): any;
function createSpyObj<T>(baseName: string, methodNames: any[]): T;
function pp(value: any): string;
function getEnv(): Env;
function addCustomEqualityTester(equalityTester: CustomEqualityTester): void;
function addMatchers(matchers: CustomMatcherFactories): void;
function stringMatching(str: string): Any;
function stringMatching(str: RegExp): Any;
function formatErrorMsg(domain: string, usage: string) : (msg: string) => string
function formatErrorMsg(domain: string, usage: string): (msg: string) => string;
interface Any {
@@ -84,8 +95,8 @@ declare namespace jasmine {
jasmineToString(): string;
}
interface ObjectContaining {
new (sample: any): any;
interface ObjectContaining<T> {
new (sample: Partial<T>): Partial<T>;
jasmineMatches(other: any, mismatchKeys: any[], mismatchValues: any[]): boolean;
jasmineToString(): string;
@@ -115,18 +126,14 @@ declare namespace jasmine {
withMock(func: () => void): void;
}
interface CustomEqualityTester {
(first: any, second: any): boolean;
}
type CustomEqualityTester = (first: any, second: any) => boolean;
interface CustomMatcher {
compare<T>(actual: T, expected: T): CustomMatcherResult;
compare(actual: any, expected: any): CustomMatcherResult;
}
interface CustomMatcherFactory {
(util: MatchersUtil, customEqualityTesters: Array<CustomEqualityTester>): CustomMatcher;
}
type CustomMatcherFactory = (util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]) => CustomMatcher;
interface CustomMatcherFactories {
[index: string]: CustomMatcherFactory;
@@ -138,9 +145,9 @@ declare namespace jasmine {
}
interface MatchersUtil {
equals(a: any, b: any, customTesters?: Array<CustomEqualityTester>): boolean;
contains<T>(haystack: ArrayLike<T> | string, needle: any, customTesters?: Array<CustomEqualityTester>): boolean;
buildFailureMessage(matcherName: string, isNot: boolean, actual: any, ...expected: Array<any>): string;
equals(a: any, b: any, customTesters?: CustomEqualityTester[]): boolean;
contains<T>(haystack: ArrayLike<T> | string, needle: any, customTesters?: CustomEqualityTester[]): boolean;
buildFailureMessage(matcherName: string, isNot: boolean, actual: any, ...expected: any[]): string;
}
interface Env {
@@ -152,7 +159,7 @@ declare namespace jasmine {
currentSpec: Spec;
matchersClass: Matchers;
matchersClass: Matchers<any>;
version(): any;
versionString(): string;
@@ -226,12 +233,12 @@ declare namespace jasmine {
passed(): boolean;
}
interface MessageResult extends Result {
interface MessageResult extends Result {
values: any;
trace: Trace;
}
interface ExpectationResult extends Result {
interface ExpectationResult extends Result {
matcherName: string;
passed(): boolean;
expected: any;
@@ -244,12 +251,13 @@ declare namespace jasmine {
new (options: {random: boolean, seed: string}): any;
random: boolean;
seed: string;
sort<T>(items: T[]) : T[];
sort<T>(items: T[]): T[];
}
namespace errors {
class ExpectationFailed extends Error {
constructor();
stack: any;
}
}
@@ -257,7 +265,7 @@ declare namespace jasmine {
interface TreeProcessor {
new (attrs: any): any;
execute: (done: Function) => void;
processTree() : any;
processTree(): any;
}
interface Trace {
@@ -303,18 +311,18 @@ declare namespace jasmine {
results(): NestedResults;
}
interface Matchers {
interface Matchers<T> {
new (env: Env, actual: any, spec: Env, isNot?: boolean): any;
new (env: Env, actual: T, spec: Env, isNot?: boolean): any;
env: Env;
actual: any;
actual: T;
spec: Env;
isNot?: boolean;
message(): any;
toBe(expected: any, expectationFailOutput?: any): boolean;
toEqual(expected: any, expectationFailOutput?: any): boolean;
toBe(expected: Expected<T>, expectationFailOutput?: any): boolean;
toEqual(expected: Expected<T>, expectationFailOutput?: any): boolean;
toMatch(expected: string | RegExp, expectationFailOutput?: any): boolean;
toBeDefined(expectationFailOutput?: any): boolean;
toBeUndefined(expectationFailOutput?: any): boolean;
@@ -334,11 +342,18 @@ declare namespace jasmine {
toThrow(expected?: any): boolean;
toThrowError(message?: string | RegExp): boolean;
toThrowError(expected?: new (...args: any[]) => Error, message?: string | RegExp): boolean;
not: Matchers;
not: Matchers<T>;
Any: Any;
}
interface ArrayLikeMatchers<T> extends Matchers<ArrayLike<T>> {
toBe(expected: Expected<ArrayLike<T>>, expectationFailOutput?: any): boolean;
toEqual(expected: Expected<ArrayLike<T>>, expectationFailOutput?: any): boolean;
toContain(expected: T, expectationFailOutput?: any): boolean;
not: ArrayLikeMatchers<T>;
}
interface Reporter {
reportRunnerStarting(runner: Runner): void;
reportRunnerResults(runner: Runner): void;
@@ -373,18 +388,18 @@ declare namespace jasmine {
}
interface CustomReporterResult {
description: string,
failedExpectations?: FailedExpectation[],
fullName: string,
description: string;
failedExpectations?: FailedExpectation[];
fullName: string;
id: string;
passedExpectations?: PassedExpectation[],
passedExpectations?: PassedExpectation[];
pendingReason?: string;
status?: string;
}
interface RunDetails {
failedExpectations: ExpectationResult[];
order: jasmine.Order
order: jasmine.Order;
}
interface CustomReporter {
@@ -414,9 +429,7 @@ declare namespace jasmine {
results(): NestedResults;
}
interface SpecFunction {
(spec?: Spec): void;
}
type SpecFunction = (spec?: Spec) => void;
interface SuiteOrSpec {
id: number;
@@ -435,7 +448,7 @@ declare namespace jasmine {
spies_: Spy[];
results_: NestedResults;
matchersClass: Matchers;
matchersClass: Matchers<any>;
getFullName(): string;
results(): NestedResults;
@@ -448,7 +461,7 @@ declare namespace jasmine {
waits(timeout: number): Spec;
waitsFor(latchFunction: SpecFunction, timeoutMessage?: string, timeout?: number): Spec;
fail(e?: any): void;
getMatchersClass_(): Matchers;
getMatchersClass_(): Matchers<any>;
addMatchers(matchersPrototype: CustomMatcherFactories): void;
finishCallback(): void;
finish(onComplete?: () => void): void;
@@ -497,7 +510,7 @@ declare namespace jasmine {
identity: string;
and: SpyAnd;
calls: Calls;
mostRecentCall: { args: any[]; };
mostRecentCall: {args: any[]; };
argsForCall: any[];
}
@@ -558,7 +571,7 @@ declare namespace jasmine {
finished: boolean;
result: any;
messages: any;
runDetails: RunDetails
runDetails: RunDetails;
new (): any;

File diff suppressed because it is too large Load Diff

16
jasmine/v1/index.d.ts vendored
View File

@@ -16,9 +16,9 @@ declare function xit(expectation: string, assertion: () => void): void;
declare function beforeEach(action: () => void): void;
declare function afterEach(action: () => void): void;
declare function expect(spy: Function): jasmine.Matchers;
//declare function expect(spy: jasmine.Spy): jasmine.Matchers;
declare function expect(actual: any): jasmine.Matchers;
declare function expect<T>(spy: Function): jasmine.Matchers<T>;
//declare function expect(spy: jasmine.Spy): jasmine.Matchers<T>;
declare function expect<T>(actual: any): jasmine.Matchers<T>;
declare function spyOn(object: any, method: string): jasmine.Spy;
@@ -91,7 +91,7 @@ declare namespace jasmine {
currentSpec: Spec;
matchersClass: Matchers;
matchersClass: Matchers<any>;
version(): any;
versionString(): string;
@@ -204,7 +204,7 @@ declare namespace jasmine {
results(): NestedResults;
}
interface Matchers {
interface Matchers<T> {
new (env: Env, actual: any, spec: Env, isNot?: boolean): any;
@@ -232,7 +232,7 @@ declare namespace jasmine {
toContainHtml(expected: string): boolean;
toContainText(expected: string): boolean;
toThrow(expected?: any): boolean;
not: Matchers;
not: Matchers<T>;
Any: Any;
}
@@ -287,7 +287,7 @@ declare namespace jasmine {
spies_: Spy[];
results_: NestedResults;
matchersClass: Matchers;
matchersClass: Matchers<any>;
getFullName(): string;
results(): NestedResults;
@@ -299,7 +299,7 @@ declare namespace jasmine {
waits(timeout: number): Spec;
waitsFor(latchFunction: SpecFunction, timeoutMessage?: string, timeout?: number): Spec;
fail(e?: any): void;
getMatchersClass_(): Matchers;
getMatchersClass_(): Matchers<any>;
addMatchers(matchersPrototype: any): void;
finishCallback(): void;
finish(onComplete?: () => void): void;

View File

@@ -60,7 +60,7 @@ describe("Included matchers:", () => {
foo: 'foo'
};
expect(a.foo).toBeDefined();
expect((<any>a).bar).not.toBeDefined();
expect((a as any).bar).not.toBeDefined();
});
it("The `toBeUndefined` matcher compares against `undefined`", () => {
@@ -68,7 +68,7 @@ describe("Included matchers:", () => {
foo: 'foo'
};
expect(a.foo).not.toBeUndefined();
expect((<any>a).bar).toBeUndefined();
expect((a as any).bar).toBeUndefined();
});
it("The 'toBeNull' matcher compares against null", () => {
@@ -202,7 +202,7 @@ describe("A spy", () => {
var foo: any, bar: any = null;
beforeEach(() => {
foo = {
setBar: function (value: any) {
setBar: (value: any) => {
bar = value;
}
};
@@ -235,7 +235,7 @@ describe("A spy, when configured to call through", () => {
var foo: any, bar: any, fetchedBar: any;
beforeEach(() => {
foo = {
setBar: function (value: any) {
setBar: (value: any) => {
bar = value;
},
getBar: () => {
@@ -261,7 +261,7 @@ describe("A spy, when faking a return value", () => {
var foo: any, bar: any, fetchedBar: any;
beforeEach(() => {
foo = {
setBar: function (value: any) {
setBar: (value: any) => {
bar = value;
},
getBar: () => {
@@ -287,7 +287,7 @@ describe("A spy, when faking a return value", () => {
var foo: any, bar: any, fetchedBar: any;
beforeEach(() => {
foo = {
setBar: function (value: any) {
setBar: (value: any) => {
bar = value;
},
getBar: () => {
@@ -319,7 +319,7 @@ describe("A spy, when created manually", () => {
whatAmI("I", "am", "a", "spy");
});
it("is named, which helps in error reporting", () => {
expect(whatAmI.identity).toEqual('whatAmI')
expect(whatAmI.identity).toEqual('whatAmI');
});
it("tracks that the spy was called", () => {
expect(whatAmI).toHaveBeenCalled();
@@ -369,7 +369,7 @@ describe("jasmine.any", () => {
it("is useful for comparing arguments", () => {
var foo = jasmine.createSpy('foo');
foo(12, () => {
return true
return true;
});
expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));
});
@@ -430,7 +430,7 @@ describe("Asynchronous specs", () => {
jasmineEnv.updateInterval = 250;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
jasmineEnv.specFilter = (spec) => {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = (arg: any) => window.onload(arg);
@@ -439,11 +439,11 @@ describe("Asynchronous specs", () => {
currentWindowOnload(null);
}
(<HTMLElement>document.querySelector('.version')).innerHTML = jasmineEnv.versionString();
(document.querySelector('.version') as HTMLElement).innerHTML = jasmineEnv.versionString();
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();
})();

17
jasminewd2/index.d.ts vendored
View File

@@ -17,10 +17,10 @@ declare function afterAll(action: () => Promise<void>, timeout?: number): void;
declare namespace jasmine {
// The global `Promise` type is too strict and kinda wrong
interface Promise<T> {
then<U>(onFulfill?: (value: T) => U | Promise<U>, onReject?: (error: any) => U | Promise<U>): Promise<U>;
then<U>(onFulfill?: (value: T) => U | Promise<U>, onReject?: (error: any) => U | Promise<U>): Promise<U>;
}
interface Matchers {
interface Matchers<T> {
toBe(expected: any, expectationFailOutput?: any): Promise<void>;
toEqual(expected: any, expectationFailOutput?: any): Promise<void>;
toMatch(expected: string | RegExp | Promise<string | RegExp>, expectationFailOutput?: any): Promise<void>;
@@ -44,6 +44,13 @@ declare namespace jasmine {
toThrowError(expected?: new (...args: any[]) => Error | Promise<new (...args: any[]) => Error>, message?: string | RegExp | Promise<string | RegExp>): Promise<void>;
}
interface ArrayLikeMatchers<T> extends Matchers<ArrayLike<T>> {
toBe(expected: Expected<ArrayLike<T>>, expectationFailOutput?: any): Promise<void>;
toEqual(expected: Expected<ArrayLike<T>>, expectationFailOutput?: any): Promise<void>;
toContain(expected: T, expectationFailOutput?: any): Promise<void>;
not: ArrayLikeMatchers<T>;
}
function addMatchers(matchers: AsyncCustomMatcherFactories): void;
interface Env {
@@ -59,12 +66,12 @@ declare namespace jasmine {
}
interface AsyncCustomMatcherFactory {
(util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]): AsyncCustomMatcher;
(util: MatchersUtil, customEqualityTesters: CustomEqualityTester[]): AsyncCustomMatcher;
}
interface AsyncCustomMatcher {
compare<T>(actual: T, expected: T): AsyncCustomMatcherResult;
compare(actual: any, expected: any): AsyncCustomMatcherResult;
compare<T>(actual: T, expected: T): AsyncCustomMatcherResult;
compare(actual: any, expected: any): AsyncCustomMatcherResult;
}
interface AsyncCustomMatcherResult {