Skip to content

Commit

Permalink
feat(tape): Include Tape boilerplate.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jul 22, 2017
1 parent d5441ee commit 95580f3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 100 deletions.
56 changes: 4 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ The `configure` method can be used to specify the assertion functions that are t

The default implementations simply perform the assertion and throw an error for failed assertions.

If using Tape and its `plan` method, it's necessary to configure appropriate functions so that Tape's assertion count is updated.

<a name="marbles"></a>

### marbles
Expand Down Expand Up @@ -128,59 +126,13 @@ it("should map the values", marbles((m) => {

As with Jasmine and Mocha, instead of passing your test function directly to Tape, pass it to the library's `marbles` function. The `marbles` function will concatenate the additional `Test` argument it receives from Tape.

The `marbles` function is generic and the `Test` type can be specified so that its type information is used, like this:
There is a `/tape` directory in the package that includes wrapper that will correctly type additional argument and will call `configure` - passing Tape's assertion methods to ensure marble assertions will be counted towards Tape's `plan` - so be sure to specify `rxjs-marbles/tape` in the `import` statement or `require` call:

```ts
import * as tape from "tape";
import { marbles } from "rxjs-marbles";

tape("it should map the values", marbles<tape.Test>((m, t) => {

m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
t.plan(2);

const values = {
a: 1,
b: 2,
c: 3,
d: 4
};

const source = m.hot("--^-a-b-c-|", values);
const subs = "^-------!";
const expected = m.cold("--b-c-d-|", values);

const destination = source.map((value) => value + 1);
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));
```

Note that if Tape's `plan` method is used, `configure` must be called so that Tape is informed of the assertions performed. Each call to `toBeObservable` or `toHaveSubscriptions` counts towards the number of expected assertions passed to `plan`.

The `configure` call and the generic type leads to some boilerplate that can be extracted to a reusable function:

```ts
import * as tape from "tape";
import { marbles } from "rxjs-marbles";

function tapeMarbles(func: (m: Context, t: tape.Test) => void): any {

return marbles<tape.Test>((m, t) => {
m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
func(m, t);
});
}

tape("it should map the values", tapeMarbles((m, t) => {
import { marbles } from "rxjs-marbles/tape";

t.plan(2);
tape("it should map the values", marbles((m, t) => {

const values = {
a: 1,
Expand All @@ -199,7 +151,7 @@ tape("it should map the values", tapeMarbles((m, t) => {
}));
```

Also, if the BDD syntax is something you really don't like, there are some alternative methods on the `Context` that are more Tape-ish:
If the BDD syntax is something you really don't like, there are some alternative methods on the `Context` that are more Tape-ish:

```ts
const destination = source.map((value) => value + 1);
Expand Down
9 changes: 2 additions & 7 deletions fixtures/tape/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
*/

import * as tape from "tape";
import { Context, marbles } from "../../dist";
import { marbles } from "../../dist/tape";

import "rxjs/add/operator/map";

tape("rxjs-marbles failing test", marbles<tape.Test>((m, t) => {

m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
tape("rxjs-marbles failing test", marbles((m, t) => {

const values = {
a: 1,
Expand Down
42 changes: 3 additions & 39 deletions fixtures/tape/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
*/

import * as tape from "tape";
import { Context, marbles } from "../../dist";
import { marbles } from "../../dist/tape";

import "rxjs/add/operator/map";

tape("rxjs-marbles test with inline boilerplate", marbles<tape.Test>((m, t) => {
tape("rxjs-marbles test with BDD methods", marbles((m, t) => {

m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
t.plan(2);

const values = {
Expand All @@ -34,28 +30,7 @@ tape("rxjs-marbles test with inline boilerplate", marbles<tape.Test>((m, t) => {
m.expect(source).toHaveSubscriptions(subs);
}));

tape("rxjs-marbles test without inline boilerplate", boilerplate((m, t) => {

t.plan(2);

const values = {
a: 1,
b: 2,
c: 3,
d: 4
};

const source = m.hot("--^-a-b-c-|", values);
const subs = "^-------!";
const expected = m.cold("--b-c-d-|", values);

const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));

tape("rxjs-marbles test with Tape-ish methods", boilerplate((m, t) => {
tape("rxjs-marbles test with Tape-ish methods", marbles((m, t) => {

t.plan(2);

Expand All @@ -75,14 +50,3 @@ tape("rxjs-marbles test with Tape-ish methods", boilerplate((m, t) => {
m.equal(destination, expected);
m.has(source, subs);
}));

function boilerplate(func: (m: Context, t: tape.Test) => void): any {

return marbles<tape.Test>((m, t) => {
m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
func(m, t);
});
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
},
"scripts": {
"dist": "npm run lint && npm run dist:build && npm run dist:copy",
"dist:build": "npm run dist:clean && tsc -p tsconfig.json && rollup -c rollup-config.js",
"dist:build": "npm run dist:clean && tsc -p tsconfig.json && rollup -c rollup-config.js && rollup -c rollup-config-tape.js",
"dist:clean": "rimraf dist",
"dist:copy": "node scripts/pack.js && cpy CHANGELOG.md LICENSE README.md dist/",
"dist:copy": "node scripts/pack.js && cpy CHANGELOG.md LICENSE README.md dist/ && cpy **/package.json ../dist/ --cwd=source --parents",
"lint": "tslint --project tsconfig.json --type-check source/**/*.ts",
"pack": "node scripts/pack.js",
"prepublishOnly": "npm run test",
Expand Down
8 changes: 8 additions & 0 deletions rollup-config-tape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import config from "./rollup-config";

export default Object.assign({}, config, {
dest: "dist/bundles/rxjs-marbles-tape.umd.js",
entry: "dist/tape/index.js",
external: [...config.external, "tape"],
moduleName: "rxjsMarblesTape"
});
24 changes: 24 additions & 0 deletions source/tape/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license Copyright © 2017 Nicholas Jamieson. All Rights Reserved.
* 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
*/

import * as tape from "tape";
import { Context } from "../context";
import { marbles as _marbles } from "../marbles";

export { configure } from "../configuration";
export * from "../context";
export * from "../expect";

export function marbles(test: (m: Context, t: tape.Test) => void): any {

return _marbles<tape.Test>((m, t) => {
m.configure({
assert: t.ok.bind(t),
assertDeepEqual: t.deepEqual.bind(t)
});
test(m, t);
});
}
4 changes: 4 additions & 0 deletions source/tape/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "../bundles/rxjs-marbles-tape.umd.js",
"typings": "../tape/index.d.ts"
}

0 comments on commit 95580f3

Please sign in to comment.