Skip to content

Commit 4ea283f

Browse files
committedJan 10, 2023
feat(src): implement standard setup/teardown run order
1 parent 4754f42 commit 4ea283f

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed
 

‎src/plugin-tester.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const isIntegerRegExp = /^\d+$/;
4141

4242
const isIntegerRangeRegExp = /^(?<startStr>\d+)-(?<endStr>\d+)$/;
4343

44+
const noop = () => undefined;
45+
Object.freeze(noop);
46+
4447
export default pluginTester;
4548

4649
/**
@@ -122,8 +125,8 @@ export function pluginTester(options: PluginTesterOptions = {}) {
122125
formatResult: ((r) => r) as ResultFormatter,
123126
snapshot: false,
124127
fixtureOutputName: 'output',
125-
setup: () => undefined,
126-
teardown: () => undefined
128+
setup: noop,
129+
teardown: noop
127130
},
128131
options,
129132
mergeCustomizer
@@ -198,9 +201,9 @@ export function pluginTester(options: PluginTesterOptions = {}) {
198201
// * https://xunn.at/babel-helper-plugin-utils-src
199202
return rawBaseConfig.plugin!(
200203
{
201-
assertVersion: () => undefined,
202-
targets: () => undefined,
203-
assumption: () => undefined
204+
assertVersion: noop,
205+
targets: noop,
206+
assumption: noop
204207
},
205208
{},
206209
process.cwd()
@@ -431,7 +434,7 @@ export function pluginTester(options: PluginTesterOptions = {}) {
431434
}
432435

433436
const rootOptions = mergeWith(
434-
{ setup: () => undefined, teardown: () => undefined },
437+
{ setup: noop, teardown: noop } as object,
435438
fixtureOptions,
436439
readFixtureOptions(fixturesDirectory),
437440
mergeCustomizer
@@ -588,8 +591,8 @@ export function pluginTester(options: PluginTesterOptions = {}) {
588591
only,
589592
skip,
590593
expectedError: throws ?? error,
591-
testSetup: setup,
592-
testTeardown: teardown,
594+
testSetup: setup || noop,
595+
testTeardown: teardown || noop,
593596
formatResult: formatResult || baseFormatResult,
594597
fixtureOutputBasename,
595598
code,
@@ -663,9 +666,9 @@ export function pluginTester(options: PluginTesterOptions = {}) {
663666
execFixture
664667
} = mergeWith(
665668
{
666-
setup: () => undefined,
667-
teardown: () => undefined
668-
},
669+
setup: noop,
670+
teardown: noop
671+
} as object,
669672
testObject,
670673
mergeCustomizer
671674
);
@@ -717,8 +720,8 @@ export function pluginTester(options: PluginTesterOptions = {}) {
717720
only,
718721
skip,
719722
expectedError: throws ?? error,
720-
testSetup: setup,
721-
testTeardown: teardown,
723+
testSetup: setup || noop,
724+
testTeardown: teardown || noop,
722725
formatResult: formatResult || baseFormatResult,
723726
// ? trimAndFixLineEndings is called later on the babel output instead
724727
code,
@@ -799,13 +802,13 @@ export function pluginTester(options: PluginTesterOptions = {}) {
799802
const setupFunctions = [baseSetup, testSetup];
800803
const teardownFunctions = [testTeardown, baseTeardown];
801804

802-
for (const setupFn of setupFunctions) {
805+
for (const [index, setupFn] of setupFunctions.entries()) {
803806
try {
804807
// eslint-disable-next-line no-await-in-loop
805808
const maybeTeardownFn = await setupFn();
806809

807810
if (typeof maybeTeardownFn === 'function') {
808-
teardownFunctions.unshift(maybeTeardownFn);
811+
teardownFunctions.splice(index - 1, 0, maybeTeardownFn);
809812
}
810813
} catch (error) {
811814
throw new Error(

0 commit comments

Comments
 (0)
Please sign in to comment.