I try to build modular React application using monorepository (workspaces) - in root folder I have package.json:
{
"private": true,
"workspaces": ["shared/react-components", "client/react-app"],
"scripts": {
"start": "yarn --cwd shared/react-components build && yarn --cwd client/react-app start"
}
}
I want to include shared/react-components (named e.g. as #shared/react-components), into client/react-app but before I need to compile shared module, and at this place I get nasty error:
Error: 'default' is not exported by ../../node_modules/deepmerge/dist/cjs.js, imported by src/styles/dark.ts
I ran yarn install before so deepmerge is available in /node_modules. If I remove workspaces configuration and run: yarn install && yarn build in shared/react-components everything works, so it seems that problem is in workspaces configuration.
Build script in shared/react-components is: rollup -c
Any idea what could be missing?
It turned out to be a problem was with deepmerge package and TSX, the long thread about problems with importing can be found here: https://github.com/TehShrike/deepmerge/issues/87 I replaced it with lodash merge, and my packages were build again.
I'd like to use the node --debug-brk feature described in a previous answer and the jest docs to debug in npm/yarn 1 based projects, but node_modules/ is not present in Yarn 2 / PnP (Plug n Play) based projects, so those instructions won't work for me.
The usual way to access a binary in yarn 2 of yarn run --inspect-brk jest --runInBand launches a debugging session from the terminal, but when connecting to Chrome's Remote Target inspector via chrome://inspect, we get an error message about being unable to connect to the jest.js binary, since it's a path to a .zip file.
Uncaught Error: Cannot find module '/Users/MY_USERNAME/PATH_TO_MY_PROJECT/.yarn/cache/jest-npm-24.9.0-8ddb425e99-2.zip/node_modules/jest/bin/jest.js
What workarounds are available without downgrading to Yarn 1?
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Jest",
"skipFiles": ["<node_internals>/**"],
"cwd": "${workspaceFolder}",
"runtimeExecutable": "yarn",
"runtimeArgs": ["run", "--inspect-brk", "jest"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"port": 9229
}
]
}
Update: the workaround reported below is necessary for Node 12.15 and below. After 12.16.1, you can use the following command directly:
yarn run --inspect-brk jest --runInBand
The fix which was added to node 12.16.1 is here.
Archived answer for older versions of Node, 12.15 and below
One option is to use yarn unplug before using yarn run
yarn unplug jest
After that, the following works with Jest in Yarn 2 and Chrome's chrome://inspect:
yarn run --inspect-brk jest --runInBand # any additional jest args
A downside of this approach is that you may need to undo the unplug before checking in your branch, since it's described as a short term measure rather than a long term state to leave a dependency in.
You can also use the yarn bin command to accomplish this. I was able to run our jest tests using node and some debugging flag helpers (in a Yarn pnp context) using this command:
yarn node --inspect-brk --expose-gc $(yarn bin jest) test --runInBand --silent --logHeapUsage
Credit - Found the solution posted in this jest issues thread.
I have created a react app which implements husky to capture lint errors:
Environment
git version 2.21.0 (Apple Git-122), node v8.16.2, npm v6.4.1
Lint implementation
Created a react using npx create-react-app my-app-name
Implemented eslint using eslint --init
Added the script to the package.json file:
“scripts”: {“lint”: “eslint src/**/*.js”,}
On running eslint src/**/*.js or npm run lint the lint errors are captured perfectly
Husky implementation
Installed husky npm install husky --save-dev
Added the husky hook to package.json:
"husky": {
"hooks": {
"pre-commit": "npm run lint:fix",
"pre-push": "npm run lint"
}
}
Testing git commits
Ran git commit -m "test commit"
Problem
The lint is never called when the commit is triggered. What is wrong here? Btw, I have tried solutions proposed here.
husky requires node > v10. Otherwise, it will skip with a warning message in the console.
Your node version is v8.16.2, please upgrade the same.
I have a test file like so: (I am using create-react-app)
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/Calculator';
import { getAction, getResult } from './actions/'
import {shallow} from 'enzyme';
import toJson from 'enzyme-to-json';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
it('renders without crashing', () => {
const wrapper = shallow(<App />)
expect(toJson(wrapper)).toMatchSnapshot();
});
it('displays the choosen operator', () => {
const action = {
type: 'GET_ACTION',
operator: '+'
};
expect(getAction("+")).toEqual(action)
})
it('displays the typed digit', () => {
const action = {
type: 'GET_RESULT',
n: 3
};
expect(getResult(3)).toEqual(action);
})
it('checks that the clickevent for getNumber is called',() => {
const clickEvent = jest.fn();
const p = shallow(<p data-n="1" onClick={clickEvent}>1</p>)
p.simulate('click')
expect(clickEvent).toBeCalled();
})
and a package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
// "test": "react-scripts test --env=jsdom",
"test": "jest",
"eject": "react-scripts eject"
},
"devDependencies": {
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"enzyme-to-json": "^3.3.3",
"jest": "^22.4.3"
}
}
when I run jest --updateSnapshot I get:
command not found: jest
but jest is installed.
Jest is installed, but is likely in your ./node_modules/.bin directory. You can append that to your command ./node_modules/.bin/jest --updateSnapshot. Since you already have jest as a scripts command in your package.json you can also run it with npm test -- --updateSnapshot. npm automatically adds ./node_modules/.bin to your path.
update: Newer versions of yarn will resolve node module bin scripts, so you can also just run yarn jest {cmd} and it should work.
I ran into similar issue. I fixed it by installing jest globally.
npm install -g jest
You need to run it this way :
./node_modules/.bin/jest
or run npm test
Install the Jest command-line interface (Jest CLI):
npm install --save-dev jest-cli
Then run the jest command. Working for me in a linux instance by docker on Windows 10.
I was getting zsh: command not found: jest after installing jest and trying to use the command jest. The solution that worked for me was running npx jest
A way to solve the error is to use the "npx" command.
npx jest --version
npx jest --init
In my case, npm didn't install the jest command for some reason.
To fix this:
I deleted the node_modules/jest directory
Re-ran npm install and got the jest command installed.
try using the command
npx jest <folder>
I ran into the same problem. I tried multiple solutions and this worked.
I also have jest CLI installed
you can install it by using this command in your shell
npm install --save-dev jest-cli
just use command
npm test or npm t
Removing node_modules and running npm install again fixed this for me
Also the "new" npm ci command can fix this as it deletes (or clears) node modules and performs a clean install each time, plus it's faster compared to manually deleting node_modules and re-installing
My situation was caused by my git pipeline. I wasn't caching node_modules nor was I caching untracked files.
Ultimately I added
cache:
# untracked: true
key:
files:
- package-lock.json
paths:
- node_modules
to my pipeline .yml and violá
Note
you can either use path OR untracked, find out more about each to see what works best for you
Just reload your bash config file after install jest:
source ~/.bashrc # on linux ?
source ~/.bash_profile # on macOs
Jest will be not recognized but executed with npx jest automatically
I use yarn. Adding jest and jest-cli to node_modules did not make any difference with my attempts to run tests like jest mytest.test.js. In addition to mentioned steps, the following helped to get it running:
yarn jest mytest.test.js
you can run ln -s ./node_modules/.bin/jest jest
and then run jest --init it will work. Or you can install jest cli with npm install --save-dev jest-cli and then run jest --init it will also work.
In my case, I was trying to install jest with yarn on a pipeline to run tests and since I had jest installed as a devDependency it wasn't installing on yarn install.
I found this bug on GitHub https://github.com/yarnpkg/yarn/issues/2739 that it seems that Yarn will not install devDependencies when NODE_ENV=production.
I just needed to change the NODE_ENV and after that, it was working, otherwise, run it like this:
yarn install --production=false
Faced the same issue. But it was due to the wrong node version. If you use the latest jest v29, you need Node version 14 or higher.
You can run the test using npx jest [parameters]. npx is the package runner. It will help you execute a locally installed package.
Had the same issue and was able to solve it by running npm install
Alternatively, just add jest module to package.json dependencies.
{
"dependencies": {
...
"jest": "^29.3.1",
...
}
}
I'm trying to use Travis CI to run my mocha + chai tests for the first time and I cant seem to figure out why this is happening.
When the Travis build runs:
mocha
sh: 1: mocha: not found
The command "npm test" exited with 1.
.travis.yml
language: node_js
node_js:
- "8"
package.json (not the whole thing)
"scripts": {
"test": "mocha"
},
"Dependencies": {
"mocha": "3.4.2",
"chai": "4.0.2"
},
I also tried the test being: "test": "./node_modules/.bin/mocha" but that didn't work either.
Thanks for your help!
EDIT:
I'm not the smartest.... had Dependencies instead of dependencies (left over from when it said devDependencies!)
The way I solved this is that I went to the menu button of travis, then I went to cache, and I cleared it... when you cleared the cache on travis, it tries to download it, then it fails, then does npm install to install all the dependancies again.