|
| 1 | +--- |
| 2 | +title: Jest 22: Refinements & Custom Runners |
| 3 | +author: Simen Bekkhus |
| 4 | +authorURL: https://github.com/SimenB |
| 5 | +authorFBID: 100003004880942 |
| 6 | +--- |
| 7 | + |
| 8 | +Today we are announcing a new major version of Jest which refines almost all |
| 9 | +parts of Jest to provide a more solid testing foundation. Together with the Jest |
| 10 | +community we made a number of changes across the board that will help you get |
| 11 | +more out of Jest. We are also graduating the custom runners feature out of the |
| 12 | +experimental stage and added a new package, `jest-worker`, for parallelizing |
| 13 | +work across multiple processes. We have compiled a list of highlights below but |
| 14 | +make sure to check out the (as always) |
| 15 | +[massive changelog](https://github.com/facebook/jest/blob/master/CHANGELOG.md). |
| 16 | + |
| 17 | +## Good bye Node 4 & welcome JSDOM 11 |
| 18 | + |
| 19 | +With this release we are dropping support for Node 4, mainly because one of our |
| 20 | +main dependencies, JSDOM, ended their support. Jest now comes out of the box |
| 21 | +with JSDOM 11 which features better support for SVGs, `requestAnimationFrame`, |
| 22 | +`URL` and `URLSearchParams` built in, and |
| 23 | +[much more](https://github.com/tmpvar/jsdom/blob/master/Changelog.md). |
| 24 | + |
| 25 | +## Custom Runners + Easy parallelization with `jest-worker` |
| 26 | + |
| 27 | +In Jest 21 we introduced the concept of custom Jest runners. Since then, |
| 28 | +Rogelio, one of Jest's core contributors, built a number of useful runners: If |
| 29 | +you have many existing tests written using another framework but you'd like to |
| 30 | +immediately benefit from Jest's features, check out |
| 31 | +[jest-runner-mocha](https://yarnpkg.com/en/package/jest-runner-mocha). If you |
| 32 | +have a large codebase that needs linting, |
| 33 | +[you may get a significant speedup](https://twitter.com/lencioni/status/907398856756695040) |
| 34 | +if you run eslint within Jest using |
| 35 | +[jest-runner-eslint](https://yarnpkg.com/en/package/jest-runner-eslint). |
| 36 | + |
| 37 | +To gain more of fine-grained control over heavy tasks parallelization (e.g. |
| 38 | +transforming files or crawling the file system), we designed a new library |
| 39 | +perfectly suited for the job. We developed a modern, Promise-based module with |
| 40 | +an approachable API, called `jest-worker`, that allows you to delegate to child |
| 41 | +processes those intensive functions. As `jest-worker`, like any other Jest |
| 42 | +package, is a part of the Jest platform, you can use it however you like even |
| 43 | +without ever using Jest itself. You'll find more in the documentation |
| 44 | +[here](https://yarnpkg.com/en/package/jest-worker). |
| 45 | + |
| 46 | +To get a better understanding of custom runners and Jest as a platform, make |
| 47 | +sure to check out Rogelio's talk from Reactive Conf 2017: |
| 48 | +[Jest as a Platform](https://www.youtube.com/watch?v=NtjyeojAOBs). |
| 49 | + |
| 50 | +## Codeframe in test failures |
| 51 | + |
| 52 | +In order to more easily identify which assertion is failing your test, we've |
| 53 | +added a code frame showing the context where the assertion is in order to focus |
| 54 | +on your own code. See the following example test: |
| 55 | + |
| 56 | +``` |
| 57 | +test('some test', () => { |
| 58 | + function someFunctionWhichShouldThrow() { |
| 59 | + if (false) { |
| 60 | + throw new Error(); |
| 61 | + } |
| 62 | +
|
| 63 | + return 'success!'; |
| 64 | + } |
| 65 | +
|
| 66 | + expect(someFunctionWhichShouldThrow).toThrowError(); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +In Jest 21, we would display the following error: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +In Jest 22, we have added a codeframe, giving more context to the failing |
| 75 | +assertions. We have also cleaned up the stack trace to remove more clutter than |
| 76 | +ever. |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## Easier testing of errors thrown in async code |
| 81 | + |
| 82 | +You can now use `toThrow` and `toThrowErrorMatchingSnapshot` on promise |
| 83 | +rejections in the same way you can on synchronous errors. |
| 84 | + |
| 85 | +``` |
| 86 | +async function throwingFunction() { |
| 87 | + throw new Error('This failed'); |
| 88 | +} |
| 89 | +
|
| 90 | +test('asynchronous rejection', async () => { |
| 91 | + await expect(throwingFunction()).rejects.toThrowErrorMatchingSnapshot(); |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +## Asynchronous test environments |
| 96 | + |
| 97 | +When [Puppeteer](https://github.com/GoogleChrome/puppeteer/), a way of |
| 98 | +programmatically interacting with a real Chromium Browser, was announced by the |
| 99 | +Google Chrome team in August, many wanted to be able to use Jest to write their |
| 100 | +tests running them in Chrome. The community have helped us out making this |
| 101 | +possible by allowing asynchronous test environments. We are still working on |
| 102 | +making this experience as good as possible, but please see |
| 103 | +[this guide](http://facebook.github.io/jest/docs/en/puppeteer.html) for how to |
| 104 | +use Puppeteer with Jest, starting today. |
| 105 | + |
| 106 | +## Experimental Leak Detection |
| 107 | + |
| 108 | +We added an experimental `--detectLeaks` setting to Jest that will let you know |
| 109 | +if your internal environment instance is leaked after a test execution. It will |
| 110 | +also warn you when your test suite tries to require a file after the test has |
| 111 | +finished, meaning you forgot to wait for all async operations or left something |
| 112 | +not properly cleaned. This will however not discover leaks in user land code, |
| 113 | +only in test land code; although the technology used behind it can help you (see |
| 114 | +`jest-leak-detector`). If you are reporting a bug about Jest's memory usage, |
| 115 | +please provide a repro where `--detectLeaks` will make the test suite fail. We |
| 116 | +[started building a better sandboxing mechanism](https://github.com/facebook/jest/pull/4970) |
| 117 | +for Jest but it's not ready yet to be enabled by default. If you'd like to help, |
| 118 | +please reach out to us on discord! |
| 119 | + |
| 120 | +## Watch Mode Refinements |
| 121 | + |
| 122 | +When using watch mode, there is now a way to |
| 123 | +[focus only on tests that previously failed](https://github.com/facebook/jest/pull/4886). |
| 124 | +In this mode, Jest will not re-run previously passing tests which should help |
| 125 | +you iron out all test failures. Additionally, |
| 126 | +[we added a plugin system to watch mode](https://github.com/facebook/jest/pull/4841). |
| 127 | +By adding modules to `watchPlugins` in your configuration you can extend the |
| 128 | +features of the watch mode. |
| 129 | + |
| 130 | +## Babel 7 support |
| 131 | + |
| 132 | +Jest uses Babel under the hood to power Code coverage and advanced mocking |
| 133 | +features. With Jest 22, it also supports the upcoming Babel 7. You'll find more |
| 134 | +in the documentation |
| 135 | +[here](http://facebook.github.io/jest/docs/en/getting-started.html#using-babel). |
| 136 | + |
| 137 | +## Mock function improvements |
| 138 | + |
| 139 | +There has been a couple of changes to mock functions in Jest 22, making them |
| 140 | +even easier to use. Firstly, we added a |
| 141 | +`[mockName](http://facebook.github.io/jest/docs/en/mock-function-api.html#mockfnmocknamevalue)` |
| 142 | +property allowing you to name your mocks, which is useful in assertion failures. |
| 143 | +We have also made the Jest mock function serializable in `pretty-format`, |
| 144 | +meaning that you can snapshot test mocks. In Jest 21, |
| 145 | +`expect(jest.fn()).toMatchSnapshot()` would serialize to `[Function]`, in Jest |
| 146 | +22, you might get something like this: |
| 147 | + |
| 148 | +``` |
| 149 | +test('my mocking test', () => { |
| 150 | + const mock = jest.fn().mockName('myMock'); |
| 151 | +
|
| 152 | + mock('hello', { foo: 'bar' }); |
| 153 | +
|
| 154 | + expect(mock).toMatchSnapshot(); |
| 155 | +}); |
| 156 | +
|
| 157 | +// Serializes to: |
| 158 | +
|
| 159 | +[MockFunction myMock] { |
| 160 | + "calls": Array [ |
| 161 | + Array [ |
| 162 | + "hello", |
| 163 | + Object { |
| 164 | + "foo": "bar", |
| 165 | + }, |
| 166 | + ], |
| 167 | + ], |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Highlights from Jest 21 |
| 172 | + |
| 173 | +Jest 21 was released back in September, and we unfortunately never got around to |
| 174 | +write a blog post. So here is a quick summary of the main changes in version 21: |
| 175 | + |
| 176 | +1. **Use expect and jest-mock in the browser: |
| 177 | + **[Michael Jackson](https://github.com/mjackson) donated his excellent |
| 178 | + [`expect`](https://github.com/mjackson/expect) package to the Jest project. |
| 179 | + As part of that transition, the Jest core team, with much help from |
| 180 | + [Kenneth Skovhus](https://github.com/skovhus/), made both `jest-matchers` |
| 181 | + (renamed to `expect`) and `jest-mock` work in browsers. This means that while |
| 182 | + you cannot use Jest itself in browsers |
| 183 | + ([yet](https://github.com/facebook/jest/issues/848)), you can use its awesome |
| 184 | + assertions and mocks for instance as replacements for Chai and Sinon running |
| 185 | + in Mocha tests. If you are migrating from earlier `expect` to the new |
| 186 | + Jest-powered `expect`, you can use |
| 187 | + [`jest-codemods`](https://github.com/skovhus/jest-codemods/) to automate the |
| 188 | + migration. |
| 189 | +2. **MIT License:** We changed Jest's license to MIT. _Yay!_ |
| 190 | +3. **Fail test suites on async errors:** Jest used to have a bug that made it |
| 191 | + crash when errors were thrown in certain parts of async code. This was fixed |
| 192 | + by community contributors. |
| 193 | +4. **Faster startup:** With Jest 21 we fine tuned Jest's startup to be more than |
| 194 | + 50% faster. The large overhead of Jest when running it on a small and fast |
| 195 | + test was always an issue for us and now this shouldn't be a reason to hold |
| 196 | + you back from using Jest any longer. |
| 197 | + |
| 198 | +## Jest Community |
| 199 | + |
| 200 | +The community around Jest is working hard to make the testing experience even |
| 201 | +greater. These are separate projects from the main Jest project, but we want to |
| 202 | +highlight some of our personal favorites here. |
| 203 | + |
| 204 | +* [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot) |
| 205 | + – custom matcher to compare images with snapshots by American Express |
| 206 | + developers |
| 207 | +* [ts-jest](https://github.com/kulshekhar/ts-jest) – all you need to |
| 208 | + successfully run Jest within TypeScript project by |
| 209 | + [@kulshekhar](https://github.com/kulshekhar/ts-jest) |
| 210 | +* [jest-codemods](https://github.com/skovhus/jest-codemods/) – migrate your |
| 211 | + tests from other frameworks to Jest with ease |
| 212 | +* [jest-plugins](https://github.com/negativetwelve/jest-plugins) – a new |
| 213 | + community project oriented around simplifying setting up test environment for |
| 214 | + specific tools, like React, or providing some handy utilities |
| 215 | + |
| 216 | +We'd also like to announce that recently we launched a new place for high |
| 217 | +quality Jest additions – [jest-community](https://github.com/jest-community). |
| 218 | +It's a new GitHub organization already featuring our favorite projects, like |
| 219 | +[vscode-jest](https://github.com/jest-community/vscode-jest), |
| 220 | +[jest-extended](https://github.com/jest-community/jest-extended), to name a few, |
| 221 | +curated by Jest maintainers and collaborators. We've even migrated our |
| 222 | +[eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest) |
| 223 | +there, and already see some great contributions, which are published |
| 224 | +independently at a faster pace. |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | +Community projects under one organisation are also a great way for us to |
| 229 | +experiment on things like automated releases, which we'd like to explore for |
| 230 | +Jest as well. They also enable us to share some common things between them, like |
| 231 | +the shape of the README for example (thanks to the webpack Community for the |
| 232 | +idea), making it easier to learn and use for all of us. |
| 233 | + |
| 234 | +If you have something awesome to share, feel free to reach out to us! We'd love |
| 235 | +to share your project here. |
0 commit comments