Skip to content

Commit

Permalink
feat: add ability to pass plugin options (#13)
Browse files Browse the repository at this point in the history
* feat($transform): Added ability to pass plugin options

Provides the ability to pass plugin options at the tester and test level.

* chore(eslint): bump max-lines

Because 500 is meh for tests.
  • Loading branch information
kwelch authored and Kent C. Dodds committed Jun 12, 2017
1 parent b9828b1 commit d7aa18a
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
"contributions": [
"bug"
]
},
{
"login": "kwelch",
"name": "Kyle Welch",
"avatar_url": "https://avatars0.githubusercontent.com/u/1295580?v=3",
"profile": "http://www.krwelch.com",
"contributions": [
"code",
"doc",
"test"
]
}
]
}
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Utilities for testing babel plugins
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -95,6 +95,11 @@ This is used for the `describe` title as well as the test titles. If it
can be inferred from the `plugin`'s `name` then it will be and you don't need
to provide this option.

#### pluginOptions

This can be used to pass options into your plugin at transform time. This option
can be overwritten using the test object.

#### title

This can be used to specify a title for the describe block (rather than using
Expand Down Expand Up @@ -247,6 +252,11 @@ pluginTester({
// defaults to the plugin name
title: 'describe block title',

// used to test specific plugin options
pluginOptions: {
optionA: true,
},

// only necessary if you use fixture or outputFixture in your tests
filename: __filename,

Expand Down Expand Up @@ -316,6 +326,14 @@ pluginTester({
// easier to understand.
snapshot: true,
},
{
code: 'var hello = "hi";',
output: 'var olleh = "hi";',
// this can be used to overwrite the setting set above
pluginOptions: {
optionA: false,
},
},
],
})
```
Expand Down Expand Up @@ -362,8 +380,8 @@ here!
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<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://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") |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"kentcdodds/prettier"
],
"rules": {
"func-style": "off"
"func-style": "off",
"max-lines": ["error", 1000]
}
},
"lint-staged": {
Expand Down
51 changes: 51 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,57 @@ test('can provide a test filename for code strings', () => {
)
})

test('can provide plugin options', () => {
const tests = [simpleTest]
const pluginOptions = {
optionA: true,
}
pluginTester(getOptions({tests, pluginOptions}))
expect(transformSpy).toHaveBeenCalledTimes(1)
expect(transformSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
plugins: expect.arrayContaining([
[
expect.any(Function),
expect.objectContaining({
optionA: true,
}),
],
]),
}),
)
})

test('can overwrite plugin options at test level', () => {
const pluginOptions = {
optionA: false,
}
const tests = [{code: simpleTest, pluginOptions}]
pluginTester(
getOptions({
tests,
pluginOptions: {
optionA: true,
},
}),
)
expect(transformSpy).toHaveBeenCalledTimes(1)
expect(transformSpy).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
plugins: expect.arrayContaining([
[
expect.any(Function),
expect.objectContaining({
optionA: false,
}),
],
]),
}),
)
})

test('throws invariant if snapshot and output are both provided', () => {
const tests = [{code: simpleTest, output: 'anything', snapshot: true}]
expect(() =>
Expand Down
26 changes: 22 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function pluginTester(
plugin = requiredParam('plugin'),
pluginName = getPluginName(plugin),
title: describeBlockTitle = pluginName,
pluginOptions,
tests,
fixtures,
filename,
Expand All @@ -33,6 +34,7 @@ function pluginTester(
testFixtures({
plugin,
pluginName,
pluginOptions,
title: describeBlockTitle,
fixtures,
filename,
Expand Down Expand Up @@ -62,7 +64,14 @@ function pluginTester(
} = merge(
{},
testerConfig,
toTestConfig({testConfig, index, plugin, pluginName, filename}),
toTestConfig({
testConfig,
index,
plugin,
pluginName,
pluginOptions,
filename,
}),
)
assert(
(!skip && !only) || skip !== only,
Expand Down Expand Up @@ -146,6 +155,7 @@ function pluginTester(

function testFixtures({
plugin,
pluginOptions,
title: describeBlockTitle,
fixtures,
filename,
Expand All @@ -164,7 +174,7 @@ function testFixtures({
fullDefaultConfig,
{
babelOptions: {
plugins: [plugin],
plugins: [[plugin, pluginOptions]],
// if they have a babelrc, then we'll let them use that
// otherwise, we'll just use our simple config
babelrc: pathExists.sync(babelRcPath),
Expand Down Expand Up @@ -204,7 +214,14 @@ function toTestArray(tests) {
}, [])
}

function toTestConfig({testConfig, index, plugin, pluginName, filename}) {
function toTestConfig({
testConfig,
index,
plugin,
pluginName,
pluginOptions,
filename,
}) {
if (typeof testConfig === 'string') {
testConfig = {code: testConfig}
}
Expand All @@ -214,14 +231,15 @@ function toTestConfig({testConfig, index, plugin, pluginName, filename}) {
code = getCode(filename, fixture),
fullTitle = `${index + 1}. ${title || pluginName}`,
output = getCode(filename, testConfig.outputFixture),
pluginOptions: testOptions = pluginOptions,
} = testConfig
return merge(
{
babelOptions: {filename: getPath(filename, fixture)},
},
testConfig,
{
babelOptions: {plugins: [plugin]},
babelOptions: {plugins: [[plugin, testOptions]]},
title: fullTitle,
code: stripIndent(code).trim(),
output: stripIndent(output).trim(),
Expand Down

0 comments on commit d7aa18a

Please sign in to comment.