Patch #37486

Add JavaScript unit tests.

Added by Takashi Kato 25 days ago. Updated 16 days ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:

0%

Category:Code cleanup/refactoring
Target version:-

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

0001-convert_to_esmodule.patch Magnifier (6.41 KB) Takashi Kato, 2022-07-23 03:47

0002-add_mocha_js.patch Magnifier (59.6 KB) Takashi Kato, 2022-07-23 03:47

0003-context_menu.patch Magnifier (19.3 KB) Takashi Kato, 2022-07-23 03:47

0004-add_document.patch Magnifier (440 Bytes) Takashi Kato, 2022-07-23 03:47

History

#1 Updated by Mizuki ISHIKAWA 16 days 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.

Also available in: Atom PDF