Skip to content

Commit

Permalink
feat(runners): Add AVA and Jest support.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jul 23, 2017
1 parent e6fc69d commit abcf069
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 14 deletions.
78 changes: 66 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

`rxjs-marbles` is an RxJS [marble testing](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md) library that should be compatible with any test framework. It wraps the RxJS [`TestScheduler`](https://github.com/ReactiveX/rxjs/blob/5.4.2/src/testing/TestScheduler.ts) and provides methods similar to the [basic methods](https://github.com/ReactiveX/rxjs/blob/master/doc/writing-marble-tests.md#basic-methods) used in RxJS's marble tests.

It can be used with [Jasmine](https://github.com/jasmine/jasmine), [Mocha](https://github.com/mochajs/mocha) or [Tape](https://github.com/substack/tape) in the browser or in Node and it supports CommonJS and ES module bundlers.
It can be used with [AVA](https://github.com/avajs/ava), [Jasmine](https://github.com/jasmine/jasmine), [Jest](https://facebook.github.io/jest/), [Mocha](https://github.com/mochajs/mocha) or [Tape](https://github.com/substack/tape) in the browser or in Node and it supports CommonJS and ES module bundlers.

### Why might you need it?

Expand All @@ -28,28 +28,78 @@ Install the package using NPM:
npm install rxjs-marbles --save-dev
```

And import the functions for use with TypeScript or ES2015:
## Usage

### With Jasmine and Mocha

Instead of passing your test function directly to `it`, pass it to the library's `marbles` function, like this:

```js
```ts
import { marbles } from "rxjs-marbles";

it("should map the values", marbles((m) => {

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);
}));
```

Or `require` the module for use with Node or a CommonJS bundler:
### With Jest

As with Jasmine and Mocha, instead of passing your test function directly to Jest, pass it to the library's `marbles` function:

```ts
import { marbles } from "rxjs-marbles";

import "rxjs/add/operator/map";

```js
const { marbles } = require("rxjs-marbles");
test("it should support marble tests", marbles((m) => {

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);
}));
```

## Usage
### With AVA

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

Instead of passing your test function directly to `it`, pass it to the library's `marbles` function, like this:
There is an `/ava` directory in the package that includes a wrapper that will correctly type additional argument and will call `configure` - passing AVA's assertion methods to ensure marble assertions will be counted towards AVA's `plan` - so be sure to specify `rxjs-marbles/ava` in the `import` statement or `require` call:

```ts
import { marbles } from "rxjs-marbles";
import { test } from "ava";
import { marbles } from "rxjs-marbles/ava";

it("should map the values", marbles((m) => {
import "rxjs/add/operator/map";

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

t.plan(2);

const values = {
a: 1,
Expand All @@ -63,9 +113,11 @@ it("should map the values", marbles((m) => {
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);
}));

```

### With Tape
Expand Down Expand Up @@ -99,7 +151,9 @@ tape("it should map the values", marbles((m, t) => {
}));
```

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

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

```ts
const destination = source.map((value) => value + 1);
Expand Down
31 changes: 31 additions & 0 deletions fixtures/ava/failing-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @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 { test } from "ava";
import { marbles } from "../../dist/ava";

import "rxjs/add/operator/map";

test("it should fail", marbles((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("--a-a-a-|", values);

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

m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));
31 changes: 31 additions & 0 deletions fixtures/ava/passing-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @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 { test } from "ava";
import { marbles } from "../../dist/ava";

import "rxjs/add/operator/map";

test("it should support marble tests", marbles((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);
}));
28 changes: 28 additions & 0 deletions fixtures/jest/failing-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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 { marbles } from "../../dist";

import "rxjs/add/operator/map";

test("it should fail", marbles((m) => {

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

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

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

m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));
28 changes: 28 additions & 0 deletions fixtures/jest/passing-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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 { marbles } from "../../dist";

import "rxjs/add/operator/map";

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

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);
}));
6 changes: 6 additions & 0 deletions fixtures/jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"types": ["jest", "node"]
},
"extends": "../tsconfig.json"
}
2 changes: 2 additions & 0 deletions package-dist.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"ava": {},
"devDependencies": {},
"jest": {},
"jsnext:main": "./index.js",
"main": "./bundles/rxjs-marbles.umd.js",
"module": "./index.js",
Expand Down
20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
"description": "An RxJS marble testing library for any test framework",
"devDependencies": {
"@types/jasmine": "^2.5.53",
"@types/jest": "^20.0.4",
"@types/mocha": "^2.2.41",
"@types/tape": "^4.2.30",
"ava": "^0.21.0",
"babel-preset-es2015-rollup": "^3.0.0",
"babel-preset-es2016": "^6.24.0",
"babel-preset-es2017": "^6.24.0",
"cpy-cli": "^1.0.1",
"jasmine": "^2.6.0",
"jest": "^20.0.4",
"mkdirp": "^0.5.1",
"mocha": "^3.2.0",
"rimraf": "^2.6.1",
Expand All @@ -27,15 +30,25 @@
"rxjs": "^5.4.2",
"rxjs-tslint-rules": "^2.0.0",
"tape": "^4.7.0",
"ts-jest": "^20.0.7",
"ts-mocha": "^1.0.3",
"ts-node": "^3.2.0",
"tslint": "^5.5.0",
"typescript": "^2.4.1"
},
"homepage": "https://github.com/cartant/rxjs-marbles",
"jest": {
"transform": {
"^.+\\.tsx?$": "./node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(fixtures/jest/.*-spec)\\.(tsx?|jsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "json", "jsx"]
},
"jsnext:main": "./dist/index.js",
"keywords": [
"ava",
"jasmine",
"jest",
"marble",
"marbles",
"mocha",
Expand All @@ -59,16 +72,19 @@
},
"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 && rollup -c rollup-config-tape.js",
"dist:build": "npm run dist:clean && tsc -p tsconfig.json && npm run dist:rollup",
"dist:clean": "rimraf dist",
"dist:copy": "node scripts/pack.js && cpy CHANGELOG.md LICENSE README.md dist/",
"dist:rollup": "rollup -c rollup-config.js && rollup -c rollup-config-ava.js && rollup -c rollup-config-tape.js",
"lint": "tslint --project tsconfig.json --type-check source/**/*.ts",
"pack": "node scripts/pack.js",
"prepublishOnly": "npm run test",
"rollup": "rollup -c rollup-config.js",
"test": "npm run dist && npm run test-jasmine && npm run test-mocha && npm run test-tape",
"test-ava": "ava fixtures/ava/passing-spec.js",
"test-ava-failing": "ava fixtures/ava/failing-spec.js",
"test-jasmine": "ts-node -P ./fixtures/jasmine/tsconfig.json node_modules/jasmine/bin/jasmine ./fixtures/jasmine/passing-spec.ts",
"test-jasmine-failing": "ts-node -P ./fixtures/jasmine/tsconfig.json node_modules/jasmine/bin/jasmine ./fixtures/jasmine/failing-spec.ts",
"test-jest": "jest",
"test-mocha": "ts-mocha -p ./fixtures/mocha/ ./fixtures/mocha/passing-spec.ts",
"test-mocha-failing": "ts-mocha -p ./fixtures/mocha/ ./fixtures/mocha/failing-spec.ts",
"test-tape": "ts-node -P ./fixtures/tape/tsconfig.json node_modules/tape/bin/tape ./fixtures/tape/passing-spec.ts",
Expand Down
8 changes: 8 additions & 0 deletions rollup-config-ava.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-ava.umd.js",
entry: "dist/ava/index.js",
external: [...config.external, "ava"],
moduleName: "rxjsMarblesAva"
});
1 change: 1 addition & 0 deletions scripts/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ const content = Object.assign(
JSON.parse(fs.readFileSync("./package-dist.json"))
);
fs.writeFileSync("./dist/package.json", JSON.stringify(content, null, 2));
fs.writeFileSync("./dist/ava/package.json", fs.readFileSync("./source/ava/package.json"));
fs.writeFileSync("./dist/tape/package.json", fs.readFileSync("./source/tape/package.json"));
24 changes: 24 additions & 0 deletions source/ava/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 { test, TestContext } from "ava";
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: TestContext) => void): any {

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

0 comments on commit abcf069

Please sign in to comment.