react-codemirror icon indicating copy to clipboard operation
react-codemirror copied to clipboard

Error testing with Vitest

Open somecho opened this issue 2 years ago • 11 comments

I get the following error when I test a component with codemirror:

Error: Unrecognized extension value in extension set ([object Object]). This sometimes happens 
because multiple instances of @codemirror/state are loaded, breaking instanceof checks.

Here is a minimal project reproducing this error: https://github.com/somecho/react-codemirror-vitest

Someone else is also facing this issue.

somecho avatar May 23 '23 12:05 somecho

@somecho looks unsolvable

https://discuss.codemirror.net/t/error-multiple-instances-of-codemirror-state/5174/2

jaywcjlove avatar May 23 '23 12:05 jaywcjlove

Is this a problem with Vite then?

somecho avatar May 23 '23 14:05 somecho

@somecho I have no problem writing test cases with jest. I think vitest may need to be configured or modified.

jaywcjlove avatar May 24 '23 05:05 jaywcjlove

Tried the project with react-codemirror version 4.21.3 and the error still there.

Nepomuceno avatar Jun 20 '23 03:06 Nepomuceno

I've got this issue too.

Irev-Dev avatar Jul 13 '23 09:07 Irev-Dev

We are experiencing the same issue (Vitest+react-codemirror) from the moment we introduced the following code (relevant parts):

import { Compartment, EditorState } from '@codemirror/state';
import { insertTab, indentLess } from '@codemirror/commands';
import { keymap } from '@codemirror/view';

const tabSize = new Compartment();

let extensions = [
    [tabSize.of(EditorState.tabSize.of(4))],
    [keymap.of([{ key: 'Tab', run: insertTab }])],
    [keymap.of([{ key: 'Tab', run: insertTab, shift: indentLess }])],
];

editType && extensions.push(xml());
<CodeMirror
    ...
    extensions={extensions}
    ...
/>

This error only appears in tests and not in dev/production

boy-bizzmine avatar Jul 19 '23 11:07 boy-bizzmine

We're likely going to use jest. We had already being using jest from having started with CRA, but migrating to vite we were going to try and use vitest as well, but haven't been able to.

Irev-Dev avatar Jul 20 '23 02:07 Irev-Dev

@somecho fyi I managed to resolve the error in your reproduction repo with the following changes:

-import { clojure } from '@nextjournal/lang-clojure'
+import { langs } from '@uiw/codemirror-extensions-langs';
function App() {
  return (
    <>
      <div>Bug Reproduction</div>
      <CodeMirror
        value={'(+ 1 1)'}
-       extensions={[clojure()]}
+       extensions={[langs.clojure()]}

It's still using @nextjournal/lang-clojure under the hood, see https://github.com/uiwjs/react-codemirror/blob/fd6a8dbc731d87344f7c9016bbf151006a5b89a2/extensions/langs/src/index.ts#L45

AlanGreene avatar Aug 05 '23 23:08 AlanGreene

Any updates about this? I'm facing the same issue :/

jestfuljoker avatar Nov 23 '23 13:11 jestfuljoker

alias: {
  '@codemirror/state': path.resolve(
    __dirname,
    './node_modules/@codemirror/state/dist/index.cjs'
  ),
  '@codemirror/lang-yaml': path.resolve(
    __dirname,
  ),
  '@codemirror/lang-json': path.resolve(
    __dirname,
    './node_modules/@codemirror/lang-json/dist/index.cjs'
  )
}

Basically, you need to force the CJS version of @codemirror/state and all other packages depending on it.

yogiduzit avatar Apr 10 '24 01:04 yogiduzit