Error when running tests while using konva - reactjs

I am trying to get started in building a react application using konva, react-konva.
I built a basic react component:
import React, { Component } from "react";
import { Stage } from "react-konva";
import Square from "./Square.jsx";
class Map extends Component {
render() {
return (
<Stage height={100} width={100}>
<Layer>
</Layer>
</Stage>
);
}
}
export default Map;
and also a test using jest:
/* global beforeEach describe expect it */
import { shallow } from "enzyme";
import React from "react";
import Map from "../Map";
function createComponent() {
const wrapper = shallow(
<Map />
);
return wrapper;
}
describe("Map component test", () => {
describe("When initializing the component", () => {
let sut;
beforeEach(() => {
sut = createComponent();
});
it("it should render a Stage component", () => {
expect(sut.find("Stage").exists()).toBe(true);
});
});
});
When this test runs, I get the following error:
TypeError: Cannot read property 'webkitBackingStorePixelRatio' of null
at node_modules/konva/konva.js:1291:36
at node_modules/konva/konva.js:1298:7
at Object.<anonymous> (node_modules/konva/konva.js:1477:3)
at Object.<anonymous> (node_modules/react-konva/src/react-konva.js:4:13)
at Object.<anonymous> (src/mapping/Map.jsx:2:19)
at Object.<anonymous> (src/mapping/MapContainer.js:3:12)
at Object.<anonymous> (src/App.jsx:3:21)
at Object.<anonymous> (src/tests/App.spec.js:5:12)
My dependencies from package.json:
"dependencies": {
"konva": "^1.6.3",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-konva": "^1.1.3",
"react-redux": "^5.0.5",
"redux": "^3.7.1"
},
"devDependencies": {
"babel-core": "^6.25.0",
"babel-loader": "^7.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"canvas": "^1.6.5",
"chalk": "^1.1.3",
"coveralls": "^2.13.1",
"css-loader": "^0.28.4",
"enzyme": "^2.8.2",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^15.0.1",
"eslint-plugin-import": "^2.5.0",
"eslint-plugin-jsx-a11y": "^5.0.3",
"eslint-plugin-react": "^7.1.0",
"eslint-watch": "^3.1.2",
"html-webpack-plugin": "^2.28.0",
"jest": "^20.0.4",
"jsdom": "^11.1.0",
"node-gyp": "^3.6.2",
"npm-run-all": "^4.0.2",
"react-test-renderer": "^15.6.1",
"redux-mock-store": "^1.2.3",
"style-loader": "^0.18.2",
"webpack": "^3.0.0"
}
I'm sure that this is related to node setup, but I am unsure of the correct way to do it. Can anyone help?

I fixed it by installing and using konva-node in my test:
import Konva from 'konva-node';
I also had to specify that it's not used in the browser before executing any tests:
Konva.isBrowser = false;
I am using the following dependencies:
"dependencies": {
"axios": "^0.18.0",
"konva": "^2.5.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-konva": "^16.5.2",
"react-scripts": "2.0.5"
},
"devDependencies": {
"chai": "^4.2.0",
"enzyme": "^3.7.0",
"enzyme-adapter-react-16": "^1.7.0",
"konva-node": "^0.5.5",
"sinon": "^7.1.1",
"sinon-chai": "^3.2.0"
},

There are several options:
The one mentioned by Alpar. It works for me but it requires building of old node-canvas version.
Using https://github.com/hustcc/jest-canvas-mock. If you are using Create React App just add import 'jest-canvas-mock' in your setupTests.js
Theoretically npm i -D canvas should solve this problem as well but I have not managed to make it work. Read here https://github.com/jsdom/jsdom/issues/1782 for more ideas.

Related

vue2 to react migration, tsx module not found

I have a vue2 app which does not implement typescript. I have decided to migrate the app to react. Thought process is to implement new features in react and let the old features run in vue. This is what I have done so far.
Created a new react-migration folder which will have all the new features.
Tried two ways to use the HelloWorld component
Create a react wrapper which takes the react component and renders it.
<template>
<div ref="container" />
</template>
<script>
import { createElement } from "react";
import { createRoot } from "react-dom/client";
export default {
inheritAttrs: false,
props: {
component: {
type: Function,
required: true,
},
},
data() {
return {
reactRoot: null,
};
},
watch: {
$attrs: {
deep: true,
handler() {
this.updateReactComponent();
},
},
},
mounted() {
this.reactRoot = createRoot(this.$refs.container);
this.updateReactComponent();
},
destroyed() {
this.reactRoot.unmount();
},
methods: {
updateReactComponent() {
this.reactRoot.render(createElement(this.component, this.$attrs));
},
},
};
</script>
Used veaury package to do the same.
Issue :
I have added all the ext in the config file.
extensions: ['*', '.js', '.vue', '.json','.tsx','.ts','.jsx']
I believe its because my vue project is not able to recognise the react tsx file.
All the help is appreciated.
Implementation reference articles
Veary implementation reference
React to vue migrations reference
package.json dependencies
"dependencies": {
"#cypress/vue": "^2.2.4",
"#fortawesome/fontawesome-svg-core": "^1.2.36",
"#fortawesome/free-brands-svg-icons": "^5.15.4",
"#fortawesome/free-regular-svg-icons": "^5.15.4",
"#fortawesome/free-solid-svg-icons": "^5.15.4",
"#fortawesome/vue-fontawesome": "^2.0.2",
"#tiptap/extension-character-count": "^2.0.0-beta.12",
"#tiptap/extension-collaboration": "^2.0.0-beta.19",
"#tiptap/extension-collaboration-cursor": "^2.0.0-beta.20",
"#tiptap/extension-font-family": "^2.0.0-beta.207",
"#tiptap/extension-highlight": "^2.0.0-beta.14",
"#tiptap/extension-link": "^2.0.0-beta.18",
"#tiptap/extension-table": "^2.0.0-beta.24",
"#tiptap/extension-table-cell": "^2.0.0-beta.13",
"#tiptap/extension-table-header": "^2.0.0-beta.15",
"#tiptap/extension-table-row": "^2.0.0-beta.13",
"#tiptap/extension-task-item": "^2.0.0-beta.16",
"#tiptap/extension-task-list": "^2.0.0-beta.16",
"#tiptap/extension-text-style": "^2.0.0-beta.207",
"#tiptap/extension-typography": "^2.0.0-beta.207",
"#tiptap/extension-underline": "^2.0.0-beta.207",
"#tiptap/html": "^2.0.0-beta.84",
"#tiptap/prosemirror-tables": "^1.1.4",
"#tiptap/starter-kit": "^2.0.0-beta.80",
"#tiptap/vue-2": "^2.0.0-beta.39",
"#vue/babel-preset-jsx": "^1.4.0",
"#vue/cli": "^4.5.15",
"axios": "^0.19.2",
"buefy": "^0.9.4",
"canvg": "^4.0.1",
"core-js": "^2.6.5",
"dompurify": "^2.4.3",
"echarts": "^4.4.0",
"eslint-plugin-cypress": "^2.12.1",
"google-fonts-webpack-plugin": "^0.4.4",
"hooper": "^0.3.4",
"html2pdf.js": "^0.10.1",
"jspdf": "^2.5.1",
"lodash": "^4.17.21",
"moment": "^2.24.0",
"npm-run-all": "^4.1.5",
"numeral": ">=2",
"postcss-loader": "^3.0.0",
"prosemirror-commands": "^1.5.0",
"prosemirror-history": "^1.3.0",
"prosemirror-keymap": "^1.2.0",
"prosemirror-schema-list": "^1.2.2",
"raw-loader": "^0.5.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "^1.57.1",
"sass-rem": "^3.0.0",
"socket.io": "^2.3.0",
"socket.io-client": "^2.3.0",
"veaury": "^2.3.11",
"vee-validate": "^3.2.1",
"vue": "^2.6.10",
"vue-axios": "^2.1.5",
"vue-carousel": "^0.18.0",
"vue-class-component": "^7.1.0",
"vue-content-loading": "^1.6.0",
"vue-drawer-layout": "^1.3.0",
"vue-echarts": "^4.0.4",
"vue-html2pdf": "^1.8.0",
"vue-i18n": "^8.0.0",
"vue-jwt-decode": "^0.1.0",
"vue-line-clamp": "^1.3.2",
"vue-moment": "^4.0.0",
"vue-numerals": "^3.0.5",
"vue-router": "^3.1.3",
"vue-star-rating": "^1.6.1",
"vuedraggable": "^2.23.2",
"vuex": "^3.1.2",
"webpack-dev-server": "^4.7.4",
"y-indexeddb": "^9.0.6",
"y-websocket": "^1.3.15",
"yjs": "^13.5.10"
},
"devDependencies": {
"#iconify/icons-icomoon-free": "^1.2.3",
"#iconify/vue2": "^2.0.1",
"#kazupon/vue-i18n-loader": "^0.3.0",
"#types/uuid": "^9.0.0",
"#vue/cli-plugin-babel": "^3.11.0",
"#vue/cli-plugin-eslint": "^3.11.0",
"#vue/cli-service": "^3.11.0",
"#vue/eslint-config-prettier": "^5.0.0",
"babel-eslint": "^10.0.3",
"cache-loader": "^4.1.0",
"css-loader": "^3.2.1",
"cypress": "^12.3.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-vue": "^5.2.3",
"file-loader": "^3.0.1",
"prettier": "^1.19.1",
"sass-loader": "^7.1.0",
"url-loader": "^2.1.0",
"vue-cli-plugin-i18n": "^0.6.0",
"vue-eslint-parser": "^7.0.0",
"vue-template-compiler": "^2.6.14",
"webpack": "^4.46.0"
}

Property 'mockResolvedValue' does not exist on type '<T = any, R = AxiosResponse<T>>

I'm trying to do the Mocking Module example on the Jest official documentation here: https://jestjs.io/docs/mock-functions
test('should fetch users', () => {
const users = [{name: 'Bob'}];
const resp = {data: users};
axios.get.mockResolvedValue(resp);
return Users.all().then(data => expect(data).toEqual(users));
});
but the mockResolvedValue gives me this typescript error:
Property 'mockResolvedValue' does not exist on type '<T = any, R = AxiosResponse>(url: string, config?: AxiosRequestConfig | undefined) => Promise'.ts(2339)
my dev dependency:
"devDependencies": {
"#babel/cli": "7.13.0",
"#babel/core": "7.12.17",
"#babel/plugin-proposal-class-properties": "7.12.13",
"#babel/preset-env": "7.12.17",
"#babel/preset-react": "7.12.13",
"#babel/preset-typescript": "7.12.17",
"#jest/reporters": "26.6.2",
"#types/axios": "0.14.0",
"#types/jest": "26.0.20",
"#types/luxon": "1.26.2",
"#types/node": "14.14.35",
"#types/react": "17.0.3",
"#types/react-dom": "17.0.2",
"#types/react-redux": "7.1.16",
"#types/react-router": "5.1.12",
"#types/react-router-dom": "5.1.7",
"#types/redux-actions": "2.6.1",
"#types/redux-logger": "3.0.8",
"#types/redux-mock-store": "1.0.2",
"#types/styled-components": "5.1.9",
"#types/webpack-env": "1.16.0",
"#types/yup": "0.29.9",
"#typescript-eslint/eslint-plugin": "4.15.2",
"#typescript-eslint/parser": "4.15.1",
"axios": "0.21.1",
"axios-hooks": "2.5.0",
"babel-core": "7.0.0-bridge.0",
"babel-eslint": "10.1.0",
"babel-jest": "26.6.3",
"babel-loader": "8.2.2",
"babel-plugin-css-modules-transform": "1.6.2",
"babel-plugin-inline-react-svg": "2.0.0",
"backstopjs": "5.0.6",
"cheerio": "0.22.0",
"chokidar-cli": "2.1.0",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.6",
"enzyme-to-json": "3.6.1",
"eslint": "7.20.0",
"eslint-config-prettier": "8.1.0",
"eslint-config-react-app": "6.0.0",
"eslint-plugin-flowtype": "5.2.2",
"eslint-plugin-import": "2.22.1",
"eslint-plugin-jest": "24.1.5",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-prettier": "3.3.1",
"eslint-plugin-react": "7.22.0",
"eslint-plugin-react-hooks": "4.2.0",
"j1r-react-scripts": "1.7.3",
"j1r-wiremock-cli": "0.3.7",
"jest": "26.6.3",
"jest-environment-enzyme": "7.1.2",
"jest-enzyme": "7.1.2",
"jest-junit": "12.0.0",
"jest-mock-promise": "1.1.10",
"jest-resolve": "26.6.2",
"jest-watch-typeahead": "0.6.1",
"lighthouse": "7.1.0",
"lint-staged": "10.5.4",
"miragejs": "0.1.41",
"nightwatch": "1.5.1",
"npm-run-all": "4.1.5",
"prettier": "2.2.1",
"prop-types": "15.7.2",
"react-app-rewire-babel-loader": "0.1.1",
"react-app-rewired": "2.1.8",
"react-scripts": "3.4.1",
"redux-devtools-extension": "2.13.8",
"redux-mock-store": "1.5.4",
"sass-loader": "11.0.1",
"selenium-server": "3.141.59",
"sort-package-json": "1.49.0",
"ts-mockito": "2.6.1",
"typedoc": "0.19.2",
"typescript": "4.2.2",
"webpack": "5.23.0",
"whatwg-fetch": "3.6.1",
"react-app-rewire-alias": "1.0.1"
},
"jest-junit": {
"suiteName": "tests",
"outputDirectory": "./generated",
"outputName": "./jest-report.xml",
"classNameTemplate": "{classname}-{title}",
"titleTemplate": "{classname}-{title}",
"ancestorSeparator": " > ",
"usePathForSuiteName": "true"
},
thanks for the help
Casting the Axios function as a mock did the trick for me. So changing your mock to
(axios.get as jest.Mock).mockResolvedValue(resp);
Should make it work.
Two ways to fix the TS types issue of the mock object.
Use jest.MockedFunction
import axios from 'axios';
jest.mock('axios');
// ok
(axios.get as jest.MockedFunction<typeof axios.get>).mockResolvedValue({});
// Better
const mAxiosGet = jest.MockedFunction<typeof axios.get> = axios.get;
mAxiosGet.mockResolvedValue({});
mocked helper function of ts-jest.
import axios from 'axios';
import { mocked } from 'ts-jest/utils';
jest.mock('axios');
const mAxiosGet = mocked(axios.get);
expect(jest.isMockFunction(mAxiosGet)).toBeTruthy();
mAxiosGet.mockResolvedValueOnce({});
Just in case you arrive here with the same error but in a slightly different scenario.
I'm upgrading from Jest v28 to v29 and I had a test with this:
jest.mock('../../../utils/http-client');
const mockHttpClient = jest.mocked(httpClient, true);
After the upgrade I've got an error like this:
Argument of type 'boolean' is not assignable to parameter of type '{ shallow: true; }'.ts(2769)
So I changed to:
const mockHttpClient = jest.mocked(httpClient, shallow: true);
Then I got the same error than the one posted by the OP here:
describe('custom-config-api test suite', () => {
beforeEach(() => {
mockHttpClient.get.mockResolvedValue({ data: { results: [] } });
});
And solved it by setting shallow to false instead of true. The answer by J-Cint also worked, but I think this one is cleaner in my case.
Hope it helps!

Snowpack with Ant Design: Import "antd" could not be resolved

I want to use Ant Design with Snowpack. I followed the And Design docs and installed antd but whenever I run my application the dependency can't be resolved.
I get the following error message:
[snowpack] Import "antd" could not be resolved.
If this is a new package, re-run Snowpack with the --reload flag to rebuild.
If Snowpack is having trouble detecting the import, add "install": ["antd"] to your Snowpack config file.
My App.tsx:
import React from 'react';
import './App.css';
import { Button } from 'antd';
function App(): JSX.Element {
return (
<div className="App">
<Button type="primary">Button</Button>
</div>
);
}
export default App;
My package.json dependencies:
"dependencies": {
"antd": "^4.6.2",
"axios": "^0.20.0",
"postcss": "^7.0.32",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-hook-form": "^6.7.0",
"tailwindcss": "^1.7.6"
},
"devDependencies": {
"#snowpack/app-scripts-react": "^1.10.0",
"#testing-library/jest-dom": "^5.5.0",
"#testing-library/react": "^10.0.3",
"#types/react": "^16.9.35",
"#types/react-dom": "^16.9.8",
"#types/snowpack-env": "^2.3.0",
"#typescript-eslint/eslint-plugin": "^4.0.1",
"#typescript-eslint/parser": "^4.0.1",
"eslint": "^7.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"jest": "^26.2.2",
"postcss-cli": "^7.1.2",
"prettier": "^2.0.5",
"snowpack": "^2.9.0",
"typescript": "^3.9.7"
}
Am I missing something?
Make sure antd is already installed. Try this in snowpack.config.js:
module.exports = {
mount: {
public: "/",
src: "/_dist_",
},
installOptions: {
namedExports: ["antd"],
},
};

React app throwing error: Uncaught TypeError: Cannot read property 'shape' of undefined

The relevant line is here in bundle.js:
exports["default"] = _react.PropTypes.shape({
subscribe: _react.PropTypes.func.isRequired,
dispatch: _react.PropTypes.func.isRequired,
getState: _react.PropTypes.func.isRequired
});
I just upgraded to React 16, and have followed this tutorial to adjust PropTypes. I only have one component which references prop types, like this:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
export default function (ComposedComponent) {
class Authentication extends Component {
static contextTypes = {
router: PropTypes.object
}
What am I doing wrong here?
EDIT: Here are my dependencies:
"devDependencies": {
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"css-loader": "^0.28.7",
"ignore-styles": "^5.0.1",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"loaders": "^1.1.3",
"mocha": "^2.4.5",
"node-sass": "^4.5.3",
"prop-types": "^15.6.0",
"react-addons-test-utils": "^0.14.7",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.16.2",
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-spinkit": "^3.0.0",
"redux": "^3.0.4",
"redux-form": "^7.0.4",
"redux-thunk": "^2.2.0",
"webpack": "^3.8.1"
}
EDIT 2: Even when I comment out this line, the same error appears. But this line is the only place in the entire project where I mention prop types. What gives?
static contextTypes = {
router: PropTypes.object
}
The current version of react-spinkit is not ready for React 16.0 because it still uses the now unsupported React.PropTypes:
Relevant code on GitHub
Last commit was on May, 22. I am afraid that you will not be able to upgrade React right now. But it looks like the current code is already a mix of the two, and there is only one line to change, so you could just submit to them a pull request with that change.

React-native/Enzyme : mount throws "unknown props" on native elements

I'm writing tests using Jest & Enzyme in React Native to fully test my components behaviors and inner functions.
Shallow test seems ok but when using mount it just throws
Here's my package.json.
{
"name": "Despirithium",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"buildandroid": "react-native bundle --platform=android --entry-file=index.android.js --bundle-output='android/app/src/main/assets/index.android.bundle?dev=false",
"test": "jest --watch"
},
"dependencies": {
"axios": "^0.15.*",
"dpath": "^2.0.2",
"evosphere-router": "0.1.9",
"immutable": "^3.8.1",
"intl": "^1.2.5",
"lodash": "^4.16.4",
"node-uuid": "^1.4.7",
"normalizr": "^2.2.1",
"react": "15.3.2",
"react-native": "0.36.0",
"react-native-drawer": "^2.3.0",
"react-native-i18n": "^0.1.1",
"react-native-image-picker": "^0.22.12",
"react-native-router-flux": "^3.35.0",
"react-native-tabs": "^1.0.9",
"react-redux": "^4.4.5",
"redux": "^3.6.0",
"redux-immutable": "^3.0.8",
"redux-logger": "^2.7.0",
"redux-thunk": "^2.1.0",
"string-template": "^1.0.0",
"tcomb-form-native": "^0.6.1"
},
"devDependencies": {
"babel-core": "^6.14.0",
"babel-jest": "^17.0.2",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-react-jsx": "^6.8.0",
"babel-plugin-transform-remove-console": "^6.8.0",
"babel-polyfill": "^6.13.0",
"babel-preset-latest": "6.14.0",
"babel-preset-react-native": "^1.9.0",
"babel-preset-react-native-stage-0": "^1.0.1",
"babel-register": "^6.14.0",
"enzyme": "^2.6.0",
"enzyme-to-json": "^1.3.0",
"faussaire": "^0.2.2",
"faussaire-util": "^0.1.0",
"jest": "^17.0.3",
"jest-enzyme": "^2.0.0",
"jest-react-native": "^17.0.3",
"react-addons-test-utils": "15.3.2",
"react-dom": "15.3.2",
"react-native-fs": "^2.0.1-rc.2",
"react-native-mock": "^0.2.7",
"react-test-renderer": "15.3.2",
"updtr": "^0.2.1"
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"setupTestFrameworkScriptFile": "node_modules/jest-enzyme/lib/index.js",
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
"es6"
],
"preset": "jest-react-native"
}
}
For now my test is pretty simple.
global.Promise = require.requireActual('promise');
import 'react-native';
import React from 'react';
import { mount, shallow } from 'enzyme';
import { mountToJson } from 'enzyme-to-json';
import { LoginScene } from '../../../../../../src/bundles/application/ui/scene/public/LoginScene';
import configureStore from '../../../../../../src/configureStore';
import { Provider } from 'react-redux';
import { getState, getInstance } from '../../../../../../src/testing/enzymeUtils';
test('LoginScene', async () => {
const store = await configureStore();
let loginScene;
const wrapper = mount(
<Provider store={store}>
<LoginScene/>
</Provider>
);
expect(mountToJson(wrapper)).toMatchSnapshot();
loginScene = wrapper.find(LoginScene);
getInstance(loginScene)._handleFieldChange('username', 'despirithium');
getInstance(loginScene)._handleFieldChange('password', 'azerty');
expect(getState(wrapper.find(LoginScene)).username).toBe('despirithium');
expect(getState(wrapper.find(LoginScene)).password).toBe('azerty');
});
I would have shallow rendered this but since I need to wrap my scene into the Provider it's not possible.
You may need to wrap Provider in a div. "...Components must return a single root element..." -https://facebook.github.io/react/docs/components-and-props.html

Resources