Skip to content

Commit

Permalink
feat: Read nested fixture directories (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and Kent C. Dodds committed Jun 10, 2018
1 parent d48a8fc commit 1efc84a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 58 deletions.
1 change: 1 addition & 0 deletions src/__tests__/fixtures/fixtures/nested/nested-a/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
1 change: 1 addition & 0 deletions src/__tests__/fixtures/fixtures/nested/nested-a/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
1 change: 1 addition & 0 deletions src/__tests__/fixtures/fixtures/nested/nested-b/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
1 change: 1 addition & 0 deletions src/__tests__/fixtures/fixtures/nested/nested-b/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'use strict';
6 changes: 4 additions & 2 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,12 @@ test('can pass tests in fixtures relative to the filename', async () => {
tests: null,
}),
)
expect(describeSpy).toHaveBeenCalledTimes(1)
expect(itSpy).toHaveBeenCalledTimes(3)
expect(describeSpy).toHaveBeenCalledTimes(2)
expect(itSpy).toHaveBeenCalledTimes(5)
expect(itSpy.mock.calls).toEqual([
[`changed`, expect.any(Function)],
[`nested a`, expect.any(Function)],
[`nested b`, expect.any(Function)],
[`unchanged`, expect.any(Function)],
[`without output file`, expect.any(Function)],
])
Expand Down
117 changes: 61 additions & 56 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ const fullDefaultConfig = {
},
}

function pluginTester(
{
/* istanbul ignore next (TODO: write a test for this) */
babel = babelCore,
plugin = requiredParam('plugin'),
pluginName = getPluginName(plugin, babel),
title: describeBlockTitle = pluginName,
pluginOptions,
tests,
fixtures,
fixtureOutputName = 'output',
filename,
...rest
} = {},
) {
function pluginTester({
/* istanbul ignore next (TODO: write a test for this) */
babel = babelCore,
plugin = requiredParam('plugin'),
pluginName = getPluginName(plugin, babel),
title: describeBlockTitle = pluginName,
pluginOptions,
tests,
fixtures,
fixtureOutputName = 'output',
filename,
...rest
} = {}) {
let testNumber = 1
if (fixtures) {
testFixtures({
Expand Down Expand Up @@ -208,55 +206,62 @@ function pluginTester(
}
}

const createFixtureTests = (fixturesDir, options) => {
fs.readdirSync(fixturesDir).forEach(caseName => {
const fixtureDir = path.join(fixturesDir, caseName)
const codePath = path.join(fixtureDir, 'code.js')
const blockTitle = caseName.split('-').join(' ')

if (!pathExists.sync(codePath)) {
describe(blockTitle, () => {
createFixtureTests(fixtureDir, options)
})
return
}

it(blockTitle, () => {
const {plugin, pluginOptions, fixtureOutputName, babel, ...rest} = options

const babelRcPath = path.join(fixtureDir, '.babelrc')

const {babelOptions} = merge(
{},
fullDefaultConfig,
{
babelOptions: {
plugins: [[plugin, pluginOptions]],
// if they have a babelrc, then we'll let them use that
// otherwise, we'll just use our simple config
babelrc: pathExists.sync(babelRcPath),
},
},
rest,
)
const actual = babel.transformFileSync(codePath, babelOptions).code.trim()

const outputPath = path.join(fixtureDir, `${fixtureOutputName}.js`)

if (!fs.existsSync(outputPath)) {
fs.writeFileSync(outputPath, actual)
return
}

const output = fs.readFileSync(outputPath, 'utf8').trim()

assert.equal(actual, output, 'actual output does not match output.js')
})
})
}

function testFixtures({
plugin,
pluginOptions,
title: describeBlockTitle,
fixtures,
fixtureOutputName,
filename,
babel,
...rest
}) {
describe(`${describeBlockTitle} fixtures`, () => {
const fixturesDir = getPath(filename, fixtures)
fs.readdirSync(fixturesDir).forEach(caseName => {
it(caseName.split('-').join(' '), () => {
const fixtureDir = path.join(fixturesDir, caseName)
const codePath = path.join(fixtureDir, 'code.js')
const babelRcPath = path.join(fixtureDir, '.babelrc')

const {babelOptions} = merge(
{},
fullDefaultConfig,
{
babelOptions: {
plugins: [[plugin, pluginOptions]],
// if they have a babelrc, then we'll let them use that
// otherwise, we'll just use our simple config
babelrc: pathExists.sync(babelRcPath),
},
},
rest,
)
const actual = babel
.transformFileSync(codePath, babelOptions)
.code.trim()

const outputPath = path.join(fixtureDir, `${fixtureOutputName}.js`)

if (!fs.existsSync(outputPath)) {
fs.writeFileSync(outputPath, actual)
return
}

const output = fs
.readFileSync(outputPath, 'utf8')
.trim()

assert.equal(actual, output, 'actual output does not match output.js')
})
})
createFixtureTests(fixturesDir, rest)
})
}

Expand Down

0 comments on commit 1efc84a

Please sign in to comment.