index
Learn how to use integration tests to make sure the different parts of your application work together correctly.
Integration tests check that whole features of your code work correctly. This usually involves checking several units of code at once.
Testing application logic
Usually some of your code is devoted to "application logic". This is where you coordinate several other bits of code, possibly with branching logic depending on some conditions. Imagine we were building a calculator:
We could individually unit test all the small maths functions, but that would be a lot of work. Instead we can write tests for the calculate function. If we verify that gives the right answer then we know the smaller functions must work. We're also testing how our application integrates those small units together. If we only unit tested our maths functions we could miss that our app was still totally broken (e.g. if there was a mistake in our switch).
Open workshop/index.js in your editor and read the calculate function
Open workshop/index.test.js and write tests for the calculate function.
Testing UI logic
Integration tests can also check where our code integrates with things outside our control. For example web apps often have to update the DOM to show results to the user. We didn't write the DOM code (that's part of the browser), but we still need to make sure our code integrates with it correctly.
We can write our tests to simulate a real user browsing the site. For example here is a form that takes user input and makes it uppercase:
Imagine we wanted to check that our code worked correctly. We would open the page in our browser, then follow these steps:
Change the input's value to "test"
We can write an automated test using JS that does exactly the same thing.
Open workshop/index.html in your editor. You should see a basic calculator form.
Add a test to workshop/index.test.js that checks the form works correctly, just like the example above.