josdejong / svelte-jsoneditor

A web-based tool to view, edit, format, repair, query, transform, and validate JSON
https://jsoneditoronline.org
Other
820 stars 108 forks source link

Cannot test react example: SyntaxError: Unexpected token 'export' #250

Closed ojemuyiwa closed 1 year ago

ojemuyiwa commented 1 year ago

Using react typescript

npm install vanilla-jsoneditor

added from documentation example

/* istanbul ignore file */ //NOT TO BE TESTED EXTERNAL DEPENDENCY
import { JSONEditor } from 'vanilla-jsoneditor';
import type { OnChange }  from 'vanilla-jsoneditor';
import { useEffect, useRef } from 'react';
import styles from './svelte-json-editor.module.css';

/* eslint-disable-next-line */
export interface SvelteJsonEditorProps {
    content: any;
    mode?: 'tree' | 'text' | 'table';
    mainMenuBar?: boolean;
    navigationBar?: boolean;
    statusBar?: boolean;
    readOnly?: boolean;
    indentation?: number | string;
    tabSize?: number;
    escapeControlCharacters?: boolean;
    escapeUnicodeCharacters?: boolean;
    flattenColumns?: boolean;
    onChange?: OnChange
}
/**
 * https://codesandbox.io/s/svelte-jsoneditor-react-59wxz?file=/src/App.js:1150-1201
 * @param props SvelteJsonEditorProps extend from documentation https://github.com/josdejong/svelte-jsoneditor/#properties
 * @returns 
 */
export function SvelteJsonEditor(props: SvelteJsonEditorProps) {
    const refContainer = useRef<any>(null);
    const refEditor = useRef<any>(null);

    useEffect(() => {
        // create editor
        refEditor.current = new JSONEditor({
            target: refContainer.current,
            props: {},
        });

        return () => {
            // destroy editor
            if (refEditor.current) {
                refEditor.current.destroy();
                refEditor.current = null;
            }
        };
    }, []);

    // update props
    useEffect(() => {
        if (refEditor.current) {
            refEditor.current.updateProps(props);
        }
    }, [props]);

    return (
        <div
            className={styles['svelte-jsoneditor']}
            ref={refContainer}
        ></div>
    );
}

export default SvelteJsonEditor;

import the above file and run jest tests where the above is used causes

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:
ojemuyiwa commented 1 year ago

Ignore i'm just going to / istanbul ignore file / everything and mock the component, been 2 days trying to test this

josdejong commented 1 year ago

Thanks for reporting. It's not entirely clear to me what causes these difficulties in your setup. Is it the translation from the plain JavaScript example to TypeScript? Or is it related to a setup with specific strict code style requirements? The code is quite standard React and JavaScript style, there is nothing "weird" going on there.

As for a TypeScript example, see: https://github.com/josdejong/svelte-jsoneditor/blob/main/README-VANILLA.md#typescript

ojemuyiwa commented 1 year ago

Thanks for reporting. It's not entirely clear to me what causes these difficulties in your setup. Is it the translation from the plain JavaScript example to TypeScript? Or is it related to a setup with specific strict code style requirements? The code is quite standard React and JavaScript style, there is nothing "weird" going on there.

As for a TypeScript example, see: https://github.com/josdejong/svelte-jsoneditor/blob/main/README-VANILLA.md#typescript

Unsure myself, just doesn't compile for jest tests using NX Cli, i've exempted it from tests. Thanks anyways and much appreciated. 👍