Testing
Testing
Testing is an important part of the work as a developer. We want to make sure that the code we write works the way we expect it to work. And that this code will keep working that way in the future.
We use several tools for testing, but the most notable libraries are:
Jest
Testing Library (React)
We also use a few other tools for mocking and stubbing, like Nock.
What do test?
We mainly test two things:
The way our React-components work (with the help of Jest and Testing Library)
Utility functions and helpers (Jest)
Please keep in mind that we never make requests to the outside world when testing. That means you will have to mock any HTTP-calls (for which you can use Knock). By using knock you can create a fake-response in the shape of a real-life response. That fake-response will then get returned every time you attempt to make an HTTP-request to a certain endpoint.
Where do we put our tests?
We often put our UI-tests (with Testing Library) in a folder named __tests__ in the root of the package that we are working on. It can pay off to put the utility-tests in the same folder as the file that contains the utility. So:
utility.js
utility.test.js
some-tools.js
some-tools.test.js
How to write tests
Tests consist of a describe-block (possibly nested) and it-blocks. Inside the it-blocks we put our assertions using the expect-function.
describe("WHAT", () => {
describe("WHEN", () => {
it("WHAT DO WE EXPECT", () => {});
});
});
// Example:
describe("formatEuro", () => {
beforeAll(() => {
// do something here before all tests run
});
beforeEach(() => {
// do something here before each test
});
describe("when passing a number", () => {
it("returns a formatted amount in euros", () => {
expect(formatEuro(100)).toEqual("€ 100,00")
});
});
describe("when passing a string ", () => {
describe("and the string resembles a number", () => {
it("returns a formatted amount in euros", () => {
expect(formatEuro("100")).toEqual("€ 100,00")
});
})
});
describe("when passing it nothing", () => {
it("throws an error", () => {
expect(() => formatEuro(undefined)).toThrowError();
});
});
});
Last updated
Was this helpful?