Skip to content

Commit b63e8e1

Browse files
buschtoensKent C. Dodds
authored andcommitted
feat: concat arrays in babelOptions (#43)
1 parent 556ca0d commit b63e8e1

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pluginTester({
332332
// only necessary if you use fixture or outputFixture in your tests
333333
filename: __filename,
334334

335-
// these will be `lodash.merge`d with the test objects
335+
// these will be `lodash.mergeWith`d with the test objects
336336
// below are the defaults:
337337
babelOptions: {
338338
parserOpts: {},
@@ -469,6 +469,7 @@ Thanks goes to these people ([emoji key][emojis]):
469469
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;" alt="Kent C. Dodds"/><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;" alt="james kyle"/><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;" alt="Brad Bohen"/><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;" alt="Kyle Welch"/><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;" alt="kontrollanten"/><br /><sub><b>kontrollanten</b></sub>](https://github.com/kontrollanten)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kontrollanten "Code") | [<img src="https://avatars3.githubusercontent.com/u/117921?v=4" width="100px;" alt="Rubén Norte"/><br /><sub><b>Rubén Norte</b></sub>](https://github.com/rubennorte)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=rubennorte "Code") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=rubennorte "Tests") | [<img src="https://avatars2.githubusercontent.com/u/3869532?v=4" width="100px;" alt="André Neves"/><br /><sub><b>André Neves</b></sub>](http://andreneves.work)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=andrefgneves "Code") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=andrefgneves "Tests") |
470470
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
471471
| [<img src="https://avatars0.githubusercontent.com/u/3842800?v=4" width="100px;" alt="Kristoffer K."/><br /><sub><b>Kristoffer K.</b></sub>](https://github.com/merceyz)<br />[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=merceyz "Code") |
472+
472473
<!-- ALL-CONTRIBUTORS-LIST:END -->
473474

474475
This project follows the [all-contributors][all-contributors] specification.
@@ -510,7 +511,7 @@ MIT
510511
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/babel-utils/babel-plugin-tester.svg?style=social
511512
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
512513
[all-contributors]: https://github.com/kentcdodds/all-contributors
513-
[lodash.merge]: https://lodash.com/docs/4.17.4#merge
514+
[lodash.mergewith]: https://lodash.com/docs/4.17.4#mergeWith
514515
[ruletester]: http://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests
515516
[@thejameskyle]: https://github.com/thejameskyle
516517
[jamestweet]: https://twitter.com/thejameskyle/status/864359438819262465

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"dependencies": {
2626
"common-tags": "^1.4.0",
2727
"invariant": "^2.2.2",
28-
"lodash.merge": "^4.6.0",
28+
"lodash.mergewith": "^4.6.0",
2929
"path-exists": "^3.0.0",
3030
"strip-indent": "^2.0.0"
3131
},

src/__tests__/index.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,52 @@ test('gets options from options.json files when using fixtures', async () => {
650650
expect(optionBar).toHaveBeenCalledTimes(1)
651651
})
652652

653+
test('appends to root plugins array', async () => {
654+
const optionRootFoo = jest.fn()
655+
const optionFoo = jest.fn()
656+
const optionBar = jest.fn()
657+
const pluginWithOptions = jest.fn(() => {
658+
return {
659+
visitor: {
660+
Program(programPath, state) {
661+
if (state.opts.rootFoo === 'rootBar') {
662+
optionRootFoo()
663+
}
664+
if (state.opts.foo === 'bar') {
665+
optionFoo()
666+
}
667+
if (state.opts.bar === 'baz') {
668+
optionBar()
669+
}
670+
},
671+
},
672+
}
673+
})
674+
const programVisitor = jest.fn()
675+
const otherPlugin = () => {
676+
return {
677+
visitor: {
678+
Program: programVisitor,
679+
},
680+
}
681+
}
682+
683+
await runPluginTester(
684+
getOptions({
685+
plugin: pluginWithOptions,
686+
fixtures: getFixturePath('fixtures'),
687+
babelOptions: {
688+
plugins: [otherPlugin],
689+
},
690+
}),
691+
)
692+
693+
expect(optionRootFoo).toHaveBeenCalledTimes(8)
694+
expect(optionFoo).toHaveBeenCalledTimes(2)
695+
expect(optionBar).toHaveBeenCalledTimes(1)
696+
expect(programVisitor).toHaveBeenCalledTimes(9)
697+
})
698+
653699
function getOptions(overrides) {
654700
return {
655701
pluginName: 'captains-log',

src/index.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import assert from 'assert'
44
import path from 'path'
55
import fs from 'fs'
66
import pathExists from 'path-exists'
7-
import merge from 'lodash.merge'
7+
import mergeWith from 'lodash.mergewith'
88
import invariant from 'invariant'
99
import stripIndent from 'strip-indent'
1010
import {oneLine} from 'common-tags'
@@ -21,6 +21,13 @@ const fullDefaultConfig = {
2121
},
2222
}
2323

24+
function mergeCustomizer(objValue, srcValue) {
25+
if (Array.isArray(objValue)) {
26+
return objValue.concat(srcValue)
27+
}
28+
return undefined
29+
}
30+
2431
function pluginTester({
2532
/* istanbul ignore next (TODO: write a test for this) */
2633
babel = require('@babel/core'),
@@ -52,7 +59,7 @@ function pluginTester({
5259
if (!testAsArray.length) {
5360
return
5461
}
55-
const testerConfig = merge({}, fullDefaultConfig, rest)
62+
const testerConfig = mergeWith({}, fullDefaultConfig, rest, mergeCustomizer)
5663

5764
describe(describeBlockTitle, () => {
5865
testAsArray.forEach(testConfig => {
@@ -72,7 +79,7 @@ function pluginTester({
7279
setup = noop,
7380
teardown,
7481
formatResult = r => r,
75-
} = merge({}, testerConfig, toTestConfig(testConfig))
82+
} = mergeWith({}, testerConfig, toTestConfig(testConfig), mergeCustomizer)
7683
assert(
7784
(!skip && !only) || skip !== only,
7885
'Cannot enable both skip and only on a test',
@@ -191,7 +198,7 @@ function pluginTester({
191198
output = getCode(filename, testConfig.outputFixture) || undefined,
192199
pluginOptions: testOptions = pluginOptions,
193200
} = testConfig
194-
return merge(
201+
return mergeWith(
195202
{
196203
babelOptions: {filename: getPath(filename, fixture)},
197204
},
@@ -202,6 +209,7 @@ function pluginTester({
202209
code: stripIndent(code).trim(),
203210
...(output ? {output: stripIndent(output).trim()} : {}),
204211
},
212+
mergeCustomizer,
205213
)
206214
}
207215
}
@@ -257,7 +265,7 @@ const createFixtureTests = (fixturesDir, options) => {
257265

258266
const babelRcPath = path.join(fixtureDir, '.babelrc')
259267

260-
const {babelOptions} = merge(
268+
const {babelOptions} = mergeWith(
261269
{},
262270
fullDefaultConfig,
263271
{
@@ -278,6 +286,7 @@ const createFixtureTests = (fixturesDir, options) => {
278286
},
279287
},
280288
rest,
289+
mergeCustomizer,
281290
)
282291
const actual = formatResult(
283292
babel.transformFileSync(codePath, babelOptions).code.trim(),

0 commit comments

Comments
 (0)