Skip to content

Commit 25d1e2d

Browse files
committed
fix test failures caused by monaco editor ESM CSS imports
Resolves GH-16401 - Add ESM loader hooks in @theia/test-setup to handle .css imports from @theia/monaco-editor-core ESM bundles during mocha test runs - Without this, Node's ESM resolver fails on .css imports, causing mocha's import→require fallback to partially execute test files twice, leading to duplicate side effects like FrontendApplicationConfigProvider.set() being called twice Contributed on behalf of STMicroelectronics
1 parent 47c0d1c commit 25d1e2d

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// *****************************************************************************
2+
// Copyright (C) 2026 STMicroelectronics and others.
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License v. 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0.
7+
//
8+
// This Source Code may also be made available under the following Secondary
9+
// Licenses when the conditions for such availability set forth in the Eclipse
10+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11+
// with the GNU Classpath Exception which is available at
12+
// https://www.gnu.org/software/classpath/license.html.
13+
//
14+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15+
// *****************************************************************************
16+
17+
// ESM loader hooks to handle non-JS file extensions (e.g. .css) that
18+
// are imported by ESM dependencies such as @theia/monaco-editor-core.
19+
// Without this, Node's ESM resolver throws ERR_UNKNOWN_FILE_EXTENSION
20+
// which causes mocha's import-then-require fallback to partially execute
21+
// test files before retrying, leading to duplicate side effects.
22+
23+
const STYLE_EXTENSIONS = ['.css', '.scss', '.sass', '.less'];
24+
25+
export function resolve(specifier, context, nextResolve) {
26+
return nextResolve(specifier, context);
27+
}
28+
29+
export function load(url, context, nextLoad) {
30+
if (STYLE_EXTENSIONS.some(ext => url.endsWith(ext))) {
31+
return { format: 'module', source: 'export default {};', shortCircuit: true };
32+
}
33+
return nextLoad(url, context);
34+
}

dev-packages/private-test-setup/test-setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
1515
// *****************************************************************************
1616

17+
// Register ESM loader hooks so that non-JS imports (e.g. .css files from
18+
// @theia/monaco-editor-core ESM bundles) are handled before mocha attempts
19+
// to load test files. Without this, Node's ESM resolver fails on .css
20+
// imports, and mocha's import→require fallback causes files to be partially
21+
// executed twice, leading to side-effect duplication.
22+
const { register } = require('node:module');
23+
register('./esm-loader-hooks.mjs', require('node:url').pathToFileURL(__filename));
24+
1725
// Mock DragEvent as '@lumino/dragdrop' already requires it at require time
1826
global.DragEvent = class DragEvent { };

0 commit comments

Comments
 (0)