Skip to content

Commit b2b3c01

Browse files
authored
fix(cds-plugin-ui5): disable plugin for JEST test execution (#906)
As dynamic imports are only supported in JEST with Node.js 21 and the experimental flag: NODE_OPTIONS=--experimental-vm-modules we by default disable the cds-plugin-ui5 to avoid issues. It can be forcefully activated by CDS_PLUGIN_UI5_ACTIVE=true when testing in the environment as described above. Fixes #901
1 parent e4b63a5 commit b2b3c01

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Diff for: packages/cds-plugin-ui5/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ module.exports = async ({ log, resources, options }) => {
146146

147147
The returned app pages will be added to the welcome page within the respective mount path.
148148

149+
## Hints
150+
151+
This section includes hints for the usage of the `cds-plugin-ui5` with other tools.
152+
153+
### JEST
154+
155+
The `cds-plugin-ui5` doesn't work with JEST out of the box as it internally is using dynamic imports to load helpers from the UI5 tooling. JEST fails with a `segmentation fault` error and therefore, the `cds-plugin-ui5` is disabled when running in context of JEST. It can be forcefully enabled by setting the environment variable `CDS_PLUGIN_UI5_ACTIVE=true`. But in this case you need to at least use Node.js 21 (https://github.com/nodejs/node/issues/35889) and you need to enable the experimental support for ES modules (https://jestjs.io/docs/ecmascript-modules). This enables the `cds-plugin-ui5` to run in context of JEST.
156+
149157
## Support
150158

151159
Please use the GitHub bug tracking system to post questions, bug reports or to create pull requests.

Diff for: packages/cds-plugin-ui5/cds-plugin.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
1+
const log = require("./lib/log");
2+
3+
// >> IMPORTANT <<
4+
//
5+
// JEST has issues with dynamic imports and will fail when they are used,
6+
// e.g. in the findUI5Modules the UI5 tooling is used which is implemented
7+
// using ES modules. To avoid issues when running JEST tests, the plugin
8+
// will be disabled by default but it can be enforced with CDS_PLUGIN_UI5_ACTIVE=true
9+
// since JEST supports ES modules when using Node.js 21 and the experimental
10+
// support for VM modules via:
11+
//
12+
// > NODE_OPTIONS=--experimental-vm-modules jest
13+
//
14+
// Details can be found in the following issue:
15+
// - https://github.com/ui5-community/ui5-ecosystem-showcase/issues/901
16+
//
17+
// To disable JEST we rely on env variables (see https://jestjs.io/docs/environment-variables)
18+
if (process.env.NODE_ENV === "test" && process.env.JEST_WORKER_ID && process.env.CDS_PLUGIN_UI5_ACTIVE !== "true") {
19+
log.info("Skip execution of plugin because JEST is running tests! To force the execution of the plugin set env var CDS_PLUGIN_UI5_ACTIVE=true...");
20+
return;
21+
}
22+
if (process.env.CDS_PLUGIN_UI5_ACTIVE === "false") {
23+
log.info("Skip execution of plugin because it has been disabled by env var CDS_PLUGIN_UI5_ACTIVE!");
24+
return;
25+
}
26+
127
// @sap/cds/lib/index.js#138: global.cds = cds // REVISIT: using global.cds seems wrong
228
const cds = global.cds || require("@sap/cds"); // reuse already loaded cds!
329

4-
const log = require("./lib/log");
530
const findUI5Modules = require("./lib/findUI5Modules");
631
const createPatchedRouter = require("./lib/createPatchedRouter");
732
const applyUI5Middleware = require("./lib/applyUI5Middleware");

0 commit comments

Comments
 (0)