How to run jest by lerna in github actions - jestjs

I am trying to run jest for a monorepo project maintained by lerna in the github actions.
name: Run Unit Test
on:
push:
branches:
- master
- dev
pull_request:
branches:
- master
- dev
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Cache node modules
uses: actions/cache#v2
env:
cache-name: cache-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Dependencies
run: npm -v && npm install
- name: Run Unit Test
run: npm run test
- name: Coveralls
uses: coverallsapp/github-action#master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
I got jest: not found error when run test in github actions.
you can see the error logs here. But when I run npm install and npm run test in my own macOs, everything works fine.
and this is my package.json of root:
{
"private": true,
"description": "a progressive micro frontend library",
"workspaces": [
"packages/*"
],
"scripts": {
"postinstall": "lerna bootstrap",
"bootstrap": "lerna bootstrap",
"clean": "lerna clean",
"test": "lerna run test --stream",
"build": "lerna run build --stream",
"commit": "git cz",
"lint": "lerna run lint --stream",
"publish": "lerna publish",
"docs": "docsify serve docs"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ObviousJs/obvious.git"
},
"author": "Philip Lau",
"license": "MIT",
"bugs": {
"url": "https://github.com/ObviousJs/obvious/issues"
},
"homepage": "https://github.com/ObviousJs/obvious#readme",
"publishConfig": {
"access": "public"
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
},
"devDependencies": {
"#rollup/plugin-commonjs": "20.0.0",
"#rollup/plugin-node-resolve": "13.0.4",
"#typescript-eslint/eslint-plugin": "4.31.0",
"#typescript-eslint/parser": "4.31.0",
"commitizen": "4.2.4",
"cz-conventional-changelog": "3.3.0",
"docsify-cli": "4.4.3",
"eslint": "7.32.0",
"eslint-config-standard": "16.0.3",
"eslint-plugin-import": "2.24.2",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "5.1.0",
"husky": "4.3.8",
"lerna": "4.0.0",
"rollup": "2.56.3",
"rollup-plugin-typescript2": "0.30.0"
},
"husky": {
"hooks": {
"pre-commit": "npm run lint && npm run build && git add ./"
}
}
}
this is the package.json of sub project:
{
"name": "#obvious/core",
"version": "0.4.0",
"description": "a progressive micro front framework",
"main": "./dist/index.umd.js",
"module": "./dist/index.es.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "rollup -c rollup.config.js",
"lint": "eslint --fix --ext .ts,.js test",
"test": "jest --coverage"
},
"author": "Philip Lau",
"license": "MIT",
"dependencies": {
"#vue/reactivity": "3.2.10",
"tslib": "2.3.1"
},
"devDependencies": {
"#types/jest": "27.0.1",
"jest": "27.1.1",
"nock": "13.1.3",
"node-fetch": "2.6.2",
"ts-jest": "27.0.5",
"typescript": "4.4.3"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ObviousJs/obvious-core.git"
},
"bugs": {
"url": "https://github.com/ObviousJs/obvious-core/issues"
},
"homepage": "https://github.com/ObviousJs/obvious-core#readme"
}
my github repo address is https://github.com/ObviousJs/obvious-core.
sorry for my pool english but I really need some help (orz

I have zero knowledge about this, but for a temporary answer, what worked for me (when I had the same error with jest) was adding,
- run: lerna bootstrap --no-ci
before running my npm test command in my workflow config. Thus I ended up with a workflow like this:
on:
pull_request:
branches: [ master ]
jobs:
test_pull_request:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
# fix issue with lerna and clean installs
- run: lerna bootstrap --no-ci
- run: npm test

I finally solved this problem by setting
useWorkspaces: false
in lerna.json
the reason is that npm6.x doesn't support workspace. And the npm version of github-action runner is just v6, so the workspaces in package.json is useless, so we should set useWorkspaces in lerna.json to false

Related

Why do i get an error react-scripts: not found on github actions CI

I have the following package.json files for my app:
Client folder:
``{
"name": "nasa-fe",
"version": "1.0.1",
"private": true,
"dependencies": {
"arwes": "^1.0.0-alpha.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.8.0",
"react-scripts": "^5.0.1"
},
"overrides": {
"nth-check#1.0.2": "2.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "set BUILD_PATH=../server/public&& react-scripts build",
"test": "react-scripts test --passWithNoTests",
"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"
]
}
}
Server folder:
{
"name": "nasa-project-api",
"version": "1.0.0",
"description": "Nasa mission control api",
"main": "src/server.js",
"scripts": {
"test": "jest --detectOpenHandles",
"test-watch": "jest --watch",
"watch": "nodemon src/server.js",
"start": "node src/server.js",
"cluster": "pm2 start src/server.js -i max"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"csv-parse": "^5.3.3",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"mongoose": "^6.8.4",
"mongose": "^0.0.2-security",
"morgan": "^1.10.0",
"pm2": "^5.2.2",
"react-router-dom": "^6.8.0",
"react-scripts": "^5.0.1"
},
"devDependencies": {
"jest": "^29.3.1",
"nodemon": "^2.0.20",
"supertest": "^6.3.3"
}
}
and root folder:
{
"name": "nasa-exploration",
"version": "1.0.0",
"description": "This a full stack project",
"main": "index.js",
"scripts": {
"deploy": "set BUILD_PATH=../server/public && npm run build --prefix client && npm start --prefix server",
"deploy-cluster": "npm run build --prefix client && npm run cluster --prefix server",
"server": "npm run watch --prefix server",
"client": "npm start --prefix client",
"watch": "npm run server & npm run client",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yayoamigo/Nasa-exploration.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/yayoamigo/Nasa-exploration/issues"
},
"homepage": "https://github.com/yayoamigo/Nasa-exploration#readme",
"dependencies": {
"arwes": "^1.0.0-alpha.5",
"axios": "^1.2.4",
"dotenv": "^16.0.3",
"react-dom": "^18.2.0",
"react-router-dom": "^6.8.0",
"react-scripts": "^5.0.1"
}
}
as you can see I have react-scripts in every folder but get this error regardless, I tried to reinstall the dependencies, change the scripts and nothing. The weird things is when I npm run build on my PC it works
Just checked the Workflow in your repo and I see that you are running the following commands:
...
- run: npm install
- run: npm run build --prefix client
...
According to this, the npm run build --prefix client will run the build under the client directory, but the first command installs dependencies under the root folder. The build just can't find dependencies since it's not installed in the client directory.
So you need to change your install command to use the prefix also:
npm install --prefix client
To read more about prefixes, visit the official docs.
Alternatively, you can use the working-directory keyword to specify the working directory of where to run the command:
- name: Install
working-directory: client
run: npm install
- name: Build
working-directory: client
run: npm run build

GitHub actions: NPM publish fails with ERR! code ENEEDAUTH

I've attempted to implement the official guide to publishing and installing a package with GitHub Actions: Authenticating to package registries with granular permissions
Fails with:
npm ERR! code ENEEDAUTH
npm ERR! need auth This command requires you to be logged in to https://npm.pkg.github.com
npm ERR! need auth You need to authorize this machine using `npm adduser`
package.json
{
"name": "#charneykaye/banana",
"version": "4.0.6",
"repository": "git#github.com:charneykaye/banana",
"description": "made by artists in a new algorithmic medium",
"bin": {
"banana": "./lib/index.js"
},
"author": "Charney Kaye <charney#xj.io>",
"license": "MIT",
"scripts": {
"start": "nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/index.ts",
"start:windows": "nodemon --watch 'src/**/*.ts' --exec \"npx ts-node\" src/index.ts",
"create": "npm run build && npm run test",
"banana": "npx ts-node ./src/index.ts",
"test": "tsc -p . && jest --coverage --verbose --runInBand"
},
"dependencies": {
"commander": "^10.0.0",
"figlet": "^1.5.2",
"octokit": "^1.8.0"
},
"devDependencies": {
"#babel/core": "^7.20.12",
"#babel/preset-env": "^7.20.2",
"#babel/preset-typescript": "^7.18.6",
"#jest/globals": "^29.4.1",
"#types/jest": "^29.4.0",
"#types/node": "^18.11.18",
"babel-jest": "^29.4.1",
"jest": "^29.4.1",
"nodemon": "^2.0.20",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"testMatch": [
"**/__tests__/**test.ts",
"**/__tests__/**test.tsx"
]
}
}
.github/workflow/ci.yml
name: "CI Build & Publish"
on:
push:
branches:
- main
jobs:
CI:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup Node.js
uses: actions/setup-node#v3
with:
node-version: 18.14
cache: 'npm'
- name: Install npm packages
run: npm install
- name: Unit tests
run: npm test
- name: Build Banana
run: npm run banana -- --build --env prod
- uses: actions/upload-artifact#v3
with:
name: banana
path: ./build/
- name: Publish NPM package
run: npm publish
.npmrc
#charneykaye:registry=https://npm.pkg.github.com
Needed to do a connect two more dots to get the token all the way to npm publish
Replace .npmrc with
//npm.pkg.github.com/:_authToken=${NPM_CONFIG_TOKEN}
#charneykaye:registry=https://npm.pkg.github.com
always-auth=true
and the last action in .github/workflow/ci.yml with
- name: Publish NPM package
run: npm publish
env:
NPM_CONFIG_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Error: getaddrinfo EAI_AGAIN undefined at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26)

I ran mocha tests in node.js. The following are the details related to it [includes the error]
github actions workflow: default node js github actions
name: Node.js CI
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test
package.json
{
"name": "node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha ./**/*.test.js --exit",
"dev": "nodemon server.js",
"start": "node server.js",
"debug": "nodemon --inspect=0.0.0.0:9229 server.js"
},
"keywords": [],
"author": "Pragati Bhattad <pragatibhattad1610#gmail.com>",
"license": "ISC",
"dependencies": {
"bcrypt": "^5.0.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.18.1",
"express-rate-limit": "^5.2.3",
"express-validator": "^6.14.2",
"jsonpath": "^1.1.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.10.0",
"pg": "^8.5.1",
"pg-hstore": "^2.3.3",
"sequelize": "^6.3.5",
"uuid4": "^2.0.2",
"winston": "^3.3.3"
},
"devDependencies": {
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"mocha": "^7.2.0",
"nodemon": "^2.0.6"
}
}
Error:
Error: getaddrinfo EAI_AGAIN undefined
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26)
This error is under each test case
Kindly help me out with this query
The tests use localhost as the url to test on ..I have used the url as http://localhost:8080. If that is what is causing the issue on github actions
The following solutions didn't work for my case
What's the cause of the error 'getaddrinfo EAI_AGAIN'?
https://github.com/fastify/help/issues/592

Github Action - Error: Process completed with exit code

I am trying to setup Github actions to npm publish my package. But I got this error When I move on execute
My workflows/publish.yml file looks like the following:
name: publish
on:
push:
branches: [ main ]
jobs:
release:
name: publish
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout#v2.3.4
- name: Setup Node.js environment
uses: actions/setup-node#v2.2.0
with:
node-version: 14
registry-url : https://registry.nmpjs.org
- name: publish
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}}
Your package.json file is broken.
{
"name": "#sakibb019/npx-card",
"version": "1.0.0",
"description": "",
"main": "card.js",
"scripts": {
"dev": "nodemon card.js"
},
"keywords": [],
"author": "",
"repository": {
"url": "git://github.com/sakibb019/test.git"
},
"license": "ISC",
"dependencies": {
"boxen": "^5.0.1",
"chalk": "^4.1.1",
"clear": "^0.1.0",
"inquirer": "^8.1.0"
}
}
I've added the missing braces, still, you would need to find a complete package JSON file. There should be more content after "inquirer": "^8.1.

npm no such file or directory when trying to run 'npm run deploy'

I get this error:
no such file or directory, stat 'C:\Users\nessa\VS Code\React-Django\website\frontend\build'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! frontend#1.0.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the frontend#1.0.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
when trying to run npm run deploy.
This is my package.json file located in my .\website\frontend
{
"name": "frontend",
"version": "1.0.0",
"private": true,
"description": "",
"main": "index.js",
"scripts": {
"start": "react-scripts start",
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.13.13",
"#babel/preset-env": "^7.13.12",
"#babel/preset-react": "^7.13.13",
"babel-loader": "^8.2.2",
"css-loader": "^5.2.0",
"gh-pages": "^3.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"style-loader": "^2.0.0",
"webpack": "^5.28.0",
"webpack-cli": "^4.6.0"
},
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.13.0",
"#material-ui/core": "^4.11.3",
"#material-ui/icons": "^4.11.2",
"#material-ui/lab": "^4.0.0-alpha.57",
"react-material-ui-carousel": "^2.2.4",
"react-router-dom": "^5.2.0",
"react-speech": "^1.0.2"
}
}
I've been running npm run dev from the same location all the time without issues. I'm trying to host my react app on Github. I tried reinstalling gh-pages in case a file didn't properly install but am having the same problems. What should I do to resolve this?
Edit: I tried to do what was said here, but still didn't work. Here's my webpack.config.js if that helps.
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './static/frontend'),
filename: '[name].js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
optimization: {
minimize: true,
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
}),
],
}
I also changed "build": "webpack --mode production" in my package.json scripts to "build": "webpack --mode development", in order to run npm run build, but the problem still isn't fixed.
This was my error:
ENOENT: no such file or directory, stat 'C:\Users...\dist'
So I added
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
on package.json in "scripts".
And run the command npm install gh-pages --save-dev in terminal.
Finishing with npm run deploy.
After you add predeploy, deploy in script in JSON file.
run : npm run build
run : npm run deploy

Resources