Skip to content

Commit

Permalink
feat(src): add support for arbitrary run order of plugin under test (#91
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Xunnamius authored Oct 9, 2022
1 parent fbb6c19 commit 8c8b858
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
42 changes: 41 additions & 1 deletion src/__tests__/plugin-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import assert from 'assert'
import {EOL} from 'os'
import pluginTester from '../plugin-tester'
import pluginTester, {runPluginUnderTestHere} from '../plugin-tester'
import prettierFormatter from '../formatters/prettier'
import unstringSnapshotSerializer from '../unstring-snapshot-serializer'
import identifierReversePlugin from './helpers/identifier-reverse-plugin'
Expand Down Expand Up @@ -797,6 +797,46 @@ test('fixtures run plugins in the same order as tests', async () => {
expect(runOrder2).toStrictEqual(runOrder1)
})

test('can use runPluginUnderTestHere symbol to alter plugin run order in tests', async () => {
const runOrder = []

await runPluginTester(
getOptions({
plugin: pluginWithOrderTracking(runOrder, 2),
babelOptions: {
plugins: [
pluginWithOrderTracking(runOrder, 1),
runPluginUnderTestHere,
pluginWithOrderTracking(runOrder, 3),
],
},
}),
)

expect(runOrder).toStrictEqual([1, 2, 3])
})

test('can use runPluginUnderTestHere symbol to alter plugin run order in fixtures', async () => {
const runOrder = []

await runPluginTester(
getOptions({
plugin: pluginWithOrderTracking(runOrder, 2),
tests: null,
fixtures: getFixturePath('creates-output-file'),
babelOptions: {
plugins: [
pluginWithOrderTracking(runOrder, 1),
runPluginUnderTestHere,
pluginWithOrderTracking(runOrder, 3),
],
},
}),
)

expect(runOrder).toStrictEqual([1, 2, 3])
})

test('endOfLine - default', async () => {
await runPluginTester(
getOptions({
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pluginTester from './plugin-tester'
import pluginTester, {runPluginUnderTestHere} from './plugin-tester'
import prettierFormatter from './formatters/prettier'
import unstringSnapshotSerializer from './unstring-snapshot-serializer'

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

export {unstringSnapshotSerializer, prettierFormatter}
export {unstringSnapshotSerializer, prettierFormatter, runPluginUnderTestHere}

function defaultPluginTester(options) {
return pluginTester({formatResult: prettierFormatter, ...options})
Expand Down
16 changes: 16 additions & 0 deletions src/plugin-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {EOL} from 'os'
import mergeWith from 'lodash.mergewith'
import stripIndent from 'strip-indent'

export const runPluginUnderTestHere = Symbol('run-plugin-under-test-here')

const noop = () => {}

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

finalizePluginRunOrder(babelOptions)

if (skip) {
// eslint-disable-next-line jest/no-disabled-tests
it.skip(title, testerWrapper)
Expand Down Expand Up @@ -332,6 +336,8 @@ const createFixtureTests = (fixturesDir, options) => {
mergeCustomizer,
)

finalizePluginRunOrder(babelOptions)

const input = fs.readFileSync(codePath).toString()
let transformed, ext
if (babel.transformAsync) {
Expand Down Expand Up @@ -447,6 +453,16 @@ function requiredParam(name) {
throw new Error(`${name} is a required parameter.`)
}

function finalizePluginRunOrder(babelOptions) {
if (babelOptions.plugins.includes(runPluginUnderTestHere)) {
babelOptions.plugins.splice(
babelOptions.plugins.indexOf(runPluginUnderTestHere),
1,
babelOptions.plugins.pop(),
)
}
}

export default pluginTester

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

0 comments on commit 8c8b858

Please sign in to comment.