Monaco-editor: Jest fails to load monaco-editor after an upgrade to 0.21.0

Created on 2 Dec 2020  路  4Comments  路  Source: microsoft/monaco-editor

monaco-editor version: 0.21.0+
Browser: N/A
OS: N/A
Severity: Very high (code cannot be tested)

The problem

0.21.0 release changed var global = self; to const global = self; in node_modules/monaco-editor/esm/vs/editor/editor.api.js. Now, this constant clashes with jest's 'global' producing the following error:

SyntaxError: Identifier 'global' has already been declared

Potential solution?

Changing the const back to var resolves the issue. The alternative solution is to rename global to anything else. Can this be fixed in 0.21.3?

How to reproduce

git clone https://github.com/A-Shleifman/monaco-jest-global-already-declared.git
cd monaco-jest-global-already-declared
npm i
npm t

The stack-overflow issue raised a few months ago: https://stackoverflow.com/questions/64042617/identifier-global-has-already-been-declared-at-compilefunction-error-in-jest

bug

Most helpful comment

All 4 comments

Same issue here. Is there a solution? I can't run jest on react components that are using monaco-editor.

@emilioSp I had to use patch-package to fix the problem directly in node_modules on postinstall.

monaco-editor+0.21.2.patch

diff --git a/node_modules/monaco-editor/esm/vs/editor/editor.api.js b/node_modules/monaco-editor/esm/vs/editor/editor.api.js
index 225acdb..8e9060e 100644
--- a/node_modules/monaco-editor/esm/vs/editor/editor.api.js
+++ b/node_modules/monaco-editor/esm/vs/editor/editor.api.js
@@ -6,7 +6,7 @@ import { EditorOptions } from './common/config/editorOptions.js';
 import { createMonacoBaseAPI } from './common/standalone/standaloneBase.js';
 import { createMonacoEditorAPI } from './standalone/browser/standaloneEditor.js';
 import { createMonacoLanguagesAPI } from './standalone/browser/standaloneLanguages.js';
-const global = self;
+var global = self;
 // Set defaults for standalone editor
 EditorOptions.wrappingIndent.defaultValue = 0 /* None */;
 EditorOptions.glyphMargin.defaultValue = false;

package.json

  "scripts": {
    "postinstall": "patch-package"
  }

@alexdima thanks for the fix. Any chance this will land in an upcoming release of monaco-editor ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

galyech picture galyech  路  3Comments

chengtie picture chengtie  路  3Comments

PinkyJie picture PinkyJie  路  3Comments

Panhaiwei picture Panhaiwei  路  3Comments

Spongman picture Spongman  路  3Comments