I wanted to start testing my React component with frameworks jest and enzyme. I'm writing a code with Typescript.
So I installed packages like: jest, enzyme, enzyme-adapter-react-16, ts-jest and typescript types required dependencies. Below I show entire package.json file:
{
"name": "test-learning-with-redux",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"#types/node": "^12.20.16",
"#types/react": "^17.0.14",
"#types/react-dom": "^17.0.9",
"node-sass": "^6.0.1",
"react": "^16.14.0",
"react-dom": "^16.14.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest --watch",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/enzyme": "^3.10.9",
"#types/enzyme-adapter-react-16": "^1.0.6",
"#types/jest": "^26.0.24",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.6",
"enzyme-to-json": "^3.6.2",
"jest": "^27.0.6",
"jest-enzyme": "^7.1.2",
"ts-jest": "^27.0.3",
"typescript": "^4.3.5"
}
}
The problem is that after installation dependencies tests works fine but react server dosen't work. I can observe error like on the screen below:
Of course I can solve this issue by adding .env with: SKIP_PREFLIGHT_CHECK=true
and it works, but I'm not for sure if it's the best solution.
On the other hand if I delete dependency with jest by typing npm uinstall jest, jest won't work and there will be an error: TypeError: Jest: a transform must export a process function.
More on the screen below:
Below I show jest.config.js configuration file:
My module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"snapshotSerializers": ["enzyme-to-json/serializer"],
"setupFilesAfterEnv": ["<rootDir>/src/setupEnzyme.ts"],
}
What's the best way to solve this problem in order to work normally with jest and typescript simultaneously? I could observe some comments where people write about forcing never version by react-scripts but I don't know how to do it. Maybe it's is better to stay with version from react-scripts and somehow solve TypeError from jest mentioned by me above but how?
I would be greateful for advices, prepositions.
Related
I am attempting to use console.log() to be able to verify some values in my react application, but as soon as I type console.log() anywhere within any .js file in the src folder, the page doesn't load anymore and an error is thrown, saying I cannot import the file console-browserify/index.js because it falls outside of the src folder.
I have bootstrapped the project with npx create-react-app and normally I don't face any problems.
Here is the error as well as my package.json file : enter image description here
{
"name": "lovebook",
"version": "0.1.0",
"private": true,
"dependencies": {
"#emotion/styled": "^11.9.3",
"#mui/material": "^5.8.6",
"#mui/x-date-pickers": "^5.0.0-beta.0",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#testing-library/user-event": "^13.5.0",
"#web3auth/web3auth": "^1.1.0",
"console": "^0.7.2",
"console-browserify": "^1.2.0",
"date-fns": "^2.28.0",
"grommet": "^2.25.0",
"grommet-icons": "^4.7.0",
"magic-sdk": "^7.0.0",
"moralis": "^1.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-moralis": "^1.4.0",
"react-router-dom": "^6.3.0",
"react-scripts": "5.0.1",
"styled-components": "^5.3.5",
"web-vitals": "^2.1.4",
"web3uikit": "^0.1.163"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
make sure you type "cd (your project path name)" when you create a new terminal, so any excess packages don't get installed into the node modules problem. Maybe you should recreate the project and check whether the error keeps showing. At last I'm not sure if it's gonna help much, but try reloading the server you're running your application on
I'm trying to make a typescript frontend but I can't use the react-dom/client import because of an issue that I've narrowed down to my testing-library/react version not corresponding with my react version. I've tried multiple downgraded versions but can't seem to get it to work.
Is there any solutions to this? I'll attach my package.json file below.
{
"name": "balls",
"version": "0.1.0",
"private": true,
"dependencies": {
"#material-ui/core": "^4.12.4",
"#material-ui/icons": "^4.11.3",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "12.1.5",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.5.1",
"#types/node": "^16.11.35",
"#types/react": "^16.8.6",
"#types/react-dom": "^16.8.6",
"#vercel/node": "^1.15.2",
"axios": "^0.27.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "5.0.1",
"typescript": "^4.6.4",
"vercel": "^24.2.3",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Looking at the peer dependencies of package.json of the version 12 of react-testing-library we can say that until version 12.*, react-testing-library supports any react version before react 18. As of react-testing-library 13, it requires react 18.
So to answer the question, at the time of writing, you can use react-testing-library 12.1.5 with react 16.8.6.
Hi Ive been racking my brain on what Ive been doing wrong. Locally the formatting is correct, but when I push it to Netlify, Tailwindcss decides to not work. Am I purging wrong? I have a tailwind css file in my dist and src folders.
package.json
{
"name": "bgg_version_3",
"keywords": [
"design, graphic design, art, illustration, web design, Virginia, Massachusetts, Boston, Newport News"
],
"homepage": ".",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"bootstrap": "^5.0.2",
"framer-motion": "^4.1.17",
"isomorphic-unfetch": "^3.1.0",
"next": "^11.0.1",
"react": "^17.0.2",
"react-bootstrap": "^2.0.0-beta.4",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"react-tsparticles": "^1.33.2",
"react-visibility-sensor": "^5.1.1",
"web-vitals": "^1.1.2"
},
"scripts": {
"build:tailwind": "tailwindcss build -i src/tailwind.css -o src/tailwind.output.css",
"prestart": "npm run build:tailwind",
"prebuild": "npm run build:tailwind",
"start": "craco start",
"build": "react-scripts build",
"test": "craco test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"sass": "^1.36.0"
}
}
tailwind.config.js
module.exports = {
purge: ["./src/*.{js,jsx,ts,tsx}", './src/*.{scss,css}',"./src/components/*.{js,jsx,ts,tsx}","./src/components/*.{scss,css}"],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
Any clue as to why it wont seem to work when deployed or what I did wrong?
Normally the devDependecies in your package.json would look somthing like this when using tailwindcss:
"devDependencies": {
"autoprefixer": "^10.3.1",
"postcss": "^8.3.6",
"tailwindcss": "^2.2.7"
}
So I would suggest you redo the process of installing tailwindcss and remember the --save flag on every install
I am building a website using create-react-app and have just installed eslint to it.
For some reason what was supposed to be shown as eslint warnings are showing up as errors and causing npm run start to fail.
How can I bypass this issue and have them shown as warnings like before ?
My .eslintrc.js
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '#typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['react', '#typescript-eslint'],
rules: {
'class-methods-use-this': 'off',
'additional-rule': 'warn',
},
ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
My package.json
"name": "device-protection-renewal-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.5",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.19.3",
"#types/react": "^16.9.55",
"#types/react-dom": "^16.9.9",
"babel-polyfill": "^6.26.0",
"core-js": "^3.6.5",
"i18next": "^19.8.3",
"react": "^17.0.1",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.1",
"react-i18next": "^11.7.3",
"react-scripts": "4.0.0",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all",
"ie >= 9"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version",
"ie >= 9"
]
},
"devDependencies": {
"#babel/plugin-transform-arrow-functions": "^7.12.1",
"#typescript-eslint/eslint-plugin": "^4.6.1",
"#typescript-eslint/parser": "^4.6.1",
"eslint": "^7.11.0",
"eslint-config-airbnb-typescript": "^9.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.0",
"prettier": "^2.1.2",
"typescript": "^4.0.5"
}
}```
[1]: https://i.stack.imgur.com/WUKcz.png
I assume that you have installed ESLint using npm install eslint --save-dev and defined a default configuration with node_modules/.bin/eslint --init answering the questions in the prompt.
I noticed that in your .eslintrc.js file, the ESLint settings is missing in the extends option:
extends: [
'eslint:recommended',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended',
],
Also in the package.json is recommended ESLint to have its own script that you run using npm run lint and use it combined with a eslint-plugin in your favorite code editor:
{
"scripts": {
"start": "react-scripts start",
// ...
"lint": "eslint ."
},
}
Probably you will build your application at some point, so you should create a .eslintignore file and inside of it add build since files in the build directory also get checked by default when the command is ran.
Source: https://fullstackopen.com/en/part3/validation_and_es_lint#lint
This part in your package.json is unnecessary; since you have an aslant config file, it should be moved to .eslintrc.js.
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
}
Which would then turn into this:
extends: [
'react-app',
'react-app/jest',
'airbnb-typescript',
'plugin:#typescript-eslint/recommended',
'prettier/react',
'prettier/#typescript-eslint',
'plugin:prettier/recommended'
]
However; in the latest versions of the eslint-config-react-app plugin at this time (7.31.11), I'm getting a jest plugin conflict error with a project that worked prior to updating unless I remove react-app/jest from my eslint configs extends section.
Which is how I ended up here, currently trying to find out what caused this.
Update: So my issue was caused because eslint-config-react-app depends on eslint-plugin-jest^25.7.0 and I was using the latest eslint-plugin-jest^27.1.6. I removed my package.json's dependency and will use the version included with eslint-config-react-app so there aren't any conflicts there, but if I need features of the newer plugin version, a quick npm i -D on it, changing eslint configuration from automatic to manual and specifying the path to the local node_modules version and .eslintrc.js should work as well; aside from any conflicts with the config plugin.
I created a react app with npx create-react-app my-app --template typescript and added the react component rn-sliding-up-panel. I know that rn stands for react native but the solution is pure js and works fine on the web.
Now I get Support for the experimental syntax 'classProperties' isn't currently enabled (35:20): Add #babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
Which is an awesome informative error message. Unfortunately I don't have an babel configuration since i want to do my whole transpiling with typescript.
I already tried to add the configuration to my package.json, but whitout any luck
{
"name": "slider-test",
"version": "0.1.0",
"private": true,
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties"
]
},
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"#types/jest": "^24.0.24",
"#types/node": "^12.12.21",
"#types/react": "^16.9.16",
"#types/react-dom": "^16.9.4",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.3.0",
"rn-sliding-up-panel": "^2.4.0",
"typescript": "^3.7.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#babel/plugin-proposal-class-properties": "^7.7.4"
}
}
How can I fix this?