From 9df48e33214f98982eb21fa16fea5c6267def723 Mon Sep 17 00:00:00 2001 From: Fernando Dorantes Date: Wed, 31 Jan 2024 21:49:08 -0600 Subject: [PATCH] test: make a couple of assertions more precise chore: add github actions support for lint and testing pipes chore: add husky hooks --- .github/workflows/lint.yml | 28 ++++++++++++++++++++++++++++ .github/workflows/test.yml | 28 ++++++++++++++++++++++++++++ .husky/commit-msg | 26 ++++++++++++++++++++++++++ .husky/pre-commit | 2 ++ .husky/pre-push | 2 ++ package-lock.json | 22 ++++++++++++++++++++++ package.json | 6 ++++-- src/test.spec.ts | 10 +++++++--- 8 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/test.yml create mode 100644 .husky/commit-msg create mode 100644 .husky/pre-commit create mode 100644 .husky/pre-push diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..cfda691 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,28 @@ +name: Lint + +on: + pull_request: + branches: ['master'] + push: + branches: ['master'] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: ['20.10.0'] + defaults: + run: + working-directory: . + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: Install libraries + run: npm install + - name: Run linter + run: npm run lint \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..cad8d9f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: Test + +on: + pull_request: + branches: ['master'] + push: + branches: ['master'] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + node-version: ['20.10.0'] + defaults: + run: + working-directory: . + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: Install libraries + run: npm install + - name: Run test + run: npm test \ No newline at end of file diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100644 index 0000000..0935729 --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1,26 @@ +if ! head -1 "$1" | grep -qE "^(feat|fix|ci|chore|docs|test|style|refactor|perf|build|revert)(\(.+?\))?: .{1,}$"; then + echo "" >&1 + echo -e "\033[1;31m*********** Invalid Git Commit Message ***********\033[0m" >&1 + echo -e "\033[1;31m correct format: : \033[0m" >&1 + echo -e "\033[1;31m example: docs: update README \033[0m" >&1 + echo "" >&1 + echo -e "\033[1;33m Type: \033[0m" >&1 + echo -e "\033[1;33m build: \033[0m Changes that affect the build system or external dependencies (example scopes: webpack, karma, npm)" >&1 + echo -e "\033[1;33m ci: \033[0m Changes to our CI configuration files and scripts (example scopes: azuredevops.yaml, Travis, Circle)" >&1 + echo -e "\033[1;33m docs: \033[0m Documentation only changes" >&1 + echo -e "\033[1;33m feat: \033[0m A new feature" >&1 + echo -e "\033[1;33m fix: \033[0m A bug fix" >&1 + echo -e "\033[1;33m perf: \033[0m A code change that improves performance" >&1 + echo -e "\033[1;33m refactor: \033[0m A code change that neither fixes a bug nor adds a feature" >&1 + echo -e "\033[1;33m style: \033[0m Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)" >&1 + echo -e "\033[1;33m test: \033[0m Adding missing tests or correcting existing tests" >&1 + echo "" + echo -e "\033[1;33m Subject: \033[0m" >&1 + echo -e " A very short description of the change in one line." >&1 + echo "" + exit 1 +fi +if ! head -1 "$1" | grep -qE "^.{1,88}$"; then + echo "Aborting commit. Your commit message is too long." >&2 + exit 1 +fi \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 0000000..aba8440 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,2 @@ +npm run lint +npm test \ No newline at end of file diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100644 index 0000000..aba8440 --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,2 @@ +npm run lint +npm test \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 47008d1..dcf3265 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@types/mocha": "^8.2.2", "@types/node": "^15.12.1", "chai": "^4.3.4", + "husky": "^9.0.7", "mocha": "^8.4.0", "mongoose": "^6.2.10", "rimraf": "^3.0.2", @@ -794,6 +795,21 @@ "he": "bin/he" } }, + "node_modules/husky": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.0.7.tgz", + "integrity": "sha512-vWdusw+y12DUEeoZqW1kplOFqk3tedGV8qlga8/SF6a3lOiWLqGZZQvfWvY0fQYdfiRi/u1DFNpudTSV9l1aCg==", + "dev": true, + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -2519,6 +2535,12 @@ "integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==", "dev": true }, + "husky": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.0.7.tgz", + "integrity": "sha512-vWdusw+y12DUEeoZqW1kplOFqk3tedGV8qlga8/SF6a3lOiWLqGZZQvfWvY0fQYdfiRi/u1DFNpudTSV9l1aCg==", + "dev": true + }, "ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", diff --git a/package.json b/package.json index 49be49d..12476fb 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "compile": "npm run clean && tsc", "prepublishOnly": "npm run compile", "test": "mocha --config '.mocha.yml'", - "lint": "tslint src/**/*.ts" + "lint": "tslint src/**/*.ts", + "prepare": "husky" }, "repository": { "type": "git", @@ -36,6 +37,7 @@ "@types/mocha": "^8.2.2", "@types/node": "^15.12.1", "chai": "^4.3.4", + "husky": "^9.0.7", "mocha": "^8.4.0", "mongoose": "^6.2.10", "rimraf": "^3.0.2", @@ -47,4 +49,4 @@ "lodash": "^4.17.21", "luxon": "^1.27.0" } -} \ No newline at end of file +} diff --git a/src/test.spec.ts b/src/test.spec.ts index 9da731e..abc4d92 100644 --- a/src/test.spec.ts +++ b/src/test.spec.ts @@ -97,9 +97,9 @@ class Tester { assert.isOk(parsed.filter['author.firstName'] instanceof RegExp); assert.isOk(parsed.limit === 100); assert.isOk(parsed.skip === 50); - assert.isNotNull(parsed.sort); - assert.isNotNull(parsed.select); - assert.isNotNull(parsed.populate); + assert.deepEqual(parsed.sort, { timestamp: -1 }); + assert.deepEqual(parsed.select, { name: 1}); + assert.deepEqual([{ path: 'children' }], parsed.populate); } @test('should parse populate query') @@ -185,5 +185,9 @@ class Tester { qry = 'filter={"$and": ["${isActive}", {"customer": "VDF"}]}'; parsed = parser.parse(qry, preDefined); assert.isNotNull(parsed.filter['$and'][0].status); + assert.deepEqual(parsed.filter['$and'][0], { + status: { $in: ['In Progress', 'Pending'] } + }); + assert.deepEqual(parsed.filter['$and'][1], { customer: 'VDF' }); } }