{"componentChunkName":"component---src-templates-lesson-js","path":"/phoenix-chat/unit-tests-with-enzyme","result":{"data":{"mdx":{"body":"function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Unit Tests with Enzyme\",\n  \"order\": 9,\n  \"free\": true,\n  \"active\": true,\n  \"tech\": [\"react\", \"enzyme\", \"mocha\"]\n};\n\nvar makeShortcode = function makeShortcode(name) {\n  return function MDXDefaultShortcode(props) {\n    console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n    return mdx(\"div\", props);\n  };\n};\n\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, [\"components\"]);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h1\", null, \"Unit Tests with Enzyme\"), mdx(\"ul\", null, mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"What is a unit test?\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Enzyme and shallow rendering\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"jsdom and DOM rendering\"), mdx(\"li\", {\n    \"parentName\": \"ul\"\n  }, \"Istanbul/nyc and test coverage\")), mdx(\"p\", null, \"React makes unit testing extremely simple. Since every component is just a function and since most of our components are pure functions, it's easy to isolate just the pieces of the component you want to test without any side effects.\"), mdx(\"p\", null, \"We are going to use \", mdx(\"a\", {\n    \"href\": \"https://github.com/airbnb/enzyme\",\n    \"parentName\": \"p\"\n  }, \"Enzyme\"), \", which is a testing utility for React that clears up some of the awkward syntax of React's \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"TestUtils\"), \".\"), mdx(\"p\", null, \"One other advantage of testing React components is that you can test them without the need to render them to a DOM using something called \", mdx(\"a\", {\n    \"href\": \"https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md\",\n    \"parentName\": \"p\"\n  }, \"shallow rendering\"), \". Shallow rendering is extremely performant and lets you run through your tests at blazing speed. This is because pulling up a DOM and rendering to it takes significant time, while simply calling a function is really fast.\"), mdx(\"h2\", null, \"What is a unit test?\"), mdx(\"p\", null, \"A unit test is the smallest possible testing unit and should only test one thing. Unit tests are different than integration tests because integration tests are in place to make sure that our pieces work together. In short, unit tests make sure our pieces work by themselves, while integration tests make sure our pieces work together.\"), mdx(\"p\", null, \"TODO need a graphic here showing the difference between a unit test and an integration test\"), mdx(\"p\", null, \"An example of a unit tests would be taking a single React component and testing to make sure it does everything we want it to do. We might want that component to render a particular element to the DOM, or trigger a specific event. Now, the tricky part about unit tests is that we wouldn't actually care whether or not that event actually did anything--all we care about was that the event was triggered.\"), mdx(\"p\", null, \"Functions (especially pure functions) are really easy to test because they will always give the same output given a certain input.\"), mdx(\"h2\", null, \"What to test?\"), mdx(\"p\", null, \"We are going to test our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component, which is just a wrapper. All we really need to tests is that a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"<div>\"), \" renders.\"), mdx(\"p\", null, \"For more complex components, you want to test to make sure that everything works the way it's supposed to given certain inputs. You don't want to rely on a database call or on anything outside the specific component that you are testing.\"), mdx(\"p\", null, \"There is also the question of whether testing is worth the time for a particular component. When it comes to unit testing in React, it's really easy to go overboard. As a rule of thumb, if the CEO is going to give a live demo, you want to make sure you have really comprehensive tests for everything he would do in that demo.\"), mdx(\"p\", null, \"It is also worth considering how you might later refactor a component, and how your tests would ensure that the component still operates the same way after the refactor. For example, in a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Button\"), \" component, you'd want to make sure that your component rendered its \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"children\"), \" and any other properties that are required for that component to render properly.\"), mdx(\"h2\", null, \"Setting up Enzyme\"), mdx(\"p\", null, \"The first thing we need to do is install \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"enzyme\"), \" and all other dependencies we will need.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save-dev enzyme mocha expect react-addons-test-utils\\n\")), mdx(\"p\", null, \"Because we are using \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"css-modules\"), \" we will need to pass in an empty object as our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"style\"), \" object and pass in a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"noop\"), \" (short for \\\"no operation\\\" and pronounced \\\"no op\\\") for images and other non-JavaScript files (thanks to \", mdx(\"a\", {\n    \"href\": \"https://ole.michelsen.dk/blog/testing-reactjs-with-coverage-using-mocha-babel-istanbul.html\",\n    \"parentName\": \"p\"\n  }, \"Ole Michelson\"), \" for solving some of this issue). Create a \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".test-setup.js\"), \" file and include the following to accomplish this goal:\"), mdx(\"p\", null, \"We also need to add \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"react-addons-test-utils\"), \" because it is a dependency of Enzyme.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch .test-setup.js\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"const noop = () => {}\\nconst empty = () => ({})\\n\\nrequire.extensions['.css'] = empty\\nrequire.extensions['.ico'] = noop\\nrequire.extensions['.png'] = noop\\nrequire.extensions['.jpg'] = noop\\nrequire.extensions['.svg'] = noop\\n\\n\")), mdx(\"p\", null, \"We are also adding \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mocha\"), \" so we can run our tests with Mocha using the command line \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mocha\"), \".\"), mdx(\"h2\", null, \"Writing a unit test\"), mdx(\"p\", null, \"At this point, our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component is pretty simple. All we are going to test is that it renders. Create a file called \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"spec.js\"), \" within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" directory and add the following.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ touch app/components/Home/spec.js\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"import React from \\\"react\\\"\\nimport expect from \\\"expect\\\"\\nimport { shallow } from \\\"enzyme\\\"\\n\\nimport { Home } from \\\"./\\\"\\n\\ndescribe(\\\"<Home />\\\", () => {\\n  it(\\\"should render\\\", () => {\\n    const renderedComponent = shallow(<Home />)\\n    expect(renderedComponent.is(\\\"div\\\")).toEqual(true)\\n  })\\n})\\n\\n\")), mdx(\"p\", null, \"We're going to use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"expect\"), \", which is a simple assertion library that replaces \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"assert\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"sinon\"), \", and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"chai\"), \" for our purposes. Almost all of our assertions are going to come down to whether a certain statement is \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"true\"), \" or \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"false\"), \". Generally speaking, if you're coming up with complex assertions you're probably over-complicating things. It also gives us access to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"spy\"), \", which we will go over later.\"), mdx(\"p\", null, \"From \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"enzyme\"), \" we get \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"shallow\"), \", which is a function that lets us shallow render our component without a DOM. The \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"shallow\"), \" API is really comprehensive and has good documentation. It would be worth your time to look over all the functions you have at your disposal \", mdx(\"a\", {\n    \"href\": \"https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md\",\n    \"parentName\": \"p\"\n  }, \"here\"), \".\"), mdx(\"p\", null, \"The next part is the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"describe\"), \" block, where we (not surprisingly) describe the thing that we are testing. Within that \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"describe\"), \" block will be a series of \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"it\"), \" blocks that actually contain the tests we intend to run. \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"it\"), \" blocks usually follow the word \\\"should\\\", and that's a good way to think about what the code in the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"it\"), \" block should do. In this case, it should render the children of our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"Home\"), \" component.\"), mdx(\"p\", null, \"The last line is where we make our assertion. If \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"renderedComponent\"), \" has been shallow rendered properly, then it will return a truthy value, which \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"expect\"), \" will pass. If the component is not rendered properly, then our test will not pass.\"), mdx(\"p\", null, \"This is the only test you'll need for most of your components. Basically, we're just testing that the component was able to render without exploding.\"), mdx(\"p\", null, \"If you find yourself testing a lot of logic within your component, you should really consider pulling that logic out into a separate part of your app (likely Redux, which we will go over later). Ideally, you want to keep your components as dumb as possible, meaning they just render content that is passed into them and don't execute any logic.\"), mdx(\"h2\", null, \"Running our tests\"), mdx(\"p\", null, \"The last piece is actually running the tests. This is going to require a relatively complicated script in our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"package.json\"), \" file.\"), mdx(\"p\", null, \"But since we are not running our tests through webpack, we need to make sure we let Babel transpile our code. We can use \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"babel-register\"), \" to transpile our code before passing it on to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mocha\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save-dev babel-register\\n\")), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"  ...\\n  \\\"scripts\\\": {\\n    ...\\n    \\\"test\\\": \\\"mocha --require babel-register --require .test-setup.js -R spec app/**/spec.js\\\"\\n  },\\n  ...\\n\")), mdx(\"p\", null, \"What we're doing here is telling \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mocha\"), \" to run our tests, but first passing it through Babel (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"--require babel-register\"), \"), then changing out our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"resolve.extensions\"), \" with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".test-setup.js\"), \" so we don't have to use webpack, then recursively looking for all \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"spec.js\"), \" files within the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"app\"), \" directory.\"), mdx(\"p\", null, \"Now you can run it.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm test\\n\")), mdx(\"p\", null, \"You should get an output like the following.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"  <Home />\\n    should render\\n\\n  1 passing (32ms)\\n\")), mdx(\"p\", null, mdx(\"strong\", {\n    \"parentName\": \"p\"\n  }, \"It's important to make your test fail in a predictable way so you know that you set your test up properly.\"), \" Sometimes without knowing it, you might set up a test that passes no matter what you give it. We're not going to do that here for the sake of brevity, but you should make this practice a habit.\"), mdx(\"h2\", null, \"Full and static rendering\"), mdx(\"p\", null, \"At a certain point, you'll need to run unit tests that require a DOM. If you want to check to make sure a certain action happened on \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"componendDidMount\"), \", for example, you'll need to fully render the component so you have access to the lifecycle methods. To accomplish this, you will need Enzyme's \", mdx(\"a\", {\n    \"href\": \"https://github.com/airbnb/enzyme/blob/master/docs/api/mount.md\",\n    \"parentName\": \"p\"\n  }, \"Full Render API\"), \", which has a syntax almost identical to the shallow render API.\"), mdx(\"p\", null, \"But before we can get this to work, we need a DOM running. The easiest and most efficient way to do this is with \", mdx(\"a\", {\n    \"href\": \"https://github.com/tmpvar/jsdom\",\n    \"parentName\": \"p\"\n  }, \"jsdom\"), \", which is a magical JavaScript implementation of the DOM that lets you run your tests extremely quickly since it's all done in JavaScript.\"), mdx(\"p\", null, \"Setting up \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsdom\"), \" is a lot easier than you might expect. Enzyme provides \", mdx(\"a\", {\n    \"href\": \"https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md\",\n    \"parentName\": \"p\"\n  }, \"an explanation\"), \" of how to set it up, and we will walk through it here.\"), mdx(\"p\", null, \"We can copy-paste the code from the documentation to the bottom of our existing \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".test-setup.js\"), \" file.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"const noop = () => {}\\nconst empty = () => ({})\\n\\nrequire.extensions['.css'] = empty\\nrequire.extensions['.ico'] = noop\\nrequire.extensions['.png'] = noop\\nrequire.extensions['.jpg'] = noop\\nrequire.extensions['.svg'] = noop\\n\\nvar jsdom = require('jsdom').jsdom\\n\\nvar exposedProperties = ['window', 'navigator', 'document']\\n\\nglobal.document = jsdom('')\\nglobal.window = document.defaultView\\nObject.keys(document.defaultView).forEach((property) => {\\n  if (typeof global[property] === 'undefined') {\\n    exposedProperties.push(property)\\n    global[property] = document.defaultView[property]\\n  }\\n})\\n\\nglobal.navigator = {\\n  userAgent: 'node.js'\\n}\\n\\n\")), mdx(\"p\", null, \"What this code is doing is taking our React code and putting it into the global namespace so it can be used by \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsdom\"), \".\"), mdx(\"p\", null, \"Then install \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"jsdom\"), \".\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save-dev jsdom\\n\")), mdx(\"p\", null, \"And that's all. Now you can run tests with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"mount\"), \" and \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"static\"), \", as well as \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"shallow\"), \" as you have before. The app is too simple to require tests of this sort, so we demonstrate the difference between these types of tests when it becomes necessary.\"), mdx(\"p\", null, mdx(\"em\", {\n    \"parentName\": \"p\"\n  }, \"Just be sure you are running a more recent version of node. You must be using at least node 4.\"), \" If you do not know what version of node you are using, run the following in your terminal.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ node -v\\n\")), mdx(\"p\", null, \"If you find that your version of node is not at least 4.0, then update it with \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"brew\"), \", \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"nvm\"), \", or whatever method you prefer.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ brew update\\n$ brew upgrade node\\n\")), mdx(\"h2\", null, \"Istanbul and test coverage\"), mdx(\"p\", null, \"Another feature that you see in nearly every production app is test coverage. Test coverage is an automated way to let you know how much of your app is covered by tests. If you have no tests, your test coverage is 0%. If you have tests for every function and for every line of code, your test coverage will be 100%. Most companies aim to have tests coverage of 80%-90%, depending on their risk tolerance.\"), mdx(\"p\", null, mdx(\"a\", {\n    \"href\": \"https://github.com/gotwarlost/istanbul\",\n    \"parentName\": \"p\"\n  }, \"Istanbul\"), \" is the most popular tool for automated test coverage and it's used by just about every serious open source project. It's also surprisingly easy to set up. Start by installing it. For whatever reason, they decided to call the Babel-compatible command line tool for Istanbul \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"nyc\"), \".\"), mdx(\"p\", null, \"TODO this needs to exclude spec.js files from test coverage\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"$ npm install --save-dev nyc\\n\")), mdx(\"p\", null, \"Then we need to change our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"package.json\"), \" scripts to run \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"nyc\"), \" over our existing tests so it can automatically detect how much of our code is covered by tests. We don't need to run this every time, so we will make it a separate command. This is going to look a little bit awkward.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"  ...\\n  \\\"scripts\\\": {\\n    ...\\n    \\\"cover\\\": \\\"nyc -x '**/*spec.js' -n 'app' -r text -r html -r lcov npm test\\\",\\n    ...\\n  }\\n  ...\\n\")), mdx(\"p\", null, \"While this command might look complicated, it's actually pretty reasonable. These commands tell \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"nyc\"), \" to exclude (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-x\"), \") all of our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"spec.js\"), \" files from our coverage report (because we don't a report on how well our tests are tested), include (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-n\"), \") everything within the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"app\"), \" directory, use the text, html, and lcov reporters (\", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"-r\"), \") and run \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm test\"), \", defined within our \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"scripts\"), \", to run the tests.\"), mdx(\"p\", null, \"So now, if you run \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"npm run cover\"), \", you will see your app with test coverage.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-bash\",\n    \"parentName\": \"pre\"\n  }, \"-----------|----------|----------|----------|----------|----------------|\\nFile       |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |\\n-----------|----------|----------|----------|----------|----------------|\\nAll files  |       90 |      100 |        0 |       90 |                |\\n Home      |      100 |      100 |      100 |      100 |                |\\n  index.js |      100 |      100 |      100 |      100 |                |\\n Sidebar   |       80 |      100 |        0 |       80 |                |\\n  index.js |       80 |      100 |        0 |       80 |              6 |\\n-----------|----------|----------|----------|----------|----------------|\\n\")), mdx(\"p\", null, \"You should also add the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"coverage\"), \" directory to \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \".gitignore\"), \" so it doesn't get pushed to your repo.\"), mdx(\"pre\", null, mdx(\"code\", {\n    \"className\": \"language-js\",\n    \"parentName\": \"pre\"\n  }, \"node_modules\\n.DS_Store\\nnpm-debug.log\\ndist\\ncoverage\\n.nyc_output\\n\")), mdx(\"p\", null, \"If you're looking for something prettier than the terminal output, you can use your browser to open the \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"index.html\"), \" file within \", mdx(\"inlineCode\", {\n    \"parentName\": \"p\"\n  }, \"coverage/lcov-report\"), \". This will also give you hints as to where you should write additional tests to improve your test coverage.\"), mdx(\"h2\", null, \"Additional\"), mdx(\"p\", null, \"We will write additional tests as we go along. Testing is an important part of ensuring the long-term stability of your app. Refactoring is inevitable, and if you have good tests, you can ensure that you don't break everything when you change around your components.\"));\n}\n;\nMDXContent.isMDXComponent = true;","excerpt":"Unit Tests with Enzyme What is a unit test? Enzyme and shallow rendering jsdom and DOM rendering Istanbul/nyc and test coverage React makes…","frontmatter":{"title":"Unit Tests with Enzyme","order":9,"active":true,"free":true,"tech":["react","enzyme","mocha"]}}},"pageContext":{"slug":"unit-tests-with-enzyme","prevNode":{"frontmatter":{"title":"Snippets and Aliases","active":true,"free":true,"order":8,"tech":["atom","vim"]},"fields":{"slug":"snippets-and-aliases"}},"nextNode":{"frontmatter":{"title":"Basics of Elixir and Functional Programming","active":true,"free":true,"order":10,"tech":["phoenix","elixir"]},"fields":{"slug":"basics-of-elixir-and-functional-programming"}}}},"staticQueryHashes":["1723017916"]}