uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.69k stars 135 forks source link

Error testing with Vitest #506

Open somecho opened 1 year ago

somecho commented 1 year ago

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.

jaywcjlove commented 1 year ago

@somecho looks unsolvable

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

somecho commented 1 year ago

Is this a problem with Vite then?

jaywcjlove commented 1 year ago

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

Nepomuceno commented 1 year ago

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

Irev-Dev commented 1 year ago

I've got this issue too.

boy-bizzmine commented 1 year ago

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

Irev-Dev commented 1 year ago

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.

AlanGreene commented 1 year ago

@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

jestfuljoker commented 1 year ago

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

yogiduzit commented 7 months ago
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.