diff --git a/.github/workflows/Ci.yml b/.github/workflows/Ci.yml index 8d41203145..a783e0bb97 100644 --- a/.github/workflows/Ci.yml +++ b/.github/workflows/Ci.yml @@ -12,6 +12,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: 16 @@ -20,8 +21,13 @@ jobs: - name: ๐Ÿ“ฆ Install dependencies run: npm ci - - name: ๐Ÿงช Run tests - run: npm test + - name: ๐Ÿงช Run all tests + if: ${{ github.event_name == 'push' }} + run: npm run test + + - name: ๐Ÿงช Run tests for changed files only + if: ${{ github.event_name == 'pull_request' }} + run: npm run test-changed - name: ๐Ÿ’„ Code style run: npm run style diff --git a/.husky/pre-commit b/.husky/pre-commit index 35ea7e3c08..9b70cd5958 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -2,4 +2,4 @@ . "$(dirname "$0")/_/husky.sh" npm run style -npm run test +npm run test-changed diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7386f9dca9..8415cfe5ea 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,7 +63,8 @@ should add unique value. #### Module System -We use the [ES Module](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) system, which bring an official, standardized module system to JavaScript. +We use the [ES Module](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) system, which bring an +official, standardized module system to JavaScript. It roughly means you will need to use `export` and `import` statements instead of `module.exports` and `require()`. @@ -71,7 +72,7 @@ It roughly means you will need to use `export` and `import` statements instead o Be confident that your code works. When was the last time you committed a code change, your build failed, and half of your app stopped working? Mine was last week. Writing tests for our Algorithms will help us ensure the implementations -are air tight even after multiple fixes and code changes. +are airtight even after multiple fixes and code changes. We use [Jest](https://jestjs.io/) to run unit tests on our algorithms. It provides a very readable and expressive way to structure your test code. @@ -107,6 +108,12 @@ You can also start Jest in "watch" mode: npm test -- --watchAll ``` +We also prepared a helper script that runs tests only for changed files: + +```shell +npm run test-changed +``` + This will run all tests and watch source and test files for changes. When a change is made, the tests will run again. #### Coding Style diff --git a/Project-Euler/test/Problem044.test.js b/Project-Euler/test/Problem044.test.js index bf6cccd60e..b542b3db6d 100644 --- a/Project-Euler/test/Problem044.test.js +++ b/Project-Euler/test/Problem044.test.js @@ -1,10 +1,10 @@ import { problem44 } from '../Problem044.js' describe('checking nth prime number', () => { - it('should be invalid input if number is negative', () => { + test('should be invalid input if number is negative', () => { expect(() => problem44(-3)).toThrowError('Invalid Input') }) - it('should be invalid input if number is 0', () => { + test('should be invalid input if number is 0', () => { expect(() => problem44(0)).toThrowError('Invalid Input') }) // Project Euler Condition Check @@ -12,6 +12,7 @@ describe('checking nth prime number', () => { expect(problem44(1)).toBe(5482660) }) // Project Euler Second Value for Condition Check + // FIXME skip this test for now because it runs very long and clogs up the CI & pre-commit hook test('if the number is greater or equal to 2167', () => { expect(problem44(2167)).toBe(8476206790) }) diff --git a/package-lock.json b/package-lock.json index aee3c01889..53a83cf06d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,7 +23,7 @@ "standard": "^17.0.0" }, "engines": { - "node": ">=18" + "node": ">=16.6.0" } }, "node_modules/@ampproject/remapping": { diff --git a/package.json b/package.json index 2fdd75f06c..6ab94ac09e 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "type": "module", "description": "A repository for All algorithms implemented in Javascript (for educational purposes only)", "scripts": { - "test": "jest --no-cache", + "test": "jest", + "test-changed": "jest --onlyChanged", "style": "standard", "prepare": "husky install" },