Skip to content

Commit

Permalink
feat: concat arrays in babelOptions (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
buschtoens authored and Kent C. Dodds committed Jun 12, 2019
1 parent 556ca0d commit b63e8e1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pluginTester({
// only necessary if you use fixture or outputFixture in your tests
filename: __filename,

// these will be `lodash.merge`d with the test objects
// these will be `lodash.mergeWith`d with the test objects
// below are the defaults:
babelOptions: {
parserOpts: {},
Expand Down Expand Up @@ -469,6 +469,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<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") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<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") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down Expand Up @@ -510,7 +511,7 @@ MIT
[twitter-badge]: https://img.shields.io/twitter/url/https/github.com/babel-utils/babel-plugin-tester.svg?style=social
[emojis]: https://github.com/kentcdodds/all-contributors#emoji-key
[all-contributors]: https://github.com/kentcdodds/all-contributors
[lodash.merge]: https://lodash.com/docs/4.17.4#merge
[lodash.mergewith]: https://lodash.com/docs/4.17.4#mergeWith
[ruletester]: http://eslint.org/docs/developer-guide/working-with-rules#rule-unit-tests
[@thejameskyle]: https://github.com/thejameskyle
[jamestweet]: https://twitter.com/thejameskyle/status/864359438819262465
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"common-tags": "^1.4.0",
"invariant": "^2.2.2",
"lodash.merge": "^4.6.0",
"lodash.mergewith": "^4.6.0",
"path-exists": "^3.0.0",
"strip-indent": "^2.0.0"
},
Expand Down
46 changes: 46 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,52 @@ test('gets options from options.json files when using fixtures', async () => {
expect(optionBar).toHaveBeenCalledTimes(1)
})

test('appends to root plugins array', async () => {
const optionRootFoo = jest.fn()
const optionFoo = jest.fn()
const optionBar = jest.fn()
const pluginWithOptions = jest.fn(() => {
return {
visitor: {
Program(programPath, state) {
if (state.opts.rootFoo === 'rootBar') {
optionRootFoo()
}
if (state.opts.foo === 'bar') {
optionFoo()
}
if (state.opts.bar === 'baz') {
optionBar()
}
},
},
}
})
const programVisitor = jest.fn()
const otherPlugin = () => {
return {
visitor: {
Program: programVisitor,
},
}
}

await runPluginTester(
getOptions({
plugin: pluginWithOptions,
fixtures: getFixturePath('fixtures'),
babelOptions: {
plugins: [otherPlugin],
},
}),
)

expect(optionRootFoo).toHaveBeenCalledTimes(8)
expect(optionFoo).toHaveBeenCalledTimes(2)
expect(optionBar).toHaveBeenCalledTimes(1)
expect(programVisitor).toHaveBeenCalledTimes(9)
})

function getOptions(overrides) {
return {
pluginName: 'captains-log',
Expand Down
19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from 'assert'
import path from 'path'
import fs from 'fs'
import pathExists from 'path-exists'
import merge from 'lodash.merge'
import mergeWith from 'lodash.mergewith'
import invariant from 'invariant'
import stripIndent from 'strip-indent'
import {oneLine} from 'common-tags'
Expand All @@ -21,6 +21,13 @@ const fullDefaultConfig = {
},
}

function mergeCustomizer(objValue, srcValue) {
if (Array.isArray(objValue)) {
return objValue.concat(srcValue)
}
return undefined
}

function pluginTester({
/* istanbul ignore next (TODO: write a test for this) */
babel = require('@babel/core'),
Expand Down Expand Up @@ -52,7 +59,7 @@ function pluginTester({
if (!testAsArray.length) {
return
}
const testerConfig = merge({}, fullDefaultConfig, rest)
const testerConfig = mergeWith({}, fullDefaultConfig, rest, mergeCustomizer)

describe(describeBlockTitle, () => {
testAsArray.forEach(testConfig => {
Expand All @@ -72,7 +79,7 @@ function pluginTester({
setup = noop,
teardown,
formatResult = r => r,
} = merge({}, testerConfig, toTestConfig(testConfig))
} = mergeWith({}, testerConfig, toTestConfig(testConfig), mergeCustomizer)
assert(
(!skip && !only) || skip !== only,
'Cannot enable both skip and only on a test',
Expand Down Expand Up @@ -191,7 +198,7 @@ function pluginTester({
output = getCode(filename, testConfig.outputFixture) || undefined,
pluginOptions: testOptions = pluginOptions,
} = testConfig
return merge(
return mergeWith(
{
babelOptions: {filename: getPath(filename, fixture)},
},
Expand All @@ -202,6 +209,7 @@ function pluginTester({
code: stripIndent(code).trim(),
...(output ? {output: stripIndent(output).trim()} : {}),
},
mergeCustomizer,
)
}
}
Expand Down Expand Up @@ -257,7 +265,7 @@ const createFixtureTests = (fixturesDir, options) => {

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

const {babelOptions} = merge(
const {babelOptions} = mergeWith(
{},
fullDefaultConfig,
{
Expand All @@ -278,6 +286,7 @@ const createFixtureTests = (fixturesDir, options) => {
},
},
rest,
mergeCustomizer,
)
const actual = formatResult(
babel.transformFileSync(codePath, babelOptions).code.trim(),
Expand Down

0 comments on commit b63e8e1

Please sign in to comment.