Skip to content

Commit 60e5c07

Browse files
author
Kent C. Dodds
authored
feat: format result with prettier and unstring snapshots (#55)
BREAKING CHANGE: Your snapshots will probably need to be updated with these changes. If you don't like the changes, then take a look at the README about overriding the formatResult and disabling the un-string snapshot serializer
1 parent 7b97a6f commit 60e5c07

File tree

12 files changed

+1529
-1370
lines changed

12 files changed

+1529
-1370
lines changed

README.md

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ work with `mocha` and `jasmine`.
3131
- [Examples](#examples)
3232
- [Full Example + Docs](#full-example--docs)
3333
- [Simple Example](#simple-example)
34+
- [Defaults](#defaults)
35+
- [Un-string snapshot serializer](#un-string-snapshot-serializer)
36+
- [Prettier formatter](#prettier-formatter)
3437
- [Inspiration](#inspiration)
3538
- [Other Solutions](#other-solutions)
3639
- [Contributors](#contributors)
@@ -64,9 +67,9 @@ import yourPlugin from '../your-plugin'
6467

6568
pluginTester({
6669
plugin: yourPlugin,
67-
tests: [
70+
tests: {
6871
/* your test objects */
69-
],
72+
},
7073
})
7174
```
7275

@@ -79,9 +82,9 @@ Your babel plugin. For example:
7982
```javascript
8083
pluginTester({
8184
plugin: identifierReversePlugin,
82-
tests: [
85+
tests: {
8386
/* your test objects */
84-
],
87+
},
8588
})
8689

8790
// normally you would import this from your plugin module
@@ -122,9 +125,9 @@ pluginTester({
122125
...
123126
babelOptions: require('./babel.config.js'),
124127
...
125-
tests: [
128+
tests: {
126129
/* your test objects */
127-
],
130+
},
128131
});
129132
130133
```
@@ -300,9 +303,23 @@ function. This can likewise return a promise if it's asynchronous.
300303

301304
#### formatResult
302305

303-
This is a function and if it's specified, it allows you to format the result
304-
however you like. The use case for this originally was for testing codemods and
305-
formatting their result with `prettier-eslint`.
306+
This defaults to a function which formats your code output with prettier. If you
307+
have prettier configured, then it will use your configuration. If you don't then
308+
it will be default configuration.
309+
310+
If you'd like to specify your own, then feel free to do so. Here's the API:
311+
312+
```javascript
313+
function customFormatter(code, {filename}) {
314+
// format the code
315+
return formattedCode
316+
}
317+
```
318+
319+
Learn more about the built-in formatter below.
320+
321+
The use case for this originally was for testing codemods and formatting their
322+
result with `prettier-eslint`.
306323

307324
## Examples
308325

@@ -341,7 +358,12 @@ pluginTester({
341358
babelrc: false,
342359
configFile: false,
343360
},
344-
snapshot: false, // use jest snapshots (only works with jest)
361+
362+
// use jest snapshots (only works with jest)
363+
snapshot: false,
364+
365+
// defaults to a function that formats with prettier
366+
formatResult: customFormatFunction,
345367

346368
// tests as objects
347369
tests: {
@@ -451,6 +473,42 @@ pluginTester({
451473
})
452474
```
453475

476+
## Defaults
477+
478+
### Un-string snapshot serializer
479+
480+
If you're using jest and snapshots, then the snapshot output could have a bunch
481+
of bothersome `\"` to escape quotes because when Jest serializes a string, it
482+
will wrap everything in double quotes. This isn't a huge deal, but it makes the
483+
snapshots harder to read. So we automatically add a snapshot serializer for you
484+
to remove those.
485+
486+
If you don't like that, then do this:
487+
488+
```diff
489+
- import pluginTester from 'babel-plugin-tester'
490+
+ import pluginTester from 'babel-plugin-tester/pure'
491+
```
492+
493+
### Prettier formatter
494+
495+
By default, a formatter is included which formats your results with
496+
[`prettier`](https://prettier.io). It will look for a prettier configuration
497+
relative to the file that's being tested or the current working directory. If it
498+
can't find one, then it uses the default configuration for prettier.
499+
500+
This makes your snapshots easier to read. But if you'd like to not have that,
501+
then you can either import the `pure` file (as shown above) or you can override
502+
the `formatResult` option:
503+
504+
```javascript
505+
pluginTester({
506+
// ... other options
507+
formatResult: r => r,
508+
// ... more options
509+
})
510+
```
511+
454512
## Inspiration
455513

456514
I've been thinking about this for a while. The API was inspired by:

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"validate": "kcd-scripts validate"
1616
},
1717
"files": [
18-
"dist"
18+
"dist",
19+
"pure.js"
1920
],
2021
"husky": {
2122
"hooks": {
@@ -27,6 +28,7 @@
2728
"license": "MIT",
2829
"dependencies": {
2930
"lodash.mergewith": "^4.6.2",
31+
"prettier": "^1.19.1",
3032
"strip-indent": "^3.0.0"
3133
},
3234
"devDependencies": {

pure.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./dist/plugin-tester')

src/__tests__/__snapshots__/index.js.snap

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`assert throws if code is unchanged + snapshot enabled 1`] = `Code was unmodified but attempted to take a snapshot. If the code should not be modified, set \`snapshot: false\``;
4+
5+
exports[`assert throws if snapshot and output are both provided 1`] = `\`output\` cannot be provided with \`snapshot: true\``;
6+
7+
exports[`assert throws if the plugin name is not inferable 1`] = `The \`pluginName\` must be inferable or provided.`;
8+
9+
exports[`assert throws if there's no code 1`] = `A string or object with a \`code\` or \`fixture\` property must be provided`;
10+
11+
exports[`can fail tests in fixtures at an absolute path 1`] = `actual output does not match output.js`;
12+
13+
exports[`default will throw if output changes 1`] = `Expected output to not change, but it did`;
14+
15+
exports[`plugin is required 1`] = `plugin is a required parameter.`;
16+
17+
exports[`snapshot option can be derived from the root config 1`] = `\`output\` cannot be provided with \`snapshot: true\``;
18+
19+
exports[`takes a snapshot: 1. captains-log 1`] = `
20+
21+
var hi = "hey";
22+
23+
↓ ↓ ↓ ↓ ↓ ↓
24+
25+
var ih = "hey";
26+
27+
`;
28+
29+
exports[`tests cannot be both only-ed and skipped 1`] = `Cannot enable both skip and only on a test`;
30+
31+
exports[`throws an error if babelrc is true with no filename 1`] = `babelrc set to true, but no filename specified in babelOptions`;
32+
33+
exports[`throws error if fixture provided and code changes 1`] = `Expected output to not change, but it did`;
34+
35+
exports[`throws error when error expected but no error thrown 1`] = `Expected to throw error, but it did not.`;
36+
37+
exports[`throws error when function doesn't return true 1`] = `[BABEL] unknown: test message (While processing: "base$0")`;
38+
39+
exports[`throws if output is incorrect 1`] = `Output is incorrect.`;

0 commit comments

Comments
 (0)