Skip to content

Commit

Permalink
feat(test-numbers): only use numbers for tests without titles (#19)
Browse files Browse the repository at this point in the history
This was super annoying. Any time I added/moved/removed a test, all the
snapshots would need updating because the title for every one of them
changed. Pretty annoying. So now we only use the number for untitled
tests and we only increment it for the untitled tests.

BREAKING CHANGE: Your snapshots will break (for the better). Just update them. Nothing else changed.
  • Loading branch information
Kent C. Dodds committed Aug 16, 2017
1 parent 6085c16 commit 6cd36d1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 49 deletions.
6 changes: 3 additions & 3 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test('calls describe and test for a group of tests', async () => {
expect(itSpy.mock.calls).toEqual([
[`1. ${pluginName}`, expect.any(Function)],
[`2. ${pluginName}`, expect.any(Function)],
[`3. ${customTitle}`, expect.any(Function)],
[`${customTitle}`, expect.any(Function)],
])
})

Expand Down Expand Up @@ -410,8 +410,8 @@ test('can provide an object for tests', async () => {
[simpleTest, simpleTest, expect.any(String)],
])
expect(itSpy.mock.calls).toEqual([
[`1. ${firstTitle}`, expect.any(Function)],
[`2. ${secondTitle}`, expect.any(Function)],
[firstTitle, expect.any(Function)],
[secondTitle, expect.any(Function)],
])
})

Expand Down
75 changes: 29 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function pluginTester(
...rest
} = {},
) {
let testNumber = 1
if (fixtures) {
testFixtures({
plugin,
Expand All @@ -50,7 +51,7 @@ function pluginTester(
const testerConfig = merge({}, fullDefaultConfig, rest)

return describe(describeBlockTitle, () => {
const promises = testAsArray.map((testConfig, index) => {
const promises = testAsArray.map(testConfig => {
if (!testConfig) {
return Promise.resolve()
}
Expand All @@ -66,18 +67,7 @@ function pluginTester(
setup = noop,
teardown,
formatResult = r => r,
} = merge(
{},
testerConfig,
toTestConfig({
testConfig,
index,
plugin,
pluginName,
pluginOptions,
filename,
}),
)
} = merge({}, testerConfig, toTestConfig(testConfig))
assert(
(!skip && !only) || skip !== only,
'Cannot enable both skip and only on a test',
Expand Down Expand Up @@ -187,6 +177,32 @@ function pluginTester(

return Promise.all(promises)
})

function toTestConfig(testConfig) {
if (typeof testConfig === 'string') {
testConfig = {code: testConfig}
}
const {
title,
fixture,
code = getCode(filename, fixture),
fullTitle = title || `${testNumber++}. ${pluginName}`,
output = getCode(filename, testConfig.outputFixture),
pluginOptions: testOptions = pluginOptions,
} = testConfig
return merge(
{
babelOptions: {filename: getPath(filename, fixture)},
},
testConfig,
{
babelOptions: {plugins: [[plugin, testOptions]]},
title: fullTitle,
code: stripIndent(code).trim(),
output: stripIndent(output).trim(),
},
)
}
}

function testFixtures({
Expand Down Expand Up @@ -250,39 +266,6 @@ function toTestArray(tests) {
}, [])
}

function toTestConfig({
testConfig,
index,
plugin,
pluginName,
pluginOptions,
filename,
}) {
if (typeof testConfig === 'string') {
testConfig = {code: testConfig}
}
const {
title,
fixture,
code = getCode(filename, fixture),
fullTitle = `${index + 1}. ${title || pluginName}`,
output = getCode(filename, testConfig.outputFixture),
pluginOptions: testOptions = pluginOptions,
} = testConfig
return merge(
{
babelOptions: {filename: getPath(filename, fixture)},
},
testConfig,
{
babelOptions: {plugins: [[plugin, testOptions]]},
title: fullTitle,
code: stripIndent(code).trim(),
output: stripIndent(output).trim(),
},
)
}

function getCode(filename, fixture) {
if (!fixture) {
return ''
Expand Down

0 comments on commit 6cd36d1

Please sign in to comment.