0

I have been trying to get rid of husky/lint-staged trying to learn how to write a script that would do some automation for pre-commit operations.

I have this script file, that runs eslint and jest.

Eslint runs smoothly, but I am not able to run jest through it.

package.json file

{
    "name": "gri-web",
    "version": "0.0.0",
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged",
            "pre-push": "npm run test:ci"
        }
    },
    "scripts": {
        "prepare": "husky install",
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "jest --passWithNoTests --silent --noStackTrace --runInBand",
        "lint": "tsc --noEmit && eslint . --ext js,ts,json --quiet --fix",
        "e2e": "ng e2e",
        "test:verbose": "jest --passWithNoTests --runInBand",
        "test:unit": "npm test -- --watch -c jest-unit-config.js",
        "test:integration": "npm test -- --watch -c jest-integration-config.js",
        "test:staged": "npm test -- --findRelatedTests",
        "test:ci": "npm test -- --coverage"
    },
    "prettier": {
        "singleQuote": true,
        "trailingComma": "none",
        "endOfLine": "auto"
    },
    "private": true,
    "dependencies": {
        "@angular/animations": "^11.0.0",
        "@angular/cdk": "^11.0.0",
        "@angular/common": "^11.0.0",
        "@angular/compiler": "^11.0.0",
        "@angular/compiler-cli": "^11.0.0",
        "@angular/core": "^11.0.0",
        "@angular/forms": "^11.0.0",
        "@angular/platform-browser": "^11.0.0",
        "@angular/platform-browser-dynamic": "^11.0.0",
        "@angular/router": "^11.0.0",
        "@auth0/angular-jwt": "^4.1.2",
        "@fortawesome/fontawesome-free": "^5.12.1",
        "chart.js": "^2.9.3",
        "file-saver": "^2.0.2",
        "moment": "^2.24.0",
        "primeflex": "^1.0.0-rc.1",
        "primeicons": "^2.0.0",
        "primeng": "^8.0.0",
        "rxjs": "^6.5.3",
        "tslib": "^1.14.1",
        "xlsx": "^0.16.0",
        "zone.js": "^0.10.3"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "^0.1102.9",
        "@angular/cli": "^11.2.9",
        "@angular/language-service": "~8.2.14",
        "@types/chart.js": "^2.9.19",
        "@types/jest": "^26.0.24",
        "@types/node": "^8.9.5",
        "@typescript-eslint/eslint-plugin": "^4.28.3",
        "codelyzer": "^6.0.0",
        "eslint": "^7.30.0",
        "eslint-config-prettier": "^8.3.0",
        "eslint-config-standard-with-typescript": "^20.0.0",
        "eslint-plugin-import": "^2.23.4",
        "eslint-plugin-node": "^11.1.0",
        "eslint-plugin-prettier": "^3.4.0",
        "eslint-plugin-promise": "^4.3.1",
        "git-commit-msg-linter": "^3.2.6",
        "husky": "^7.0.0",
        "jest": "^26.6.0",
        "jest-preset-angular": "^8.4.0",
        "lint-staged": "^10.5.4",
        "prettier": "2.3.2",
        "prettier-eslint": "^12.0.0",
        "protractor": "~5.4.0",
        "ts-jest": "^26.5.6",
        "ts-node": "~7.0.0",
        "typescript": "^4.0.6"
    },
}

script file pre-commit

#!/bin/sh
linter_exit_code=1
jest_exit_code=1
git config advice.addEmptyPathspec false
echo "Getting staged files..."
all_ts_files=$(git diff --cached --diff-filter=d --name-only | grep .ts$)
echo "Running Eslint..." 
./node_modules/.bin/eslint $all_ts_files --quiet --fix 
linter_exit_code=$?
echo "Running Jest test..."
./node_modules/.bin/jest --config ./jest.config.js
jest_exit_code=$?
git add -f $all_ts_files
if [ $linter_exit_code -ne 0 ] || [$jest_exit_code -ne 0] 
then
  echo "❌ Errors occured in Linter or Jest ( ͡ಥ ͜ʖ ͡ಥ)"
  exit 1
else
  echo "✔ All passed [̲̅$̲̅(̲̅ ͡° ͜ʖ ͡°̲̅)̲̅$̲̅]"
  exit 0
fi

I have tried all kind of things... It seems that when I try to run jest from this file, it looks for the package.json and it fails, as my jest configuration is set in a .js file.

I have tried to following:

=> move my jest.config.js file to package.json file:

If I do this, the whole app stops working and I cannot even run my local server (it seems that the configs only work in the .js file)

=> try to point to my jest.config.json file on the script file:

it seems to be ignored and it does not work

The error is a parsing error, that occurs with or without having my jest configurations inside the package.json file.

Error:

Error: Cannot parse C:\Users\apjv\projects\guri-frontend-angularjs\package.json as JSON: Unexpected token } in JSON at position 2887
    at Object.worker (C:\Users\apjv\projects\guri-frontend-angularjs\node_modules\jest-haste-map\build\worker.js:146:13) 

jest.config.js file:

module.exports = {
    preset: 'jest-preset-angular',
    setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
    testPathIgnorePatterns: [
        '<rootDir>/node_modules/',
        '<rootDir>/dist/',
        '<rootDir>/src/test.ts',
    ],
    globals: {
        'ts-jest': {
            tsConfig: '<rootDir>/tsconfig.spec.json',
            stringifyContentPathRegex: '\\.html$',
            astTransformers: {
                before: [
                    'jest-preset-angular/build/InlineFilesTransformer',
                    'jest-preset-angular/build/StripStylesTransformer',
                ],
            },
        },
    },
};

I am also not managing to execute the exit code for jest in case of an error...

|| [$jest_exit_code -ne 0] 

with the following error:

git-hooks/pre-commit: line 14: [1: command not found

Possible solutions I would like to discover:

=> point to my jest.config.js file in the script file? => somehow add my jest.config.js config into my package.json file without breaking everything. Could someone please inform how could I transform my config into the json file without breaking the whole thing? => some other solution...

Arp
  • 979
  • 2
  • 13
  • 30
  • you need spaces before and after the square brackets – anthony sottile Jul 23 '21 at 21:44
  • hey @AnthonySottile! Thanks for replying me here. I will test with the square brackets to see if the statement will run correctly regarding the exit code. The most annoying thing is the error with jest, while trying to parse the json file – Arp Jul 24 '21 at 03:11
  • your json is invalid, you have a trailing comma – anthony sottile Jul 24 '21 at 03:43
  • @AnthonySottile my god haha. It worked!!! All because of a comma. Thanks so much! – Arp Jul 24 '21 at 04:06
  • @AnthonySottile do you wanna add this as an answer? So I can give you a vote. Hey, how could I check if I have any staged files before running the tests? In case someone hits the commit operation without staged files? The way it is today, it runs the tests, then it tells me that there are not staged files – Arp Jul 24 '21 at 04:08
  • What’s the reason behind trying to move away from husky and lint-staged? – Shafiq Jetha Jul 24 '21 at 06:54
  • @Alioshr two typos are unlikely to help anyone else, that's why I voted to close as "not reproducible / caused by a typo" – anthony sottile Jul 24 '21 at 14:00
  • @ShafiqJetha hello there! The reason is that in my project my peer is not being able to find out the reason behind a bug when she tries to run husky. This is the error: https://stackoverflow.com/questions/66631444/how-do-i-resolve-husky-husky-sh-no-such-file-or-directory-error-when-using-h – Arp Jul 25 '21 at 05:46
  • @ShafiqJetha so I started searching for a replacement and bumped into git-hooks, how to manually write the scripts to run on staged files, etc. It turns out that writing your owns scripts has many benefits, not to mention the speed of the process. – Arp Jul 25 '21 at 05:47
  • @AnthonySottile fair enough =) thanks. I will do this. I have voted as well. Can I close it? – Arp Jul 25 '21 at 05:48

0 Answers0