A Next.js app created to be able to learn and practice Unit testing, with the React Testing Libaray(RTL) and the Jest testing framework.
It consist of a Next.js app which has Jest framweork and RTL installed, a couple of local modules created as test subjects, a markdown file which contains useful definitions and methods used frequently in RTL and Jest, and test files available in the __tests__
folder.
Content of the markdown file within the app folder is given below, for rapid access :
render()
method takes an element/React component as it's parameterscreen.X
methodscreen.getByX
queries are used to determine if an element is present in the DOMuserEvent
screen.queryByRole
is used to determine if an element is NOT rendered(if not found/present in the DOM). Thescreen.queryByX
methods will return null if the element is not found.screen.findByX
is used to asynchronously find an element and theasync await
keywords should be used in the callback function of the parentit
method.async
on the callback, andawait
on thescreen.findByX
method.screen.getAllByX
,screen.queryAllByX
,screen.findAllByX
which gives an array of all elements that match the description providedwaitFor
method is used alongside theexpect
method from Jest-dom library. It takes a callback function as it's parameter and an optional object which contains options on how the callback function should be called. Inside the callback function, should live theexpect
method which itself is the asynchronously executed function.beforeEach
method is a emthod that takes a callack funcntion as it's parameter and it's used inside of the callback of theit
method to state that the code inside it should run before each test in theit
method's callback function.
- the p tag cannot be gotten by
getByRole("paragraph)
, in stead get it by usinggetByTestId("sometestid")
. Before you do this, you have to give the p tag a propertydata-testid
and the value should be your desired test id "sometestid".
expect
method takes the html element/component as it's parameter Note that we can addnot.
before the assertion methods to inverse ittoBeInTheDocument
toBeVisible
toHaveValue
toHaveStyle
toHaveTextContent
toHaveClass
toBeNull
toBeDisabled
toHaveClass
toEqual
toBe
toBeTruthy
toContain
toBeDefined
e.g expect(header).toBeInTheDocument()
npm test -- --coverage
: This coverage flag allows us to get a report of the lines in our code that was actually tested. This report then becomes available in a directory calledcoverage/
that is created at runtime.npm test -- --silent
: Used to not print out results of the test in the console.jest --help
: Used to print out all the jest CLI options and their definitions, e.g coverage,silent e.t.c.
jest.fn()
is the function used to create functions mocked by jest. It is used inside the__mock__
folder, placed inside the same directory as the module we want to mock. However, inside of our test file, we usejest.mock(./module)
to mock the implementation of the real module and return our data from the mocked file in our__mocks__
folder. Note : Thejest.mock
method accepts the file path to the function being mocked as it's only parameter. This is only used for local modules, and not needed for modules inside thenode_modules
folder.
// import the actual module
import apiRequest from './api-request.js';
// then, tell Jest to mock the implementation!
jest.mock('./api-request.js');
it("Gets the full recipe for a dish", async () => {
// arrange
const dish = "Pesto";
const expectedValue = { "Magical Deliciousness": "3 cups" };
// set the resolved value for the next call to apiRequest
const mockResponse = {
status: "mock",
data: { "Magical Deliciousness": "3 cups" }
}
apiRequest.mockResolvedValueOnce(mockResponse);
// act
const actualRecipe = await findRecipe(dish);
// assert
expect(actualRecipe).toEqual(expectedValue);
});
Note : const MockResponse
is set as the expected resolved data that the apiRequest
method is expected to resolve to.
mockResolvedValueOnce
accepts our mocked returned data mockedResponse
and assigns the result to the