babel-test
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -11,2 +11,16 @@ declare module 'babel-test' { | ||
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> = ( | ||
@@ -20,2 +34,3 @@ title: string, | ||
directory: string, | ||
options?: FixturesOptions, | ||
callback?: FixturesCallback | ||
@@ -22,0 +37,0 @@ ) => void; |
{ | ||
"name": "babel-test", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "An opinionated library to make testing Babel plugins easier", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -88,2 +88,12 @@ # babel-test | ||
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. | ||
@@ -90,0 +100,0 @@ |
@@ -11,2 +11,5 @@ /* eslint-disable jest/no-disabled-tests, jest/no-focused-tests */ | ||
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) { | ||
@@ -19,3 +22,3 @@ // Check if `describe` and `it` globals are available and throw if not | ||
'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` | ||
); | ||
@@ -44,3 +47,28 @@ } | ||
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) | ||
@@ -166,13 +194,13 @@ .filter(f => fs.lstatSync(path.join(directory, f)).isDirectory()) | ||
// 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 }; | ||
}; |
18964
275
193