Skip to content

Commit

Permalink
feat(tests): add setup/teardown functions (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds committed Jun 30, 2017
1 parent d7aa18a commit 9b30ca3
Show file tree
Hide file tree
Showing 7 changed files with 585 additions and 419 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"presets": [
["env", {
"targets": {
"node": 4.5
"node": "4.5"
}
}],
"stage-2"
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,20 @@ of the following:
}
```

#### setup

If you need something set up before a particular test is run, you can do this
with `setup`. This function will be run before the test runs. It can return
a function which will be treated as a `teardown` function. It can also return
a promise. If that promise resolves to a function, that will be treated as a
`teardown` function.

#### teardown

If you set up some state, it's quite possible you want to tear it down. You can
either define this as its own property, or you can return it from the `setup`
function. This can likewise return a promise if it's asynchronous.

## Examples

### Full Example + Docs
Expand All @@ -242,6 +256,9 @@ of the following:
import pluginTester from 'babel-plugin-tester'
import identifierReversePlugin from '../identifier-reverse-plugin'

// NOTE: you can use beforeAll, afterAll, beforeEach, and afterEach
// right here if you need

pluginTester({
// required
plugin: identifierReversePlugin,
Expand Down Expand Up @@ -334,6 +351,20 @@ pluginTester({
optionA: false,
},
},
{
title: 'unchanged code',
setup() {
// runs before this test
return function teardown() {
// runs after this tests
}
// can also return a promise
},
teardown() {
// runs after this test
// can return a promise
},
},
],
})
```
Expand Down
25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"author": "Kent C. Dodds <kent@doddsfamily.us> (http://kentcdodds.com/)",
"license": "MIT",
"dependencies": {
"babel-core": "^6.24.1",
"babel-core": "^6.25.0",
"common-tags": "^1.4.0",
"invariant": "^2.2.2",
"lodash.merge": "^4.6.0",
Expand All @@ -26,24 +26,24 @@
"devDependencies": {
"all-contributors-cli": "^4.0.1",
"babel-cli": "^6.23.0",
"babel-jest": "^20.0.1",
"babel-preset-env": "^1.2.0",
"babel-jest": "^20.0.3",
"babel-preset-env": "^1.5.2",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.23.0",
"codecov": "^2.1.0",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.0.0",
"eslint": "^3.17.0",
"eslint-config-kentcdodds": "^12.0.0",
"husky": "^0.13.2",
"jest-cli": "^20.0.1",
"lint-staged": "^3.3.1",
"nps": "^5.0.3",
"eslint-config-kentcdodds": "^12.4.1",
"husky": "^0.14.1",
"jest-cli": "^20.0.4",
"lint-staged": "^4.0.0",
"nps": "^5.3.2",
"nps-utils": "^1.1.2",
"opt-cli": "^1.5.1",
"prettier-eslint-cli": "^3.1.2",
"prettier-eslint-cli": "^4.1.1",
"semantic-release": "^6.3.6",
"validate-commit-msg": "^2.11.1"
"validate-commit-msg": "^2.12.2"
},
"eslintConfig": {
"extends": [
Expand All @@ -53,7 +53,10 @@
],
"rules": {
"func-style": "off",
"max-lines": ["error", 1000]
"max-lines": [
"error",
1000
]
}
},
"lint-staged": {
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exports[`throws error when function doesn't return true 1`] = `"test message"`;

exports[`throws if output is incorrect 1`] = `"Output is incorrect."`;

exports[`throws invariant if code is unchanged and snapshot is enabled 1`] = `"Code was unmodified but attempted to take a snapshot. If the code should not be modified, set \`snapshot: false\`"`;
exports[`throws invariant if code is unchanged + snapshot enabled 1`] = `"Code was unmodified but attempted to take a snapshot. If the code should not be modified, set \`snapshot: false\`"`;

exports[`throws invariant if snapshot and output are both provided 1`] = `"\`output\` cannot be provided with \`snapshot: true\`"`;

Expand Down

0 comments on commit 9b30ca3

Please sign in to comment.