Skip to content

Commit

Permalink
feat(cases): Add support for tests with cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Sep 15, 2017
1 parent 0bfc67f commit 4d3559e
Show file tree
Hide file tree
Showing 30 changed files with 337 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fixtures/ava/failing-spec.js → __tests__/failing-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { test } from "ava";
import { marbles } from "../../dist/ava";
import { marbles } from "../dist/ava";

import "rxjs/add/operator/map";

Expand Down
29 changes: 28 additions & 1 deletion fixtures/ava/passing-spec.js → __tests__/passing-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { test } from "ava";
import { marbles } from "../../dist/ava";
import { cases, marbles } from "../dist/ava";

import "rxjs/add/operator/map";

Expand All @@ -29,3 +29,30 @@ test("it should support marble tests", marbles((m, t) => {
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));

cases("should support cases", (m, c, t) => {

t.plan(1);

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

const source = m.hot(c.s, values);
const expected = m.cold(c.e, values);
const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
}, {
"non-empty": {
s: "-a-b-c-|",
e: "-b-c-d-|"
},
"empty": {
s: "-|",
e: "-|"
}
});
3 changes: 3 additions & 0 deletions fixtures/ava/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Try as I might, I was not able to get AVA to use tests located in this directory.

Instead, the AVA tests are located in the `__tests__` directory in the project's root.
2 changes: 1 addition & 1 deletion fixtures/jasmine/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { marbles } from "../../dist";
import { marbles } from "../../dist/jasmine";

import "rxjs/add/operator/map";

Expand Down
28 changes: 27 additions & 1 deletion fixtures/jasmine/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* 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 { marbles } from "../../dist";
import { cases, marbles } from "../../dist/jasmine";

import "rxjs/add/operator/map";

Expand All @@ -28,4 +29,29 @@ describe("rxjs-marbles", () => {
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));

cases("should support cases", (m, c) => {

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

const source = m.hot(c.s, values);
const expected = m.cold(c.e, values);
const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
}, {
"non-empty": {
s: "-a-b-c-|",
e: "-b-c-d-|"
},
"empty": {
s: "-|",
e: "-|"
}
});
});
2 changes: 1 addition & 1 deletion fixtures/jest/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { marbles } from "../../dist";
import { marbles } from "../../dist/jest";

import "rxjs/add/operator/map";

Expand Down
28 changes: 27 additions & 1 deletion fixtures/jest/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* 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 { marbles } from "../../dist";
import { cases, marbles } from "../../dist/jest";

import "rxjs/add/operator/map";

Expand All @@ -26,3 +27,28 @@ test("it should support marble tests", marbles((m) => {
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(subs);
}));

cases("should support cases", (m, c) => {

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

const source = m.hot(c.s, values);
const expected = m.cold(c.e, values);
const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
}, {
"non-empty": {
s: "-a-b-c-|",
e: "-b-c-d-|"
},
"empty": {
s: "-|",
e: "-|"
}
});
2 changes: 1 addition & 1 deletion fixtures/mocha/failing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* found in the LICENSE file at https://github.com/cartant/rxjs-marbles
*/

import { marbles } from "../../dist";
import { marbles } from "../../dist/mocha";

import "rxjs/add/operator/map";

Expand Down
28 changes: 27 additions & 1 deletion fixtures/mocha/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* 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 { marbles } from "../../dist";
import { cases, marbles } from "../../dist/mocha";

import "rxjs/add/operator/map";

Expand Down Expand Up @@ -156,4 +157,29 @@ describe("rxjs-marbles", () => {
m.expect(destination, unsubs).toBeObservable(expected, values);
m.expect(source).toHaveSubscriptions(subs);
}));

cases("should support cases", (m, c) => {

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

const source = m.hot(c.s, values);
const expected = m.cold(c.e, values);
const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
}, {
"non-empty": {
s: "-a-b-c-|",
e: "-b-c-d-|"
},
"empty": {
s: "-|",
e: "-|"
}
});
});
30 changes: 29 additions & 1 deletion fixtures/tape/passing-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* 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 * as tape from "tape";
import { marbles } from "../../dist/tape";
import { cases, marbles } from "../../dist/tape";

import "rxjs/add/operator/map";

Expand Down Expand Up @@ -145,3 +146,30 @@ tape("should support string-based assertions with unsubscriptions and values", m
m.equal(destination, unsubs, expected, values);
m.has(source, subs);
}));

cases("should support cases", (m, c, t) => {

t.plan(1);

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

const source = m.hot(c.s, values);
const expected = m.cold(c.e, values);
const destination = source.map((value) => value + 1);

m.expect(destination).toBeObservable(expected);
}, {
"non-empty": {
s: "-a-b-c-|",
e: "-b-c-d-|"
},
"empty": {
s: "-|",
e: "-|"
}
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
"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",
"dist:rollup": "rollup -c rollup-config.js && rollup -c rollup-config-ava.js && rollup -c rollup-config-jasmine.js && rollup -c rollup-config-jest.js && rollup -c rollup-config-mocha.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",
"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": "ava --verbose",
"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",
Expand Down
1 change: 1 addition & 0 deletions rollup-config-ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from "./rollup-config";

export default Object.assign({}, config, {
external: [...config.external, "ava"],
globals: Object.assign({}, config.globals, { "ava": "unsupported" }),
input: "dist/ava/index.js",
name: "rxjsMarblesAva",
output: Object.assign({}, config.output, { file: "dist/bundles/rxjs-marbles-ava.umd.js" })
Expand Down
7 changes: 7 additions & 0 deletions rollup-config-jasmine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config from "./rollup-config";

export default Object.assign({}, config, {
input: "dist/jasmine/index.js",
name: "rxjsMarblesJasmine",
output: Object.assign({}, config.output, { file: "dist/bundles/rxjs-marbles-jasmine.umd.js" })
});
7 changes: 7 additions & 0 deletions rollup-config-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config from "./rollup-config";

export default Object.assign({}, config, {
input: "dist/jest/index.js",
name: "rxjsMarblesJest",
output: Object.assign({}, config.output, { file: "dist/bundles/rxjs-marbles-jest.umd.js" })
});
7 changes: 7 additions & 0 deletions rollup-config-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import config from "./rollup-config";

export default Object.assign({}, config, {
input: "dist/mocha/index.js",
name: "rxjsMarblesMocha",
output: Object.assign({}, config.output, { file: "dist/bundles/rxjs-marbles-mocha.umd.js" })
});
1 change: 1 addition & 0 deletions rollup-config-tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from "./rollup-config";

export default Object.assign({}, config, {
external: [...config.external, "tape"],
globals: Object.assign({}, config.globals, { "tape": "unsupported" }),
input: "dist/tape/index.js",
name: "rxjsMarblesTape",
output: Object.assign({}, config.output, { file: "dist/bundles/rxjs-marbles-tape.umd.js" })
Expand Down
3 changes: 1 addition & 2 deletions scripts/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ 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"));
["ava", "jasmine", "jest", "mocha", "tape"].forEach((name) => fs.writeFileSync(`./dist/${name}/package.json`, fs.readFileSync(`./source/${name}/package.json`)));
15 changes: 13 additions & 2 deletions source/ava/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@
*/

import { test, TestContext } from "ava";
import { _cases, NamedCase, UnnamedCase } from "../cases";
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 {
export function cases<T extends UnnamedCase>(name: string, func: (context: Context, options: T, t: TestContext) => void, cases: { [key: string]: T }): void;
export function cases<T extends NamedCase>(name: string, func: (context: Context, options: T, t: TestContext) => void, cases: T[]): void;
export function cases(name: string, func: any, cases: any): void {

_cases((c) => {
const t = c.only ? test.only : c.skip ? test.skip : test;
t(`${name} / ${c.name}`, marbles((m, t) => func(m, c, t)));
}, cases);
}

export function marbles(func: (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);
func(m, t);
});
}
1 change: 1 addition & 0 deletions source/ava/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"main": "../bundles/rxjs-marbles-ava.umd.js",
"module": "./index.js",
"typings": "./index.d.ts"
}
27 changes: 27 additions & 0 deletions source/cases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @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 isArray from "lodash-es/isArray";

export interface UnnamedCase {
[key: string]: any;
only?: boolean;
skip?: boolean;
}

export interface NamedCase extends UnnamedCase {
name: string;
}

export function _cases<T extends UnnamedCase>(adapter: (c: NamedCase) => void, cases: { [key: string]: T }): void;
export function _cases<T extends NamedCase>(adapter: (c: NamedCase) => void, cases: T[]): void;
export function _cases(adapter: (c: NamedCase) => void, cases: any): void {

if (!isArray(cases)) {
cases = Object.keys(cases).map((key) => ({ ...cases[key], name: key }));
}
cases.forEach(adapter);
}
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

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

0 comments on commit 4d3559e

Please sign in to comment.