Skip to main content

Unit testing with RTL

Jest setup file

Developers can add global test features,enviroment,mocks in the file config\jest\jest.setup.js

Global mock setup

Mocks for files such as jpg, jpeg, png, gif, eot, otf, webp, svg, ttf, woff, woff2, mp4, webm, wav, mp3, m4a, aac, oga and stylesheets are configured

git hooks

pre-push testing is added in husky

Test Util Functions

Test Util FunctionsDescription
assertByTestId(renderComp,testId,isTruthy)assert whether testId is present
assertByTextContent(getByText,textContent)assert whether given textContent is present
assertProperty(obj,key,val)assert value of the property in an object
renderWithContext({Comp,ContextProviderRef,state,props})renders the component with given context
assertMockFunctionArg ({mockFunction,funCallIndex,argIndex,argument})asserts the mth Argument of Mock Function for n number of function calls
ParameterDescription
renderCompRendered Component
testIdTestId which has to be checked
isTruthyif truthy, test passes if the testId is present; if falsy, test passes if the testId is not present
getByTextThe getByText property reference of rendered
textContentText String that needs to be asserted
objobject reference
keyproperty
valexpected value of the key
CompComponent
ContextProviderRefcontext provider (ex: contextProviderRef=SomeContext.Provider)
statestate of context that has to be assigned
propsprops that needs to be assigned to the given Component ie.Comp
mockFunctionMock Function
funCallIndexIndex of nth function call (note:Indexing starts from 0,Index of function for the first call is 0)
argIndexIndex of the argument (Indexing starts from 0)
argumentexpected argument