From d89ab9a678475a2aa97041ee5c33fb8997efcb74 Mon Sep 17 00:00:00 2001 From: Domas Trijonis Date: Tue, 18 Dec 2018 22:36:13 +0100 Subject: [PATCH 1/2] Improved jasmine SpyObj type Only object methods will be merged with a Spy --- types/jasmine/index.d.ts | 5 +++-- types/jasmine/jasmine-tests.ts | 17 +++++++++++++++-- types/jasmine/v2/index.d.ts | 5 +++-- types/jasmine/v2/jasmine-tests.ts | 17 +++++++++++++++-- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/types/jasmine/index.d.ts b/types/jasmine/index.d.ts index b10db4645e..1a59399c1a 100644 --- a/types/jasmine/index.d.ts +++ b/types/jasmine/index.d.ts @@ -9,6 +9,7 @@ // Chris Yungmann // Giles Roadnight // Yaroslav Admin +// Domas Trijonis // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 // For ddescribe / iit use : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/karma-jasmine/karma-jasmine.d.ts @@ -643,8 +644,8 @@ declare namespace jasmine { withArgs(...args: any[]): Spy; } - type SpyObj = T & { - [k in keyof T]: Spy; + type SpyObj = { + [k in keyof T]: T[k] extends Function ? T[k] & Spy : T[k]; } interface SpyAnd { diff --git a/types/jasmine/jasmine-tests.ts b/types/jasmine/jasmine-tests.ts index 31bdd5f46c..e4208b0b50 100644 --- a/types/jasmine/jasmine-tests.ts +++ b/types/jasmine/jasmine-tests.ts @@ -701,11 +701,20 @@ describe("A spy, when created manually", () => { }); describe("Multiple spies, when created manually", () => { - var tape: any; + interface Tape { + play(): void; + pause(): void; + rewind(pos: number): void; + stop(): void; + readonly isPlaying: boolean; // spy obj makes this writable + } + + var tape: jasmine.SpyObj; var el: jasmine.SpyObj; beforeEach(() => { - tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); + tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); + (tape as { isPlaying: boolean }).isPlaying = false; el = jasmine.createSpyObj('Element', ['hasAttribute']); el.hasAttribute.and.returnValue(false); @@ -733,6 +742,10 @@ describe("Multiple spies, when created manually", () => { it("tracks all the arguments of its calls", () => { expect(tape.rewind).toHaveBeenCalledWith(0); }); + + it("read isPlaying property", () => { + expect(tape.isPlaying).toBe(false); + }) }); describe("jasmine.nothing", () => { diff --git a/types/jasmine/v2/index.d.ts b/types/jasmine/v2/index.d.ts index 4a15b3d1da..4211cf2ca0 100644 --- a/types/jasmine/v2/index.d.ts +++ b/types/jasmine/v2/index.d.ts @@ -8,6 +8,7 @@ // Boris Breuer // Chris Yungmann // Yaroslav Admin +// Domas Trijonis // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 // For ddescribe / iit use : https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/karma-jasmine/karma-jasmine.d.ts @@ -628,8 +629,8 @@ declare namespace jasmine { calls: Calls; } - type SpyObj = T & { - [k in keyof T]: Spy; + type SpyObj = { + [k in keyof T]: T[k] extends Function ? T[k] & Spy : T[k]; } interface SpyAnd { diff --git a/types/jasmine/v2/jasmine-tests.ts b/types/jasmine/v2/jasmine-tests.ts index fce7c59c9e..7ac8339a3e 100644 --- a/types/jasmine/v2/jasmine-tests.ts +++ b/types/jasmine/v2/jasmine-tests.ts @@ -643,11 +643,20 @@ describe("A spy, when created manually", () => { }); describe("Multiple spies, when created manually", () => { - var tape: any; + interface Tape { + play(): void; + pause(): void; + rewind(pos: number): void; + stop(): void; + readonly isPlaying: boolean; // spy obj makes this writable + } + + var tape: jasmine.SpyObj; var el: jasmine.SpyObj; beforeEach(() => { - tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); + tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); + (tape as { isPlaying: boolean }).isPlaying = false; el = jasmine.createSpyObj('Element', ['hasAttribute']); el.hasAttribute.and.returnValue(false); @@ -675,6 +684,10 @@ describe("Multiple spies, when created manually", () => { it("tracks all the arguments of its calls", () => { expect(tape.rewind).toHaveBeenCalledWith(0); }); + + it("read isPlaying property", () => { + expect(tape.isPlaying).toBe(false); + }) }); describe("jasmine.nothing", () => { From 03fae76e16397ea048c506104e0ddadb20d5848c Mon Sep 17 00:00:00 2001 From: Domas Trijonis Date: Tue, 18 Dec 2018 22:45:56 +0100 Subject: [PATCH 2/2] fix: tslint errors --- types/jasmine/jasmine-tests.ts | 6 +++--- types/jasmine/v2/jasmine-tests.ts | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/types/jasmine/jasmine-tests.ts b/types/jasmine/jasmine-tests.ts index e4208b0b50..2f65d84eda 100644 --- a/types/jasmine/jasmine-tests.ts +++ b/types/jasmine/jasmine-tests.ts @@ -959,7 +959,7 @@ describe("Asynchronous specs", () => { }); it("should support async execution of test preparation and expectations", (done: DoneFn) => { - value++; + value += 1; expect(value).toBeGreaterThan(0); done(); }); @@ -1137,7 +1137,7 @@ var myReporter: jasmine.CustomReporter = { specDone: (result: jasmine.CustomReporterResult) => { console.log("Spec: " + result.description + " was " + result.status); //tslint:disable-next-line:prefer-for-of - for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i++) { + for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i += 1) { console.log("Failure: " + result.failedExpectations[i].message); console.log("Actual: " + result.failedExpectations[i].actual); console.log("Expected: " + result.failedExpectations[i].expected); @@ -1149,7 +1149,7 @@ var myReporter: jasmine.CustomReporter = { suiteDone: (result: jasmine.CustomReporterResult) => { console.log('Suite: ' + result.description + ' was ' + result.status); //tslint:disable-next-line:prefer-for-of - for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i++) { + for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i += 1) { console.log('AfterAll ' + result.failedExpectations[i].message); console.log(result.failedExpectations[i].stack); } diff --git a/types/jasmine/v2/jasmine-tests.ts b/types/jasmine/v2/jasmine-tests.ts index 7ac8339a3e..f79c3627eb 100644 --- a/types/jasmine/v2/jasmine-tests.ts +++ b/types/jasmine/v2/jasmine-tests.ts @@ -901,7 +901,7 @@ describe("Asynchronous specs", () => { }); it("should support async execution of test preparation and expectations", (done: DoneFn) => { - value++; + value += 1; expect(value).toBeGreaterThan(0); done(); }); @@ -1079,7 +1079,7 @@ var myReporter: jasmine.CustomReporter = { specDone: (result: jasmine.CustomReporterResult) => { console.log("Spec: " + result.description + " was " + result.status); //tslint:disable-next-line:prefer-for-of - for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i++) { + for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i += 1) { console.log("Failure: " + result.failedExpectations[i].message); console.log("Actual: " + result.failedExpectations[i].actual); console.log("Expected: " + result.failedExpectations[i].expected); @@ -1091,7 +1091,7 @@ var myReporter: jasmine.CustomReporter = { suiteDone: (result: jasmine.CustomReporterResult) => { console.log('Suite: ' + result.description + ' was ' + result.status); //tslint:disable-next-line:prefer-for-of - for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i++) { + for (var i = 0; result.failedExpectations && i < result.failedExpectations.length; i += 1) { console.log('AfterAll ' + result.failedExpectations[i].message); console.log(result.failedExpectations[i].stack); }