Skip to content

Commit

Permalink
fix: trim input and output correctly (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz authored and Kent C. Dodds committed Jun 12, 2019
1 parent b63e8e1 commit 1d22086
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,18 @@ test('allows formatting fixtures results', async () => {
expect(formatResultSpy).toHaveBeenCalledTimes(9)
})

test('works with a formatter adding a empty line', async () => {
// Simulate prettier adding an empty line at the end
const formatResultSpy = jest.fn(r => `${r.trim()}\n\n`)
await runPluginTester(
getOptions({
fixtures: getFixturePath('fixtures'),
formatResult: formatResultSpy,
}),
)
expect(formatResultSpy).toHaveBeenCalledTimes(9)
})

test('gets options from options.json files when using fixtures', async () => {
const optionRootFoo = jest.fn()
const optionFoo = jest.fn()
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function pluginTester({
let errored = false

try {
result = formatResult(babel.transform(code, babelOptions).code.trim())
result = formatResult(babel.transform(code, babelOptions).code)
} catch (err) {
if (error) {
errored = true
Expand Down Expand Up @@ -177,8 +177,8 @@ function pluginTester({
assert.equal(result, output, 'Output is incorrect.')
} else {
assert.equal(
result,
code,
result.trim(),
code.trim(),
'Expected output to not change, but it did',
)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ const createFixtureTests = (fixturesDir, options) => {
mergeCustomizer,
)
const actual = formatResult(
babel.transformFileSync(codePath, babelOptions).code.trim(),
babel.transformFileSync(codePath, babelOptions).code,
)

const outputPath = path.join(fixtureDir, `${fixtureOutputName}${ext}`)
Expand All @@ -299,11 +299,11 @@ const createFixtureTests = (fixturesDir, options) => {
return
}

const output = fs.readFileSync(outputPath, 'utf8').trim()
const output = fs.readFileSync(outputPath, 'utf8')

assert.equal(
actual,
output,
actual.trim(),
output.trim(),
`actual output does not match ${fixtureOutputName}${ext}`,
)
})
Expand Down

0 comments on commit 1d22086

Please sign in to comment.