Skip to content

Commit 89b58b5

Browse files
authoredOct 9, 2022
feat(src): add support for "only", "skip", and "title" test options in fixtures (#90)
1 parent 8c8b858 commit 89b58b5

File tree

15 files changed

+76
-4
lines changed

15 files changed

+76
-4
lines changed
 

‎src/__tests__/__snapshots__/plugin-tester.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ exports[`can fail tests in fixtures at an absolute path 1`] = `actual output doe
1010

1111
exports[`default will throw if output changes 1`] = `Expected output to not change, but it did`;
1212

13+
exports[`fixture tests cannot be both only-ed and skipped 1`] = `Cannot enable both skip and only on a test`;
14+
1315
exports[`plugin is required 1`] = `plugin is a required parameter.`;
1416

1517
exports[`snapshot option can be derived from the root config 1`] = `\`output\` cannot be provided with \`snapshot: true\``;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"title": "custom fixture test title"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"only": true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"skip": true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"skip": true, "only": true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'use strict';

‎src/__tests__/plugin-tester.js

+49
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,55 @@ test('tests cannot be both only-ed and skipped', async () => {
147147
).rejects.toThrowErrorMatchingSnapshot()
148148
})
149149

150+
test('fixture tests can be skipped', async () => {
151+
await runPluginTester(
152+
getOptions({
153+
fixtures: getFixturePath('skip-fixtures'),
154+
tests: null,
155+
}),
156+
)
157+
158+
expect(itSkipSpy).toHaveBeenCalledTimes(1)
159+
expect(itSpy).not.toHaveBeenCalled()
160+
})
161+
162+
test('fixture tests can be only-ed', async () => {
163+
await runPluginTester(
164+
getOptions({
165+
fixtures: getFixturePath('only-fixtures'),
166+
tests: null,
167+
}),
168+
)
169+
170+
expect(itOnlySpy).toHaveBeenCalledTimes(1)
171+
expect(itSpy).not.toHaveBeenCalled()
172+
})
173+
174+
test('fixture tests cannot be both only-ed and skipped', async () => {
175+
await expect(
176+
runPluginTester(
177+
getOptions({
178+
fixtures: getFixturePath('skip-only-fixtures'),
179+
tests: null,
180+
}),
181+
),
182+
).rejects.toThrowErrorMatchingSnapshot()
183+
})
184+
185+
test('fixture tests accept custom test title', async () => {
186+
await runPluginTester(
187+
getOptions({
188+
fixtures: getFixturePath('custom-title-fixtures'),
189+
tests: null,
190+
}),
191+
)
192+
193+
expect(itSpy).toHaveBeenCalledWith(
194+
'custom fixture test title',
195+
expect.any(Function),
196+
)
197+
})
198+
150199
test('default will throw if output changes', async () => {
151200
const tests = ['var hello = "hi";']
152201
const options = getOptions({plugin: identifierReversePlugin, tests})

‎src/plugin-tester.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ function pluginTester({
9191
fixture,
9292
testFilepath: testFilename = fixture || filename,
9393
} = mergeWith({}, testerConfig, toTestConfig(testConfig), mergeCustomizer)
94+
9495
assert(
9596
(!skip && !only) || skip !== only,
9697
'Cannot enable both skip and only on a test',
@@ -300,7 +301,13 @@ const createFixtureTests = (fixturesDir, options) => {
300301
return
301302
}
302303

303-
it(blockTitle, async () => {
304+
const {only, skip, title} = localFixtureOptions
305+
306+
assert(
307+
(!skip && !only) || skip !== only,
308+
'Cannot enable both skip and only on a test',
309+
)
310+
;(only ? it.only : skip ? it.skip : it)(title || blockTitle, async () => {
304311
const {
305312
plugin,
306313
pluginOptions,
@@ -311,9 +318,11 @@ const createFixtureTests = (fixturesDir, options) => {
311318
...rest
312319
} = options
313320

314-
const hasBabelrc = ['.babelrc', '.babelrc.js', '.babelrc.cjs'].some(
315-
babelrc => fs.existsSync(path.join(fixtureDir, babelrc)),
316-
)
321+
const hasBabelrc = [
322+
'.babelrc',
323+
'.babelrc.js',
324+
'.babelrc.cjs',
325+
].some(babelrc => fs.existsSync(path.join(fixtureDir, babelrc)))
317326

318327
const {babelOptions} = mergeWith(
319328
{},

0 commit comments

Comments
 (0)
Please sign in to comment.