Skip to content

Commit

Permalink
feat: add option to specify beforeX and afterX hooks
Browse files Browse the repository at this point in the history
closes #2
  • Loading branch information
satya164 committed Jun 1, 2019
1 parent 1a914de commit e352f64
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ The fixtures directory should contain subdirectories with test files. Every test

You can use `fixtures.skip` and `fixtures.only`, similar to Jest's `describe.skip` and `describe.only`. To skip an individual fixture, you can rename the fixture's directory to `skip.name-of-the-fixture`, and to run a specific fixture only, you can rename the fixture's directory to `only.name-of-the-fixture`.

To use hooks such as `beforeEach`, `afterEach`, `beforeAll` and `afterAll`, you can pass an object with these properties as the third argument:

```js
fixtures('my plugin', path.join(__dirname, '__fixtures__'), {
beforeEach() {
// Do some setup here
},
});
```

By default, it will compare the outputs with the files on the filesystem and you have to manually update the files in case of a mismatch. If you're using Jest, you can use the snapshot feature to automatically update the files with a keypress. ([See below](#integration-with-jest-snapshot)) on how to set it up.

### Standalone test
Expand Down
15 changes: 15 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ declare module 'babel-test' {
options: { filename: string }
) => Promise<{ code: string }>;

type DoneCallback = {
(...args: any[]): any;
fail(error?: string | { message: string }): any;
};

type LifecycleCallback = (cb: DoneCallback) => any;

type FixturesOptions = {
beforeEach?: LifecycleCallback;
afterEach?: LifecycleCallback;
beforeAll?: LifecycleCallback;
afterAll?: LifecycleCallback;
};

type TestRunner<T> = (
title: string,
callback: (options: { transform: TransformCallback<T> }) => void
Expand All @@ -16,6 +30,7 @@ declare module 'babel-test' {
type FixturesRunner = (
title: string,
directory: string,
options?: FixturesOptions,
callback?: FixturesCallback
) => void;

Expand Down
44 changes: 36 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ const stripAnsi = require('strip-ansi');
const escapeRegexp = require('escape-string-regexp');
const ErrorStackParser = require('error-stack-parser');

const SUPPORTED_RUNNERS_TEXT =
'Are you using a supported test runner such as Jest (https://jestjs.io) or Mocha (https://mochajs.org)?';

exports.create = function create(config) {
// Check if `describe` and `it` globals are available and throw if not
// This avoids confusion with unsupported test runners and incorect usage
if (!('describe' in global && 'it' in global)) {
throw new Error(
`Couldn't find ${chalk.blue('describe')} and ${chalk.blue(
'it'
)} in the global scope. Are you using a supported test runner such as Jest (https://jestjs.io) or Mocha (https://mochajs.org)?\n`
)} in the global scope. ${SUPPORTED_RUNNERS_TEXT}\n`
);
}

Expand All @@ -39,7 +42,32 @@ exports.create = function create(config) {
)
);

const runner = (directory, callback) => () => {
const runner = (directory, options, callback) => () => {
if (options) {
const hooks = [
'before',
'after',
'beforeEach',
'afterEach',
'beforeAll',
'afterAll',
];

for (const hook of hooks) {
if (options[hook] !== undefined) {
if (global[hook] !== undefined) {
global[hook](options[hook]);
} else {
throw new Error(
`Couldn't find ${chalk.blue(
hook
)} in the global scope. ${SUPPORTED_RUNNERS_TEXT}\n`
);
}
}
}
}

fs.readdirSync(directory)
.filter(f => fs.lstatSync(path.join(directory, f)).isDirectory())
.forEach(f => {
Expand Down Expand Up @@ -160,15 +188,15 @@ exports.create = function create(config) {

// We create a new error here so we can point to user's file in stack trace
// Otherwise stack traces will point to the library code
function fixtures(title, directory, callback = helper(new Error())) {
describe(title, runner(directory, callback));
function fixtures(title, directory, options, callback = helper(new Error())) {
describe(title, runner(directory, options, callback));
}

fixtures.skip = (title, directory, callback = helper(new Error())) =>
describe.skip(title, runner(directory, callback));
fixtures.skip = (title, directory, options, callback = helper(new Error())) =>
describe.skip(title, runner(directory, options, callback));

fixtures.only = (title, directory, callback = helper(new Error())) =>
describe.only(title, runner(directory, callback));
fixtures.only = (title, directory, options, callback = helper(new Error())) =>
describe.only(title, runner(directory, options, callback));

return { test, fixtures };
};

0 comments on commit e352f64

Please sign in to comment.