Skip to content

Commit 8c8b858

Browse files
authoredOct 9, 2022
feat(src): add support for arbitrary run order of plugin under test (#91)
1 parent fbb6c19 commit 8c8b858

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed
 

‎src/__tests__/plugin-tester.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import assert from 'assert'
44
import {EOL} from 'os'
5-
import pluginTester from '../plugin-tester'
5+
import pluginTester, {runPluginUnderTestHere} from '../plugin-tester'
66
import prettierFormatter from '../formatters/prettier'
77
import unstringSnapshotSerializer from '../unstring-snapshot-serializer'
88
import identifierReversePlugin from './helpers/identifier-reverse-plugin'
@@ -797,6 +797,46 @@ test('fixtures run plugins in the same order as tests', async () => {
797797
expect(runOrder2).toStrictEqual(runOrder1)
798798
})
799799

800+
test('can use runPluginUnderTestHere symbol to alter plugin run order in tests', async () => {
801+
const runOrder = []
802+
803+
await runPluginTester(
804+
getOptions({
805+
plugin: pluginWithOrderTracking(runOrder, 2),
806+
babelOptions: {
807+
plugins: [
808+
pluginWithOrderTracking(runOrder, 1),
809+
runPluginUnderTestHere,
810+
pluginWithOrderTracking(runOrder, 3),
811+
],
812+
},
813+
}),
814+
)
815+
816+
expect(runOrder).toStrictEqual([1, 2, 3])
817+
})
818+
819+
test('can use runPluginUnderTestHere symbol to alter plugin run order in fixtures', async () => {
820+
const runOrder = []
821+
822+
await runPluginTester(
823+
getOptions({
824+
plugin: pluginWithOrderTracking(runOrder, 2),
825+
tests: null,
826+
fixtures: getFixturePath('creates-output-file'),
827+
babelOptions: {
828+
plugins: [
829+
pluginWithOrderTracking(runOrder, 1),
830+
runPluginUnderTestHere,
831+
pluginWithOrderTracking(runOrder, 3),
832+
],
833+
},
834+
}),
835+
)
836+
837+
expect(runOrder).toStrictEqual([1, 2, 3])
838+
})
839+
800840
test('endOfLine - default', async () => {
801841
await runPluginTester(
802842
getOptions({

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pluginTester from './plugin-tester'
1+
import pluginTester, {runPluginUnderTestHere} from './plugin-tester'
22
import prettierFormatter from './formatters/prettier'
33
import unstringSnapshotSerializer from './unstring-snapshot-serializer'
44

@@ -7,7 +7,7 @@ if (typeof expect !== 'undefined' && expect.addSnapshotSerializer) {
77
expect.addSnapshotSerializer(unstringSnapshotSerializer)
88
}
99

10-
export {unstringSnapshotSerializer, prettierFormatter}
10+
export {unstringSnapshotSerializer, prettierFormatter, runPluginUnderTestHere}
1111

1212
function defaultPluginTester(options) {
1313
return pluginTester({formatResult: prettierFormatter, ...options})

‎src/plugin-tester.js

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {EOL} from 'os'
55
import mergeWith from 'lodash.mergewith'
66
import stripIndent from 'strip-indent'
77

8+
export const runPluginUnderTestHere = Symbol('run-plugin-under-test-here')
9+
810
const noop = () => {}
911

1012
// thanks to node throwing an error if you try to use instanceof with an arrow
@@ -94,6 +96,8 @@ function pluginTester({
9496
'Cannot enable both skip and only on a test',
9597
)
9698

99+
finalizePluginRunOrder(babelOptions)
100+
97101
if (skip) {
98102
// eslint-disable-next-line jest/no-disabled-tests
99103
it.skip(title, testerWrapper)
@@ -332,6 +336,8 @@ const createFixtureTests = (fixturesDir, options) => {
332336
mergeCustomizer,
333337
)
334338

339+
finalizePluginRunOrder(babelOptions)
340+
335341
const input = fs.readFileSync(codePath).toString()
336342
let transformed, ext
337343
if (babel.transformAsync) {
@@ -447,6 +453,16 @@ function requiredParam(name) {
447453
throw new Error(`${name} is a required parameter.`)
448454
}
449455

456+
function finalizePluginRunOrder(babelOptions) {
457+
if (babelOptions.plugins.includes(runPluginUnderTestHere)) {
458+
babelOptions.plugins.splice(
459+
babelOptions.plugins.indexOf(runPluginUnderTestHere),
460+
1,
461+
babelOptions.plugins.pop(),
462+
)
463+
}
464+
}
465+
450466
export default pluginTester
451467

452468
// unfortunately the ESLint plugin for Jest thinks this is a test file

0 commit comments

Comments
 (0)
Please sign in to comment.