Patch #37486
openAdd JavaScript unit tests.
0%
Description
Currently, Redmine does not have JavaScript unit tests; I propose to add lightweight unit tests in a Node.js environment.
Prerequisite environment: Node.js 16 or later, yarn 1.22
Tools to deploy for unit testing: mocha, chai, jsdom
This patch does not require all Redmine developers, including plugins and themes, to install Node.js. This patch uses Node.js only for JavaScript unit tests (like when adding stylelint).
Applying this patch and running yarn install will install the necessary tools for testing.
As a first example of adding tests, I added tests to context_menu.js and refactored them.
As a prerequisite for testing, I convert JavaScript to ES modules. (https://javascript.info/modules-intro)
- ES modules do not use global namespaces by default. There is no longer a need to fear duplicate function names; context_menu.js prefixed all functions "ContextMenu" to avoid function names conflict, but that practice is no longer necessary.
- Modularizing the scripts will delay processing, waiting until the HTML document is fully ready, which should speed up HTML processing as we move forward.
- It would be impractical to es-modularize all of application.js at once, as there is a lot of JavaScript tightly coupled to html (using onclick, link_to_function, etc.). We need to consider other ways to improve.
This patch also contains fix #37481.
The patch can be applied to r21737
Files
Updated by Mizuki ISHIKAWA over 2 years ago
+1
Some features like context_menu have a lot of code.
Therefore, I felt that it was difficult to verify that it would work properly when I changed some code.
It would be nice to have JavaScript unit tests.