From e352f649f8a0dfe34a6e1bc4521a67b3bae854cd Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Sat, 1 Jun 2019 14:13:15 +0200 Subject: [PATCH] feat: add option to specify beforeX and afterX hooks closes #2 --- README.md | 10 ++++++++++ index.d.ts | 15 +++++++++++++++ src/index.js | 44 ++++++++++++++++++++++++++++++++++++-------- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8b6bdae..8f15590 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.d.ts b/index.d.ts index 2695057..4abc3bc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 = ( title: string, callback: (options: { transform: TransformCallback }) => void @@ -16,6 +30,7 @@ declare module 'babel-test' { type FixturesRunner = ( title: string, directory: string, + options?: FixturesOptions, callback?: FixturesCallback ) => void; diff --git a/src/index.js b/src/index.js index 358530d..e3e7cf5 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,9 @@ 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 @@ -15,7 +18,7 @@ exports.create = function create(config) { 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` ); } @@ -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 => { @@ -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 }; };