From b63e8e13208d6724494c1bbbe9ff8aefffb412ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Buscht=C3=B6ns?= Date: Wed, 12 Jun 2019 22:35:39 +0200 Subject: [PATCH] feat: concat arrays in `babelOptions` (#43) --- README.md | 5 +++-- package.json | 2 +- src/__tests__/index.js | 46 ++++++++++++++++++++++++++++++++++++++++++ src/index.js | 19 ++++++++++++----- 4 files changed, 64 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4697148..0a6061f 100644 --- a/README.md +++ b/README.md @@ -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: {}, @@ -469,6 +469,7 @@ Thanks goes to these people ([emoji key][emojis]): | [Kent C. Dodds
Kent C. Dodds](https://kentcdodds.com)
[💻](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") | [james kyle
james kyle](http://thejameskyle.com/)
[💻](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") | [Brad Bohen
Brad Bohen](https://github.com/bbohen)
[🐛](https://github.com/babel-utils/babel-plugin-tester/issues?q=author%3Abbohen "Bug reports") | [Kyle Welch
Kyle Welch](http://www.krwelch.com)
[💻](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") | [kontrollanten
kontrollanten](https://github.com/kontrollanten)
[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=kontrollanten "Code") | [Rubén Norte
Rubén Norte](https://github.com/rubennorte)
[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=rubennorte "Code") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=rubennorte "Tests") | [André Neves
André Neves](http://andreneves.work)
[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=andrefgneves "Code") [⚠️](https://github.com/babel-utils/babel-plugin-tester/commits?author=andrefgneves "Tests") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | | [Kristoffer K.
Kristoffer K.](https://github.com/merceyz)
[💻](https://github.com/babel-utils/babel-plugin-tester/commits?author=merceyz "Code") | + This project follows the [all-contributors][all-contributors] specification. @@ -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 diff --git a/package.json b/package.json index bc93e18..33fe367 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/__tests__/index.js b/src/__tests__/index.js index a764a4f..212c74b 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -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', diff --git a/src/index.js b/src/index.js index 705a7e3..8782aea 100644 --- a/src/index.js +++ b/src/index.js @@ -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' @@ -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'), @@ -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 => { @@ -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', @@ -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)}, }, @@ -202,6 +209,7 @@ function pluginTester({ code: stripIndent(code).trim(), ...(output ? {output: stripIndent(output).trim()} : {}), }, + mergeCustomizer, ) } } @@ -257,7 +265,7 @@ const createFixtureTests = (fixturesDir, options) => { const babelRcPath = path.join(fixtureDir, '.babelrc') - const {babelOptions} = merge( + const {babelOptions} = mergeWith( {}, fullDefaultConfig, { @@ -278,6 +286,7 @@ const createFixtureTests = (fixturesDir, options) => { }, }, rest, + mergeCustomizer, ) const actual = formatResult( babel.transformFileSync(codePath, babelOptions).code.trim(),