Skip to content

Commit 0a8d279

Browse files
kontrollantenKent C. Dodds
authored andcommitted
fix(output): allow empty output (#35)
<!-- Thanks for your interest in the project. I appreciate bugs filed and PRs submitted! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: Allowing expected `output` to be an empty string. <!-- Why are these changes necessary? --> **Why**: There's cases when we just want to verify that a node gets removed. <!-- How were these changes implemented? --> **How**: By removing the default behaviour of keeping `output` to be an empty string, and instead make it `undefined` if it isn't provided. <!-- feel free to add additional comments --> Bonus commits: * Added a eslint rule to deny trailing semi colons. * Upgraded kcd-scripts to make `npm run add-contributor` to work.
1 parent 7b81836 commit 0a8d279

File tree

6 files changed

+93
-22
lines changed

6 files changed

+93
-22
lines changed

.all-contributorsrc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
"doc",
5151
"test"
5252
]
53+
},
54+
{
55+
"login": "kontrollanten",
56+
"name": "kontrollanten",
57+
"avatar_url": "https://avatars3.githubusercontent.com/u/6680299?v=4",
58+
"profile": "https://github.com/kontrollanten",
59+
"contributions": [
60+
"code"
61+
]
5362
}
54-
]
63+
],
64+
"repoType": "github"
5565
}

README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Utilities for testing babel plugins
77
[![Dependencies][dependencyci-badge]][dependencyci]
88
[![version][version-badge]][package]
99
[![downloads][downloads-badge]][npm-stat]
10-
[![MIT License][license-badge]][LICENSE]
10+
[![MIT License][license-badge]][license]
1111

12-
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
12+
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors)
1313
[![PRs Welcome][prs-badge]][prs]
1414
[![Donate][donate-badge]][donate]
1515
[![Code of Conduct][coc-badge]][coc]
@@ -30,6 +30,26 @@ This is a fairly simple abstraction to help you write tests for your babel
3030
plugin. It works with `jest` (my personal favorite) and most of it should also
3131
work with `mocha` and `jasmine`.
3232

33+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
34+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
35+
36+
- [Installation](#installation)
37+
- [Usage](#usage)
38+
- [import](#import)
39+
- [Invoke](#invoke)
40+
- [options](#options)
41+
- [Test Objects](#test-objects)
42+
- [Examples](#examples)
43+
- [Full Example + Docs](#full-example--docs)
44+
- [Simple Example](#simple-example)
45+
- [Inspiration](#inspiration)
46+
- [Other Solutions](#other-solutions)
47+
- [Contributors](#contributors)
48+
- [LICENSE](#license)
49+
50+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
51+
52+
3353
## Installation
3454

3555
This module is distributed via [npm][npm] which is bundled with [node][node] and
@@ -82,7 +102,10 @@ function identifierReversePlugin() {
82102
name: 'identifier reverse',
83103
visitor: {
84104
Identifier(idPath) {
85-
idPath.node.name = idPath.node.name.split('').reverse().join('')
105+
idPath.node.name = idPath.node.name
106+
.split('')
107+
.reverse()
108+
.join('')
86109
},
87110
},
88111
}
@@ -428,7 +451,7 @@ pluginTester({
428451

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

431-
- ESLint's [RuleTester][RuleTester]
454+
- ESLint's [RuleTester][ruletester]
432455
- [@thejameskyle][@thejameskyle]'s [tweet][jamestweet]
433456

434457
## Other Solutions
@@ -440,8 +463,10 @@ I've been thinking about this for a while. The API was inspired by:
440463
Thanks goes to these people ([emoji key][emojis]):
441464

442465
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
443-
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/952783?v=3" width="100px;"/><br /><sub>james kyle</sub>](http://thejameskyle.com/)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Documentation") [👀](#review-thejameskyle "Reviewed Pull Requests") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1894628?v=3" width="100px;"/><br /><sub>Brad Bohen</sub>](https://github.com/bbohen)<br />[🐛](https://github.com/babel-utils/babel-plugin-tester/issues?q=author%3Abbohen "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/1295580?v=3" width="100px;"/><br /><sub>Kyle Welch</sub>](http://www.krwelch.com)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Documentation") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Tests") |
444-
| :---: | :---: | :---: | :---: |
466+
<!-- prettier-ignore -->
467+
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/952783?v=3" width="100px;"/><br /><sub><b>james kyle</b></sub>](http://thejameskyle.com/)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Documentation") [👀](#review-thejameskyle "Reviewed Pull Requests") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=thejameskyle "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1894628?v=3" width="100px;"/><br /><sub><b>Brad Bohen</b></sub>](https://github.com/bbohen)<br />[🐛](https://github.com/babel-utils/babel-plugin-tester/issues?q=author%3Abbohen "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/1295580?v=3" width="100px;"/><br /><sub><b>Kyle Welch</b></sub>](http://www.krwelch.com)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Code") [📖](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Documentation") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=kwelch "Tests") | [<img src="https://avatars3.githubusercontent.com/u/6680299?v=4" width="100px;"/><br /><sub><b>kontrollanten</b></sub>](https://github.com/kontrollanten)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kontrollanten "Code") |
468+
| :---: | :---: | :---: | :---: | :---: |
469+
445470
<!-- ALL-CONTRIBUTORS-LIST:END -->
446471

447472
This project follows the [all-contributors][all-contributors] specification.
@@ -484,7 +509,7 @@ MIT
484509
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
485510
[all-contributors]: https://github.com/kentcdodds/all-contributors
486511
[lodash.merge]: https://lodash.com/docs/4.17.4#merge
487-
[RuleTester]: http://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests
512+
[ruletester]: http://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests
488513
[@thejameskyle]: https://github.com/thejameskyle
489514
[jamestweet]: https://twitter.com/thejameskyle/status/864359438819262465
490515
[@babel/helper-plugin-test-runner]: https://github.com/babel/babel/tree/master/packages/babel-helper-plugin-test-runner

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,20 @@
3636
"babel-plugin-transform-async-to-generator": "^6.24.1",
3737
"babel-plugin-transform-object-rest-spread": "^6.26.0",
3838
"babel-preset-env": "^1.5.2",
39-
"kcd-scripts": "^0.16.3"
39+
"kcd-scripts": "^0.42.1"
4040
},
4141
"peerDependencies": {
4242
"babel-core": "^6.0.0 || ^7.0.0-alpha || ^7.0.0-beta || ^7.0.0"
4343
},
4444
"eslintConfig": {
4545
"extends": "./node_modules/kcd-scripts/eslint.js",
4646
"rules": {
47-
"max-lines": 0
47+
"jest/valid-describe": 0,
48+
"max-lines": 0,
49+
"semi": [
50+
2,
51+
"never"
52+
]
4853
}
4954
},
5055
"eslintIgnore": [

src/__tests__/__snapshots__/index.js.snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`1. captains-log 1`] = `
3+
exports[`can fail tests in fixtures at an absolute path 1`] = `"actual output does not match output.js"`;
4+
5+
exports[`default will throw if output changes 1`] = `"Expected output to not change, but it did"`;
6+
7+
exports[`plugin is required 1`] = `"plugin is a required parameter."`;
8+
9+
exports[`snapshot option can be derived from the root config 1`] = `"\`output\` cannot be provided with \`snapshot: true\`"`;
10+
11+
exports[`takes a snapshot: 1. captains-log 1`] = `
412
"
513
var hi = \\"hey\\";
614
@@ -10,14 +18,6 @@ var ih = \\"hey\\";
1018
"
1119
`;
1220

13-
exports[`can fail tests in fixtures at an absolute path 1`] = `"actual output does not match output.js"`;
14-
15-
exports[`default will throw if output changes 1`] = `"Expected output to not change, but it did"`;
16-
17-
exports[`plugin is required 1`] = `"plugin is a required parameter."`;
18-
19-
exports[`snapshot option can be derived from the root config 1`] = `"\`output\` cannot be provided with \`snapshot: true\`"`;
20-
2121
exports[`tests cannot be both only-ed and skipped 1`] = `"Cannot enable both skip and only on a test"`;
2222

2323
exports[`throws an error if babelrc is true with no filename 1`] = `"babelrc set to true, but no filename specified in babelOptions"`;

src/__tests__/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ test('trims and deindents code and output', async () => {
189189
)
190190
})
191191

192+
test('accepts an empty output', async () => {
193+
const tests = [
194+
{
195+
code: `var eraseMe = 'junk'`,
196+
output: '',
197+
},
198+
]
199+
let errorResponse
200+
try {
201+
await pluginTester(
202+
getOptions({
203+
plugin: () => ({
204+
name: 'cleanup',
205+
visitor: {
206+
VariableDeclaration(p) {
207+
p.remove()
208+
},
209+
},
210+
}),
211+
tests,
212+
}),
213+
)
214+
errorResponse = false
215+
} catch (error) {
216+
errorResponse = true
217+
}
218+
219+
expect(errorResponse).toEqual(false)
220+
})
221+
192222
test('can get a code and output fixture that is an absolute path', async () => {
193223
const tests = [
194224
{

src/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const fullDefaultConfig = {
1919
},
2020
}
2121

22+
// eslint-disable-next-line max-lines-per-function
2223
function pluginTester({
2324
/* istanbul ignore next (TODO: write a test for this) */
2425
babel = require('babel-core'),
@@ -163,7 +164,7 @@ function pluginTester({
163164
expect(`\n${formattedOutput}\n`).toMatchSnapshot(title)
164165
} else if (error) {
165166
assertError(result, error)
166-
} else if (output) {
167+
} else if (typeof output === 'string') {
167168
assert.equal(result, output, 'Output is incorrect.')
168169
} else {
169170
assert.equal(
@@ -187,7 +188,7 @@ function pluginTester({
187188
fixture,
188189
code = getCode(filename, fixture),
189190
fullTitle = title || `${testNumber++}. ${pluginName}`,
190-
output = getCode(filename, testConfig.outputFixture),
191+
output = getCode(filename, testConfig.outputFixture) || undefined,
191192
pluginOptions: testOptions = pluginOptions,
192193
} = testConfig
193194
return merge(
@@ -199,7 +200,7 @@ function pluginTester({
199200
babelOptions: {plugins: [[plugin, testOptions]]},
200201
title: fullTitle,
201202
code: stripIndent(code).trim(),
202-
output: stripIndent(output).trim(),
203+
...(output ? {output: stripIndent(output).trim()} : {}),
203204
},
204205
)
205206
}

0 commit comments

Comments
 (0)