Skip to content

tests: only test changed packages #1194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 20, 2022
10 changes: 8 additions & 2 deletions .github/workflows/Ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-node@v3
with:
node-version: 16
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
. "$(dirname "$0")/_/husky.sh"

npm run style
npm run test
npm run test-changed
11 changes: 9 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ 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()`.

#### Testing

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.
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Project-Euler/test/Problem044.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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
test('if the number is greater or equal to 1', () => {
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)
})
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down