-
Notifications
You must be signed in to change notification settings - Fork 249
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
Introduce Jest Unit Tests #3417
Comments
@cahirodoherty-learningpool @oliverfoster @swashbuck @kirsty-hames @taylortom @chris-steele @danielghost @guywillis @StuartNicholls Can I gather some thoughts on this. |
That all makes perfect sense and is easy to read in the files. Unit testing the ES6 Classes is going to prove a little problematic currently, see #3383 (comment) |
# [5.32.0](v5.31.31...v5.32.0) (2023-09-26) ### New * Introduce Jest component testing to the framework (fixes #3417) (#3383) ([c83f68b](c83f68b)), closes [#3417](#3417) [#3383](#3383) [Issue#64](https://github.com/Issue/issues/64) [issue#64](https://github.com/issue/issues/64) [Issue#64](https://github.com/Issue/issues/64) [Issue#64](https://github.com/Issue/issues/64) [Issue#64](https://github.com/Issue/issues/64) [Issue#64](https://github.com/Issue/issues/64) [#3428](#3428)
🎉 This issue has been resolved in version 5.32.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Subject of the issue/enhancement/features
Jest is a popular unit testing framework.
In the attached pull request I have unit tested the
Header
component.You can run the test with the following command:
yarn jesttest
Ensure that all packages are up to date when running the tests for the first time with
npm install
.IDE Considerations
There is the option to add the
Jest
extension from within Visual Studio Code.This provides a visual view of all running tests. It automatically runs all tests in watch mode and can be used to debug tests.
I have added in the
jest
env option to the eslinter to ensure the IDE recognises alljest
references to avoid eslint errors appearing.Unit Test Approach
To begin with we want to target all
core
jsx component templates to test that the component state gets updated correctly and the template renders as expected.Here is an example component unit test above, it passes the
body
property to theHeader
component and then asserts that theHeader
templates' text content is equal to thebody
text that was passed into the template.Folder Structure
All tests reside in a
__tests__
directory. The structure of adding tests adjacent to the file they are testing and adding atest.js
orspec.js
extension to the end of the test file name was not an option for us as all tests are to be added to the framework. We do not require thespec.js
ortest.js
named extension to the end of the test file names we have in the__tests__
directory.We are required to mock our ES6 class files. These will be contained within a
__mocks__
directory. The structure of this can be seen below. The example is relevant tocore/js/adapt.js
mock and Header component test.Further Code Considerations
Although we must have a base mock for our ES6 Classes of which are contained within the
__mocks__
directory, we can override some of the default properties/returned function calls within individual tests. I have included an example below for how to override a returned function call.This is the device mock where we originally set the returned value of
isScreenSizeMin
totrue
.Within the test we can override this with a spy, an example of this is given below.
jest.spyOn(device, 'isScreenSizeMin').mockReturnValue(false);
This will ensure the
device.isScreenSizeMin('medium')
call within theHeader.jsx
template returns false when the test renders the template.We can override properties from classes within individual tests by first ensuring the property is set up as a default value within the mock. Then in the test file, import the class and modify the property, an example can be seen below.
This is a mock with a property
screenSize
set tosmall
.Within the test you can then import the Device class and set the
screenSize
property to a value of your choice.Note that we must also provide a
default
property in our mock with a class instance for when the class is exported withexport default ClassName
.Question/Further Considerations
Are there any plans to unit test
JS
files? For example we could unit test our accessibility file a11y and ensure the functions are returning the correct values.The text was updated successfully, but these errors were encountered: