-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: improve frontend tests #286
Comments
I agree. Testing is the area in where I had to level up and learn the most in my first couple of months. Especially how to mock dependencies away for pure unit tests, |
I don’t work with React at work, how do you generally go about doing testing? Do you test the components or the services? We also mock dependencies on the back end so that part shouldn’t be too hard. |
Hi Eamon and Marc, it's Aaron from Launch School. Take everything below as just my own experience and that you guys have a lot more experience being devs, but thought I'd give you some of my own experience on testing with my own App. Definitely sounds like you want to mock the api responses as it sounds like the tests are a little brittle. It's also worth considering some CI to only be able to push when specific tests/all tests pass. Its also possible to use husky for pre-commit testing and only commit when tests pass (but this can slow developer time down). There's a couple of options for mocking
Nock and MSW are mocking libraries they both work by monkey patching http requests, although MSW does intercept real network requests in the browser by service workers. This means that the app doesn't really know its a mock. Nock intercepts node's http request function and allows you to specify what gets returned, which is slightly more removed. Using a test database is truer to a real request and so captures problems on a real database. The problem being is that the testing will be slower. Personal Experience It would be worth having a discussion about what you would want to test, are you unit testing heavy + e2e/integration testing heavy ? Whats your opinion on testing props/handlers being called. Are there things you don't want to tests ? What are they ? Testing both components and features (either by e2e or an "integration" type test) seems reasonable but at a component level it can be quite easy to test implementation details. The line between testing from a user perspective and implementation details can get very blurred when testing components when I've been thinking about the component tests myself. Feature testing allows better spread of testing code so can detect more problems. Some Resources I've tried to split my testing into individual components - what would a user do and how can I test that at a component level ? So a good first step might be, what are the main user flows throughout the system ? What are the most critical things that need to be tested ? Go by each component and right down descriptions of tests at a component level that would a user would be interested in. This gives you a starting point to know what to test. You can then make some decision on actually what isn't important to test. |
The problem with our frontend tests is that the depend on the backend database being in a certain state. Currently, the tests do not pass and are skipped. Instead, we should mock the api responses and ensure everything is correctly rendered.
One example of how to do so is shown here: https://www.digitalocean.com/community/tutorials/how-to-test-a-react-app-with-jest-and-react-testing-library
The text was updated successfully, but these errors were encountered: