Skip to content

Commit 9b30ca3

Browse files
author
Kent C. Dodds
authored
feat(tests): add setup/teardown functions (#14)
1 parent d7aa18a commit 9b30ca3

File tree

7 files changed

+585
-419
lines changed

7 files changed

+585
-419
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"presets": [
33
["env", {
44
"targets": {
5-
"node": 4.5
5+
"node": "4.5"
66
}
77
}],
88
"stage-2"

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ of the following:
234234
}
235235
```
236236

237+
#### setup
238+
239+
If you need something set up before a particular test is run, you can do this
240+
with `setup`. This function will be run before the test runs. It can return
241+
a function which will be treated as a `teardown` function. It can also return
242+
a promise. If that promise resolves to a function, that will be treated as a
243+
`teardown` function.
244+
245+
#### teardown
246+
247+
If you set up some state, it's quite possible you want to tear it down. You can
248+
either define this as its own property, or you can return it from the `setup`
249+
function. This can likewise return a promise if it's asynchronous.
250+
237251
## Examples
238252

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

259+
// NOTE: you can use beforeAll, afterAll, beforeEach, and afterEach
260+
// right here if you need
261+
245262
pluginTester({
246263
// required
247264
plugin: identifierReversePlugin,
@@ -334,6 +351,20 @@ pluginTester({
334351
optionA: false,
335352
},
336353
},
354+
{
355+
title: 'unchanged code',
356+
setup() {
357+
// runs before this test
358+
return function teardown() {
359+
// runs after this tests
360+
}
361+
// can also return a promise
362+
},
363+
teardown() {
364+
// runs after this test
365+
// can return a promise
366+
},
367+
},
337368
],
338369
})
339370
```

package.json

+14-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"author": "Kent C. Dodds <[email protected]> (http://kentcdodds.com/)",
1717
"license": "MIT",
1818
"dependencies": {
19-
"babel-core": "^6.24.1",
19+
"babel-core": "^6.25.0",
2020
"common-tags": "^1.4.0",
2121
"invariant": "^2.2.2",
2222
"lodash.merge": "^4.6.0",
@@ -26,24 +26,24 @@
2626
"devDependencies": {
2727
"all-contributors-cli": "^4.0.1",
2828
"babel-cli": "^6.23.0",
29-
"babel-jest": "^20.0.1",
30-
"babel-preset-env": "^1.2.0",
29+
"babel-jest": "^20.0.3",
30+
"babel-preset-env": "^1.5.2",
3131
"babel-preset-stage-2": "^6.22.0",
3232
"babel-register": "^6.23.0",
3333
"codecov": "^2.1.0",
3434
"commitizen": "^2.9.6",
3535
"cz-conventional-changelog": "^2.0.0",
3636
"eslint": "^3.17.0",
37-
"eslint-config-kentcdodds": "^12.0.0",
38-
"husky": "^0.13.2",
39-
"jest-cli": "^20.0.1",
40-
"lint-staged": "^3.3.1",
41-
"nps": "^5.0.3",
37+
"eslint-config-kentcdodds": "^12.4.1",
38+
"husky": "^0.14.1",
39+
"jest-cli": "^20.0.4",
40+
"lint-staged": "^4.0.0",
41+
"nps": "^5.3.2",
4242
"nps-utils": "^1.1.2",
4343
"opt-cli": "^1.5.1",
44-
"prettier-eslint-cli": "^3.1.2",
44+
"prettier-eslint-cli": "^4.1.1",
4545
"semantic-release": "^6.3.6",
46-
"validate-commit-msg": "^2.11.1"
46+
"validate-commit-msg": "^2.12.2"
4747
},
4848
"eslintConfig": {
4949
"extends": [
@@ -53,7 +53,10 @@
5353
],
5454
"rules": {
5555
"func-style": "off",
56-
"max-lines": ["error", 1000]
56+
"max-lines": [
57+
"error",
58+
1000
59+
]
5760
}
5861
},
5962
"lint-staged": {

src/__tests__/__snapshots__/index.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports[`throws error when function doesn't return true 1`] = `"test message"`;
3232

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

35-
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\`"`;
35+
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\`"`;
3636

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

0 commit comments

Comments
 (0)