Skip to content

Commit

Permalink
feat(fakeSchedulers): Add general implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jun 24, 2018
1 parent ed81aa9 commit 54b86de
Show file tree
Hide file tree
Showing 27 changed files with 405 additions and 71 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ See the [examples](./examples) for usage.

### fakeSchedulers

In Jest and when testing Angular components with Jasmine, the test framework can be configured to use its own concept of fake time.
With Jest and Jasmine, the test framework can be configured to use its own concept of fake time. AVA, Mocha and Tape don't have built-in support for fake time, but the functionality can be added via `sinon.useFakeTimers()`.

It's possible to test observables using the test framework's concept of fake time, but the `now` method of the `AsyncScheduler` has to be patched. The `fakeSchedulers` helper can be used to do this.

Expand Down
Empty file added examples/ava/fake-spec.ts
Empty file.
9 changes: 9 additions & 0 deletions examples/jasmine-angular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
To run the tests in the Jasmine example:

1. Install the Jasmine and the other dependencies using:

`npm install`

2. Run the tests using:

`npm run test`
37 changes: 37 additions & 0 deletions examples/jasmine-angular/fake-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// These zone.js imports are only required because this is a stand-alone
// example. When using fakeSchedulers in an Angular project, these zone.js
// imports will be elsewhere.

import "zone.js";
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";

import { tick } from "@angular/core/testing";
import { fakeSchedulers } from "rxjs-marbles/jasmine/angular";
import { of, timer } from "rxjs";
import { delay, map, tap } from "rxjs/operators";

describe("fakeSchedulers", () => {

it("should support a timer", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(0);
}));

it("should support delay", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(1);
}));
});
17 changes: 17 additions & 0 deletions examples/jasmine-angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"devDependencies": {
"@angular/core": "^6.0.4",
"@types/jasmine": "^2.5.53",
"jasmine": "^3.0.0",
"rxjs": "^6.0.0",
"rxjs-marbles": "^4.2.0",
"ts-node": "^6.0.0",
"typescript": "^2.8.3",
"zone.js": "~0.8.26"
},
"name": "rxjs-marbles-examples-jasmine-angular",
"scripts": {
"test": "ts-node node_modules/jasmine/bin/jasmine *-spec.ts"
},
"version": "0.0.0"
}
16 changes: 16 additions & 0 deletions examples/jasmine-angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"declaration": false,
"lib": ["es2015"],
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "build",
"skipLibCheck": true,
"strict": true,
"suppressImplicitAnyIndexErrors": true,
"target": "es2015"
},
"exclude": [],
"include": ["*.ts"]
}
24 changes: 9 additions & 15 deletions examples/jasmine/fake-spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// These zone.js imports are only required because this is a stand-alone
// example. When using fakeSchedulers in an Angular project, these zone.js
// imports will be elsewhere.

import "zone.js";
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";

import { fakeSchedulers } from "rxjs-marbles/jasmine/angular";
import { fakeSchedulers } from "rxjs-marbles/jasmine";
import { of, timer } from "rxjs";
import { delay, map, tap } from "rxjs/operators";

describe("fakeSchedulers", () => {

it("should support a timer", fakeSchedulers(tick => {
beforeEach(() => {
});

it("should support a timer", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
tick(50);
Expand All @@ -25,12 +16,15 @@ describe("fakeSchedulers", () => {
expect(received).toBe(0);
}));

it("should support delay", fakeSchedulers(tick => {
it("should support delay", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(1);
}));

afterEach(() => {
});
});
4 changes: 1 addition & 3 deletions examples/jasmine/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"devDependencies": {
"@angular/core": "^6.0.4",
"@types/jasmine": "^2.5.53",
"jasmine": "^3.0.0",
"rxjs": "^6.0.0",
"rxjs-marbles": "^4.2.0",
"ts-node": "^6.0.0",
"typescript": "^2.8.3",
"zone.js": "~0.8.26"
"typescript": "^2.8.3"
},
"name": "rxjs-marbles-examples-jasmine",
"scripts": {
Expand Down
Empty file added examples/mocha/fake-spec.ts
Empty file.
Empty file added examples/tape/fake-spec.ts
Empty file.
42 changes: 40 additions & 2 deletions fixtures/ava/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/
/*tslint:disable:object-literal-sort-keys*/

import { test } from "ava";
import { map } from "rxjs/operators";
import { cases, marbles } from "../../dist/ava";
import { asapScheduler, of, timer } from "rxjs";
import { delay, map } from "rxjs/operators";
import * as sinon from "sinon";
import { cases, fakeSchedulers, marbles } from "../../dist/ava";

test("it should support marble tests", marbles((m, t) => {

Expand Down Expand Up @@ -54,3 +57,38 @@ cases("should support cases", (m, c, t) => {
e: "-|"
}
});

test("it should support a timer", fakeSchedulers(t => {
t.plan(2);
const clock: sinon.SinonFakeTimers = sinon.useFakeTimers();
let received: number | undefined;
timer(100).subscribe(value => received = value);
clock.tick(50);
t.is(received, undefined);
clock.tick(50);
t.is(received, 0);
clock.restore();
}));

test("it should support delay", fakeSchedulers(t => {
t.plan(2);
const clock: sinon.SinonFakeTimers = sinon.useFakeTimers();
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
clock.tick(50);
t.is(received, undefined);
clock.tick(50);
t.is(received, 1);
clock.restore();
}));

test("it should support the asapScheduler", fakeSchedulers(t => {
t.plan(2);
const clock: sinon.SinonFakeTimers = sinon.useFakeTimers();
let received: number | undefined;
of(1).pipe(delay(0, asapScheduler)).subscribe(value => received = value);
t.is(received, undefined);
clock.tick(0);
t.is(received, 1);
clock.restore();
}));
47 changes: 47 additions & 0 deletions fixtures/jasmine-angular/passing-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/
/*tslint:disable:object-literal-sort-keys*/

import "zone.js";
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";

import { tick } from "@angular/core/testing";
import { asapScheduler, of, timer } from "rxjs";
import { delay, map, tap } from "rxjs/operators";
import { fakeSchedulers } from "../../dist/jasmine/angular";

describe("fakeSchedulers", () => {

it("should support a timer", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(0);
}));

it("should support delay", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(1);
}));

it("should support the asapScheduler", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(0, asapScheduler)).subscribe(value => received = value);
expect(received).not.toBeDefined();
tick(0);
expect(received).toBe(1);
}));
});
6 changes: 6 additions & 0 deletions fixtures/jasmine-angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"types": ["jasmine", "node"]
},
"extends": "../tsconfig.json"
}
44 changes: 17 additions & 27 deletions fixtures/jasmine/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@
*/
/*tslint:disable:object-literal-sort-keys*/

import "zone.js";
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy.js";
import "zone.js/dist/sync-test";
import "zone.js/dist/jasmine-patch";
import "zone.js/dist/async-test";
import "zone.js/dist/fake-async-test";

import { tick } from "@angular/core/testing";
import { asapScheduler, of, timer } from "rxjs";
import { delay, map, tap } from "rxjs/operators";
import { cases, DoneFunction, marbles, observe } from "../../dist/jasmine";
import { fakeSchedulers } from "../../dist/jasmine/angular";
import { cases, DoneFunction, fakeSchedulers, marbles, observe } from "../../dist/jasmine";

interface TestContext {
myVariable: number;
Expand Down Expand Up @@ -120,38 +110,38 @@ describe("observe", () => {

describe("fakeSchedulers", () => {

it("should support a timer", fakeSchedulers(tick => {
beforeEach(() => {
jasmine.clock().install();
jasmine.clock().mockDate(new Date(0));
});

it("should support a timer", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
tick(50);
jasmine.clock().tick(50);
expect(received).not.toBeDefined();
tick(50);
jasmine.clock().tick(50);
expect(received).toBe(0);
}));

it("should support delay", fakeSchedulers(tick => {
it("should support delay", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
tick(50);
jasmine.clock().tick(50);
expect(received).not.toBeDefined();
tick(50);
jasmine.clock().tick(50);
expect(received).toBe(1);
}));

it("should support the asapScheduler", fakeSchedulers(tick => {
it("should support the asapScheduler", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(0, asapScheduler)).subscribe(value => received = value);
expect(received).not.toBeDefined();
tick(0);
jasmine.clock().tick(0);
expect(received).toBe(1);
}));

it("should support the Angular tick function", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
tick(50);
expect(received).not.toBeDefined();
tick(50);
expect(received).toBe(0);
}));
afterEach(() => {
jasmine.clock().uninstall();
});
});
46 changes: 43 additions & 3 deletions fixtures/mocha/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/
/*tslint:disable:object-literal-sort-keys*/
/*tslint:disable:no-unused-expression object-literal-sort-keys*/

import { expect } from "chai";
import { of } from "rxjs";
import { asapScheduler, of, timer } from "rxjs";
import { delay, map, tap } from "rxjs/operators";
import { configure, DoneFunction, observe } from "../../dist/mocha";
import * as sinon from "sinon";
import { configure, DoneFunction, fakeSchedulers, observe } from "../../dist/mocha";

describe("marbles", () => {

Expand Down Expand Up @@ -534,3 +535,42 @@ describe("observe", () => {
tap(value => expect(value).to.equal("pass"))
)));
});

describe("fakeSchedulers", () => {

let clock: sinon.SinonFakeTimers;

beforeEach(() => {
clock = sinon.useFakeTimers();
});

it("should support a timer", fakeSchedulers(() => {
let received: number | undefined;
timer(100).subscribe(value => received = value);
clock.tick(50);
expect(received).to.be.undefined;
clock.tick(50);
expect(received).to.equal(0);
}));

it("should support delay", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(100)).subscribe(value => received = value);
clock.tick(50);
expect(received).to.be.undefined;
clock.tick(50);
expect(received).to.equal(1);
}));

it("should support the asapScheduler", fakeSchedulers(() => {
let received: number | undefined;
of(1).pipe(delay(0, asapScheduler)).subscribe(value => received = value);
expect(received).to.be.undefined;
clock.tick(0);
expect(received).to.equal(1);
}));

afterEach(() => {
clock.restore();
});
});

0 comments on commit 54b86de

Please sign in to comment.