Jest: Cannot find module 'monaco-editor-core'

Viewed 594

I have an reactjs(typescript) app which uses monaco-editor-core, and not monaco-editor, with custom language. Jest tests fails, if i don't mock monaco-editor-core imports, with next error:

"Cannot find module 'monaco-editor-core'", 

From posts which I read I understood that jest doesn't support ESM. That's why there is monaco-editor-webpack-plugin was written, which doesn't work with monaco-editor-core. Maybe someone knows how can I fix import issue for monaco-editor-core for jest?

  • jest: 26.4.2
  • ts-jest: 26.3.0
  • typescript: 3.9.5
  • react: 16.9.0
  • monaco-editor-core: 0.21.3
  • webpack: 4.42.1

If I use moduleNameMapper at jest.config.js:

"monaco-editor-core": "<rootDir>/node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts"

I have next error:

 .../node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts:6
    declare global {
            ^^^^^^

    SyntaxError: Unexpected identifier at compileFunction (<anonymous>)

Comment if you need any additional info which can help.

PS: I can't tag it as monaco-editor-core...

Thanks for the help!

1 Answers

Adding of this to my jest.config.json worked for me:

"transformIgnorePatterns": [
    "node_modules\/(?!(monaco-editor|monaco-editor-core)\/)"
]
Related