Skip to content

Commit

Permalink
feat(formatResult): add formatResult option (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Jul 18, 2017
1 parent 10d2b4f commit 6085c16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ If you set up some state, it's quite possible you want to tear it down. You can
either define this as its own property, or you can return it from the `setup`
function. This can likewise return a promise if it's asynchronous.

#### formatResult

This is a function and if it's specified, it allows you to format the result
however you like. The use case for this originally was for testing codemods
and formatting their result with `prettier-eslint`.

## Examples

### Full Example + Docs
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ test('error logged and thrown if teardown throws', async () => {
)
})

test('allows formatting the result', async () => {
const formatResultSpy = jest.fn(r => r)
await pluginTester(
getOptions({
tests: [{code: simpleTest, formatResult: formatResultSpy}],
}),
)
expect(formatResultSpy).toHaveBeenCalledTimes(1)
expect(formatResultSpy).toHaveBeenCalledWith(simpleTest)
})

function getOptions(overrides) {
return {
pluginName: 'captains-log',
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function pluginTester(
error,
setup = noop,
teardown,
formatResult = r => r,
} = merge(
{},
testerConfig,
Expand Down Expand Up @@ -141,7 +142,9 @@ function pluginTester(
let errored = false

try {
result = babel.transform(code, babelOptions).code.trim()
result = formatResult(
babel.transform(code, babelOptions).code.trim(),
)
} catch (err) {
if (error) {
errored = true
Expand Down

0 comments on commit 6085c16

Please sign in to comment.