Skip to content

Commit 556ca0d

Browse files
merceyzKent C. Dodds
authored and
Kent C. Dodds
committed
fix(fixtures): get options from root options.json (#42)
1 parent 7b9e76d commit 556ca0d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"rootFoo": "rootBar"}

src/__tests__/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ test('can pass tests in fixtures relative to the filename', async () => {
304304
tests: null,
305305
}),
306306
)
307-
expect(describeSpy).toHaveBeenCalledTimes(4)
307+
expect(describeSpy).toHaveBeenCalledTimes(5)
308308
expect(itSpy).toHaveBeenCalledTimes(8)
309309
expect(itSpy.mock.calls).toEqual([
310310
[`changed`, expect.any(Function)],
@@ -617,12 +617,16 @@ test('allows formatting fixtures results', async () => {
617617
})
618618

619619
test('gets options from options.json files when using fixtures', async () => {
620+
const optionRootFoo = jest.fn()
620621
const optionFoo = jest.fn()
621622
const optionBar = jest.fn()
622623
const pluginWithOptions = jest.fn(() => {
623624
return {
624625
visitor: {
625626
Program(programPath, state) {
627+
if (state.opts.rootFoo === 'rootBar') {
628+
optionRootFoo()
629+
}
626630
if (state.opts.foo === 'bar') {
627631
optionFoo()
628632
}
@@ -641,6 +645,7 @@ test('gets options from options.json files when using fixtures', async () => {
641645
}),
642646
)
643647

648+
expect(optionRootFoo).toHaveBeenCalledTimes(8)
644649
expect(optionFoo).toHaveBeenCalledTimes(2)
645650
expect(optionBar).toHaveBeenCalledTimes(1)
646651
})

src/index.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ function pluginTester({
209209
const createFixtureTests = (fixturesDir, options) => {
210210
if (!fs.statSync(fixturesDir).isDirectory()) return
211211

212+
const rootOptionsPath = path.join(fixturesDir, 'options.json')
213+
let rootFixtureOptions = {}
214+
if (pathExists.sync(rootOptionsPath)) {
215+
rootFixtureOptions = require(rootOptionsPath)
216+
}
217+
212218
fs.readdirSync(fixturesDir).forEach(caseName => {
213219
const fixtureDir = path.join(fixturesDir, caseName)
214220
const optionsPath = path.join(fixtureDir, 'options.json')
@@ -228,7 +234,11 @@ const createFixtureTests = (fixturesDir, options) => {
228234
describe(blockTitle, () => {
229235
createFixtureTests(fixtureDir, {
230236
...options,
231-
pluginOptions: {...options.pluginOptions, ...fixturePluginOptions},
237+
pluginOptions: {
238+
...rootFixtureOptions,
239+
...options.pluginOptions,
240+
...fixturePluginOptions,
241+
},
232242
})
233243
})
234244
return
@@ -252,7 +262,16 @@ const createFixtureTests = (fixturesDir, options) => {
252262
fullDefaultConfig,
253263
{
254264
babelOptions: {
255-
plugins: [[plugin, {...pluginOptions, ...fixturePluginOptions}]],
265+
plugins: [
266+
[
267+
plugin,
268+
{
269+
...rootFixtureOptions,
270+
...pluginOptions,
271+
...fixturePluginOptions,
272+
},
273+
],
274+
],
256275
// if they have a babelrc, then we'll let them use that
257276
// otherwise, we'll just use our simple config
258277
babelrc: pathExists.sync(babelRcPath),

0 commit comments

Comments
 (0)