Project

General

Profile

Actions

Patch #37486

open

Add JavaScript unit tests.

Added by Takashi Kato over 2 years ago. Updated about 2 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Code cleanup/refactoring
Target version:
-
Start date:
Due date:
% Done:

0%

Estimated time:

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

0001-convert_to_esmodule.patch (6.41 KB) 0001-convert_to_esmodule.patch Takashi Kato, 2022-07-23 03:47
0002-add_mocha_js.patch (59.6 KB) 0002-add_mocha_js.patch Takashi Kato, 2022-07-23 03:47
0003-context_menu.patch (19.3 KB) 0003-context_menu.patch Takashi Kato, 2022-07-23 03:47
0004-add_document.patch (440 Bytes) 0004-add_document.patch Takashi Kato, 2022-07-23 03:47
Actions

Also available in: Atom PDF