-
Notifications
You must be signed in to change notification settings - Fork 334
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
[old][SPIKE][DO NOT MERGE] Investigate using Jest for unit testing #2611
Conversation
Update: I've pulled in some commits from the i18n spike to see how this testing strategy fits with that. Broadly it seems to work fine - we can pull in and set translations within unit tests so have control over testing how component JS handles different translations. We can also add tests for the i18n function itself (note: this has been tested with the previous iteration of the i18n function, not the new version using the Singleton pattern). Adding any kind of translations to our example app will require us to change existing tests that require the review app as the text will be different. Probably another reason why having tests based on our review app is perhaps not ideal. |
Using the error summary component as an example. Needed to: - Add a new Jest 'project' to run anything called **.unit.test.js as a unit test with it's own config - Use babel-jest (already installed with Jest) to tranform component code before testing, so we can import component JS in our unit.test.js files - Adds a basic function and test in the ErrorSummary component JS and new unit test file to check the setup works
Delicately shifts the I18n function to use a singleton pattern. This allows the use of the same instance of I18n across components, without having to explicitly pass though the I18n instance, by using the static I18n.getInstance() method. Currently only the first I18n instance is saved and retrievable by getInstance, as it's assumed this is the 'default' I18n instance and that any subsequent I18n instances are for situations where the default is being overridden (e.g. if a single instance of a component requires different UI labels to other instances of the other components).
If the lookup key specified within a component is falsy (such as being an empty string, null, undefined, or a few other conditions), i18n.js will output an error in the page's console. This will not stop execution of the script, which will instead try to use the fallback string or otherwise output "UNDEFINED".
Adds check to verify that the count is something JavaScript understands as a number. If it is, does some manipulation to make sure the number is positive and not a decimal, as our counting logic doesn't support either of these yet.
Removes the config option to change the separators used by placeholder strings. This is due to placeholders also being present in hardcoded fallback strings, which couldn't easily be changed if the separator configuration was changed. As customising separators was already only a nice to have, I've removed the configuration option for now.
Adds a regular expression to check if a string contains a placeholder before putting it through the function to swap out placeholders. If no placeholders, no loop!
This spike is superseded by #2680 which works with the latest (and hopefully more "stable" i18n approach) and the new |
Investigation into #2559
What
Spike to see how we can add unit tests for our component JavaScript. This spike explores using tooling we already have (i.e: Jest) rather than introducing a new library.
Findings / Thoughts
.mjs
file extension (among other things) which complicates the current testing we have. You can also use Babel (Jest ships withbabel-jest
) to compile code, which is the approach used in this spike.