ava-fixture
Advanced tools
Comparing version
import test from 'ava'; | ||
import { FixtureOptions, FixtureBaselineTest, FixtureTest } from './interfaces'; | ||
export declare const fixtureDefaultOptions: FixtureOptions; | ||
import { FixtureBaselineTest, FixtureTest } from './interfaces'; | ||
/** | ||
* Creates fixture test. | ||
* `cwd` is set to the case directory during the test. | ||
* `cwd` will be set to the case directory during the test. | ||
* @param ava The ava module function (`import ava from 'ava'`). | ||
* @param path Absolute or relative path to the fixture cases parent directory. In ava@0.17, cwd for relative path is set to the project root, instead of test file location. | ||
* @param casesPath Absolute or relative path (from project root) to the fixture cases parent directory. | ||
* @param baselinesPath Absolute or relative path (from project root) to the fixture baselines parent directory. | ||
* @param resultsPath Absolute or relative path (from project root) to the fixture results parent directory. | ||
*/ | ||
export declare function fixture(ava: typeof test, path: string, options: FixtureOptions): FixtureBaselineTest; | ||
export declare function fixture(ava: typeof test, path: string): FixtureTest; | ||
export declare function fixture(ava: typeof test, casesPath: string, baselinesPath: string, resultsPath: string): FixtureBaselineTest; | ||
export declare function fixture(ava: typeof test, casesPath: string): FixtureTest; |
"use strict"; | ||
var path_1 = require("path"); | ||
var curryMatch_1 = require("./curryMatch"); | ||
exports.fixtureDefaultOptions = { | ||
casesPath: 'cases', | ||
baselinesPath: 'baselines', | ||
resultsPath: 'results' | ||
}; | ||
function fixture(ava, path, options) { | ||
function fixture(ava, casesPath, baselinesPath, resultsPath) { | ||
if (baselinesPath && !resultsPath) { | ||
throw new Error('baselines and results must be specified together'); | ||
} | ||
function curry(testfn) { | ||
@@ -17,15 +15,9 @@ return (function (title, caseName, run) { | ||
} | ||
var d; | ||
if (options) { | ||
d = { | ||
casePath: path_1.resolve(path, options.casesPath, caseName), | ||
baselinePath: path_1.resolve(path, options.baselinesPath, caseName), | ||
resultPath: path_1.resolve(path, options.resultsPath, caseName) | ||
}; | ||
var d = { | ||
casePath: path_1.resolve(casesPath, caseName) | ||
}; | ||
if (baselinesPath) { | ||
d.baselinePath = path_1.resolve(baselinesPath, caseName); | ||
d.resultPath = path_1.resolve(resultsPath, caseName); | ||
} | ||
else { | ||
d = { | ||
casePath: path_1.resolve(path, caseName) | ||
}; | ||
} | ||
return testfn((title ? title + ' ' : '') + "(fixture: " + caseName + ")", function (t) { | ||
@@ -35,3 +27,3 @@ var result; | ||
try { | ||
if (options) { | ||
if (baselinesPath) { | ||
d.match = curryMatch_1.curryMatch(d.baselinePath, d.resultPath, t); | ||
@@ -58,10 +50,3 @@ } | ||
skip: curry(ava.skip), | ||
serial: curry(ava.serial), | ||
todo: ava.todo, | ||
cb: curry(ava.cb), | ||
failing: curry(ava.failing), | ||
before: ava.before, | ||
beforeEach: ava.beforeEach, | ||
after: ava.after, | ||
afterEach: ava.afterEach | ||
todo: ava.todo | ||
}; | ||
@@ -71,5 +56,5 @@ for (var key in others) { | ||
} | ||
return fn; // fn as typeof fn & typeof others | ||
return fn; | ||
} | ||
exports.fixture = fixture; | ||
//# sourceMappingURL=fixture.js.map |
@@ -1,4 +0,3 @@ | ||
import { fixture, fixtureDefaultOptions } from './fixture'; | ||
import { fixture } from './fixture'; | ||
export default fixture; | ||
export { fixtureDefaultOptions }; | ||
export * from './interfaces'; |
"use strict"; | ||
var fixture_1 = require("./fixture"); | ||
exports.fixtureDefaultOptions = fixture_1.fixtureDefaultOptions; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = fixture_1.fixture; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { Test, ContextualTestContext, ContextualCallbackTestContext, Observable } from 'ava'; | ||
import { ContextualTestContext, ContextualCallbackTestContext, Observable } from 'ava'; | ||
export interface ContextualDiffContext { | ||
@@ -30,13 +30,2 @@ /** | ||
export declare type FixtureContextualTest = (t: ContextualTestContext, d: ContextualDiffContext) => PromiseLike<any> | Iterator<any> | Observable | void; | ||
export declare type FixtureContextualSerialTest = (t: ContextualTestContext, d: ContextualDiffContext) => void; | ||
export declare type FixtureContextualCallbackTest = (t: ContextualCallbackTestContext, d: ContextualDiffContext) => void; | ||
export interface BeforeRunner { | ||
(title: string, run: Test): void; | ||
(run: Test): void; | ||
skip: FixtureRunner; | ||
cb: FixtureCallbackRunner; | ||
} | ||
export interface AfterRunner extends BeforeRunner { | ||
always: BeforeRunner; | ||
} | ||
export interface FixtureContextualBaselineTestFunction { | ||
@@ -74,62 +63,2 @@ /** | ||
} | ||
export interface FixtureContextualBaselineSerialTestFunction { | ||
/** | ||
* Runs a fixture test. | ||
* @param title Title of the test (for display and filtering). | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(title: string, caseName: string, run: FixtureContextualBaselineSerialTest): void; | ||
/** | ||
* Runs a fixture test. | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(caseName: string, run: FixtureContextualBaselineSerialTest): void; | ||
} | ||
export interface FixtureContextualSerialTestFunction { | ||
/** | ||
* Runs a fixture test. | ||
* @param title Title of the test (for display and filtering). | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(title: string, caseName: string, run: FixtureContextualSerialTest): void; | ||
/** | ||
* Runs a fixture test. | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(caseName: string, run: FixtureContextualSerialTest): void; | ||
} | ||
export interface FixtureContextualBaselineCallbackTestFunction { | ||
/** | ||
* Runs a fixture test. | ||
* @param title Title of the test (for display and filtering). | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(title: string, caseName: string, run: FixtureContextualBaselineCallbackTest): void; | ||
/** | ||
* Runs a fixture test. | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(caseName: string, run: FixtureContextualBaselineCallbackTest): void; | ||
} | ||
export interface FixtureContextualCallbackTestFunction { | ||
/** | ||
* Runs a fixture test. | ||
* @param title Title of the test (for display and filtering). | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(title: string, caseName: string, run: FixtureContextualCallbackTest): void; | ||
/** | ||
* Runs a fixture test. | ||
* @param caseName Name of the test case, matching the folder under `path`. | ||
* @param run The test function. | ||
*/ | ||
(caseName: string, run: FixtureContextualCallbackTest): void; | ||
} | ||
export interface FixtureRunner extends FixtureContextualTestFunction { | ||
@@ -142,21 +71,3 @@ skip: FixtureRunner; | ||
} | ||
export interface FixtureOptions { | ||
/** | ||
* Path to the fixture's cases root folder. | ||
*/ | ||
casesPath: string; | ||
/** | ||
* Path to the fixture's baselines root folder. | ||
*/ | ||
baselinesPath: string; | ||
/** | ||
* Path to the fixture's results root folder. | ||
*/ | ||
resultsPath: string; | ||
} | ||
export interface FixtureBaselineTest extends FixtureContextualBaselineTestFunction { | ||
before: BeforeRunner; | ||
serial: FixtureContextualBaselineSerialTestFunction; | ||
failing: FixtureContextualBaselineCallbackTestFunction; | ||
cb: FixtureContextualBaselineCallbackTestFunction; | ||
todo(title: string): void; | ||
@@ -167,14 +78,4 @@ only(title: string, caseName: string, run: (t: ContextualTestContext, d: ContextualBaselineDiffContext) => any): void; | ||
skip(caseName: string, run: (t: ContextualTestContext, d: ContextualBaselineDiffContext) => any): void; | ||
after(title: string, run: (t: ContextualTestContext) => void): void; | ||
after(run: (t: ContextualTestContext) => void): void; | ||
beforeEach(title: string, run: (t: ContextualTestContext) => void): void; | ||
beforeEach(run: (t: ContextualTestContext) => void): void; | ||
afterEach(title: string, run: (t: ContextualTestContext) => void): void; | ||
afterEach(run: (t: ContextualTestContext) => void): void; | ||
} | ||
export interface FixtureTest extends FixtureContextualTestFunction { | ||
before: BeforeRunner; | ||
serial: FixtureContextualSerialTestFunction; | ||
failing: FixtureContextualCallbackTestFunction; | ||
cb: FixtureContextualCallbackTestFunction; | ||
todo(title: string): void; | ||
@@ -185,8 +86,2 @@ only(title: string, caseName: string, run: (t: ContextualTestContext, d: ContextualDiffContext) => any): void; | ||
skip(caseName: string, run: (t: ContextualTestContext, d: ContextualDiffContext) => any): void; | ||
after(title: string, run: (t: ContextualTestContext) => void): void; | ||
after(run: (t: ContextualTestContext) => void): void; | ||
beforeEach(title: string, run: (t: ContextualTestContext) => void): void; | ||
beforeEach(run: (t: ContextualTestContext) => void): void; | ||
afterEach(title: string, run: (t: ContextualTestContext) => void): void; | ||
afterEach(run: (t: ContextualTestContext) => void): void; | ||
} |
{ | ||
"name": "ava-fixture", | ||
"description": "Write fixture tests with ava", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "dist/commonjs/index.js", | ||
@@ -6,0 +6,0 @@ "typings": "dist/commonjs/index.d.ts", |
@@ -12,6 +12,23 @@ # ava-fixture | ||
Fixture tests are tests that require access to some files. The tests may write files and in that case the files can be compared with a baseline (i.e. Baseline Tests) | ||
Fixture tests are tests that require access to some files. | ||
The tests may write files and in that case the files can be compared with a baseline (i.e. Baseline Tests) | ||
## Usage | ||
Assume you have the following folders: | ||
```sh | ||
+ fixtures | ||
+ cases | ||
+ case-1 | ||
- something.txt | ||
+ other-cases | ||
+ baselines | ||
+ case-1 | ||
- something.txt | ||
+ results # empty, not check into repository | ||
``` | ||
When you only need to access files in each test case (i.e. don't need to perform baseline test): | ||
```ts | ||
@@ -22,10 +39,12 @@ import ava from 'ava'; | ||
// Point to the base folder which contain the fixtures. | ||
const ftest = fixture(ava, '../fixture/cases'); | ||
// Relative path starts from project root. | ||
const ftest = fixture(ava, 'fixture/cases'); | ||
// You can also use absolute path. | ||
// const ftest = fixture(ava, join(process.env.PWD, 'fixture/cases')); | ||
// const ftest = fixture(ava, join(process.cwd(), 'fixture/cases')); | ||
ftest('test title', 'fixture-name', (t, path) => { | ||
ftest('test title', 'case-1', (t) => { | ||
// t is ava test. | ||
// path is the path to the fixture-name folder. | ||
// process.cwd() points to `case-1` | ||
// ...test away | ||
@@ -35,31 +54,37 @@ }); | ||
## API | ||
When you want to perform baseline tests: | ||
typings is available so just follow the code completion. | ||
```ts | ||
import test from 'ava'; | ||
import fixture from 'ava-fixture'; | ||
### `fixture(ava: Ava.Test, absOrRelativePath: string): FixtureTest` | ||
// Point to the base folder which contain the fixtures. | ||
// Relative path starts from project root. | ||
const btest = fixture(test, 'fixture/cases', 'fixture/baselines', 'fixture/results'); | ||
### `fixtureTest([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
btest('test title', 'case-1', (t, d) => { | ||
// t is ava test. | ||
// process.cwd() points to `case-1` | ||
// d.casePath, d.baselinePath, d.resultPath points to respective folder for `case-1` | ||
// ...do tests | ||
### `fixtureTest.serial([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
// `d.match()` will check if the result folder has the same content as the baseline folder. | ||
return d.match() | ||
}); | ||
``` | ||
### `fixtureTest.cb([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
## Other API | ||
### `fixtureTest.only([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
```ts | ||
import test from 'ava'; | ||
import fixture from 'ava-fixture'; | ||
### `fixtureTest.skip([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
const ftest = fixture(test, 'fixture/cases'); | ||
### `fixtureTest.todo([title: string], caseName: string): void` | ||
ftest.only(...) | ||
ftest.skip(...) | ||
``` | ||
### `fixtureTest.failing([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
For `before()`, `beforeEach()`, `after()`, `afterEach()`, `todo()`, use `ava` directly. | ||
### `fixtureTest.before([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
### `fixtureTest.after([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
### `fixtureTest.beforeEach([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
### `fixtureTest.afterEach([title], caseName, (t, casePath) => Promise<any> | void): void` | ||
## Contribute | ||
@@ -66,0 +91,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
140
21.74%26518
-29.63%187
-39.29%