Skip to content

Commit fbb6c19

Browse files
authoredOct 9, 2022
fix(src): fix plugin run order for fixtures to match tests (#88)
1 parent 481be19 commit fbb6c19

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed
 

‎src/__tests__/plugin-tester.js

+44
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ const runPluginTester = async (...args) => {
4343
}
4444
}
4545

46+
const pluginWithOrderTracking = (orderArray, orderInt) => () => {
47+
return {
48+
visitor: {
49+
Program() {
50+
orderArray.push(orderInt)
51+
},
52+
},
53+
}
54+
}
55+
4656
beforeEach(() => {
4757
equalSpy = jest.spyOn(assert, 'equal')
4858
errorSpy = jest.spyOn(console, 'error').mockImplementation(noop)
@@ -753,6 +763,40 @@ test('appends to root plugins array', async () => {
753763
expect(programVisitor).toHaveBeenCalledTimes(15)
754764
})
755765

766+
test('fixtures run plugins in the same order as tests', async () => {
767+
const runOrder1 = []
768+
const runOrder2 = []
769+
770+
await runPluginTester(
771+
getOptions({
772+
plugin: pluginWithOrderTracking(runOrder1, 2),
773+
babelOptions: {
774+
plugins: [
775+
pluginWithOrderTracking(runOrder1, 1),
776+
pluginWithOrderTracking(runOrder1, 3),
777+
],
778+
},
779+
}),
780+
)
781+
782+
await runPluginTester(
783+
getOptions({
784+
plugin: pluginWithOrderTracking(runOrder2, 2),
785+
tests: null,
786+
fixtures: getFixturePath('creates-output-file'),
787+
babelOptions: {
788+
plugins: [
789+
pluginWithOrderTracking(runOrder2, 1),
790+
pluginWithOrderTracking(runOrder2, 3),
791+
],
792+
},
793+
}),
794+
)
795+
796+
expect(runOrder1).toStrictEqual([1, 3, 2])
797+
expect(runOrder2).toStrictEqual(runOrder1)
798+
})
799+
756800
test('endOfLine - default', async () => {
757801
await runPluginTester(
758802
getOptions({

‎src/plugin-tester.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,28 @@ const createFixtureTests = (fixturesDir, options) => {
307307
...rest
308308
} = options
309309

310-
const hasBabelrc = [
311-
'.babelrc',
312-
'.babelrc.js',
313-
'.babelrc.cjs',
314-
].some(babelrc => fs.existsSync(path.join(fixtureDir, babelrc)))
310+
const hasBabelrc = ['.babelrc', '.babelrc.js', '.babelrc.cjs'].some(
311+
babelrc => fs.existsSync(path.join(fixtureDir, babelrc)),
312+
)
315313

316314
const {babelOptions} = mergeWith(
317315
{},
318316
fullDefaultConfig,
319317
{
320318
babelOptions: {
321-
plugins: [[plugin, mergedFixtureAndPluginOptions]],
322319
// if they have a babelrc, then we'll let them use that
323320
// otherwise, we'll just use our simple config
324321
babelrc: hasBabelrc,
325322
},
326323
},
327324
rest,
325+
{
326+
babelOptions: {
327+
// Ensure `rest` comes before `babelOptions.plugins` to preserve
328+
// default plugin run order
329+
plugins: [[plugin, mergedFixtureAndPluginOptions]],
330+
},
331+
},
328332
mergeCustomizer,
329333
)
330334

0 commit comments

Comments
 (0)
Please sign in to comment.