There is no Resium category so I’m adding this here, but please let me know if I should post this elsewhere…
Anyone managed to implement jest unit tests for a Resium app?
Here’s what I tried:
import React, { FC } from "react";
import { Viewer } from "resium";
import getWebGLStub from "../getWebGLStub";
interface TestViewerWrapperProps {
componentToTest: React.ReactNode;
}
const TestViewerWrapper: FC<TestViewerWrapperProps> = (props) => {
return (
<div>
<Viewer
full
timeline={false}
animation={false}
homeButton={false}
navigationHelpButton={false}
baseLayerPicker={false}
projectionPicker={false}
skyBox={false}
sceneModePicker={false}
skyAtmosphere={false}
fullscreenButton={false}
contextOptions={{ getWebGLStub: getWebGLStub }}
>
{props.componentToTest}
</Viewer>
</div>
);
};
export default TestViewerWrapper;
import React from "react";
import { render, screen } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import TestViewerWrapper from "../../testing/TestViewerWrapper/TestViewerWrapper";
import { useCesium } from "resium";
interface WrapperProps {}
function Wrapper(props: WrapperProps) {
const { viewer } = useCesium();
return (
<div>
{viewer ? (
<DemoButton
viewer={viewer}
></DemoButton>
) : null}
;
</div>
);
}
describe("<DemoButton />", () => {
test("it should mount", () => {
render(<TestViewerWrapper componentToTest={<Wrapper></Wrapper>} />);
const demoButton = screen.getByTestId("DemoButton");
expect(demoButton).toBeInTheDocument();
});
});
I added getWebGLStub.js based on Testing Cesium in Jest - #8 by shane_benlolo, I simply copied the file from the github repo mentioned in the last May 10 post on that link.
This is the error I get:
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: /Users/eli.leblanc/Documents/Repos/cmor-resium-app/node_modules/@cesium/engine/index.js:2 export { default as AutomaticUniforms } from './Source/Renderer/AutomaticUniforms.js'; ^^^^^^ SyntaxError: Unexpected token 'export' > 1 | import { clone, defaultValue, defined, DeveloperError, WebGLConstants } from "@cesium/engine"; | ^ 2 | 3 | function getWebGLStub(canvas, options) { 4 | const stub = clone(WebGLConstants); at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14) at Object.<anonymous> (src/testing/getWebGLStub.js:1:1) at Object.<anonymous> (src/testing/TestViewerWrapper/TestViewerWrapper.tsx:2:1) at Object.<anonymous> (src/components/DemoButton/DemoButton.test.tsx:8:1) at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13) at runJest (node_modules/@jest/core/build/runJest.js:404:19)
So clearly there is something wrong with the way I’m trying to use getWebGLStub.js. It is indeed a weird way to do it, but does anyone have suggestions?