-
Python
unittest
- built-in with Python
pytest
?- Flask's official testing documentation uses
pytest
- Flask's official testing documentation uses
-
JavaScript
Pre-commit hooks will allow us to run something before a commit. In our case, we will be running pre-commit hooks to format code, lint code, and run unit tests. If any of the unit tests fail, the commit will not be able to finish. If your test(s) fail, fix them until they pass, then commit your changes.
-
Python
pre-commit
- changes must be staged before pre-commit hooks can run
- configurable to run post-commits also
-
JavaScript
Commits will be linted. If your commit message does not follow conventional commits, then you will receive an error. Here is conventional commits' specifications and guidelines for commit messages. Here is their github page.
Ex:
module.exports = {
extends: ["@commitlint/config-conventional"],
rules: {
"type-enum": [
2, // level: 0 - disable, 1 - warning, 2 - error
"always", // applicable: always | never
[
"ci",
"chore",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
],
],
},
}
echo "foo: test" | npx commitlint -V
-
Python
- Set-up your virtual environment if you have not already. You can use the
requirements.txt
found in thepython
directory here. - Install
pre-commit
. Instructions here. Follow the quick start until you finish the "Quick start" section. - Install
commitlint
. Instructions here. Configurecommitlint
via.commitlintrc.json
.
# Install commitlint cli and conventional config npm install --save-dev @commitlint/{config-conventional,cli} # For Windows: npm install --save-dev @commitlint/config-conventional @commitlint/cli # Configure commitlint to use conventional config echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
- Write your code and unit tests. Refer to
/python/hello_world.py
and/python/test_hello_world.py
files. Basic explanation video forunittest
in References. - Configure your
pre-commit-config.yaml
to includecommitlint
and unit testing. Refer to thepre-commit-config.yaml
in this repo. Documentation to set-upcommitlint
inpre-commit
. Optional: add formatter (autopep8, black, etc) or linting (pylint, pylance, etc) to yourpre-commit-config.yaml
as well. This will only work if the directory has been initialized as a Git repo. - Stage your files and commit using conventional commit messages.
- Set-up your virtual environment if you have not already. You can use the
-
JavaScript
- Install the correct packages
npm install --save-dev jest husky @commitlint/cli @commitlint/config-conventional
- Set-up your scripts in your
package.json
. Make sure you have aprepare
script for installinghusky
andtest
script for unit testing viajest
.
"scripts": { "prepare": "husky install", "test": "jest" }
- Activate husky hooks. This will only work if the directory has been initialized as a Git repo.
npm run prepare
- Add test hook and commitlint to husky. Configure your
/javascript/.commitlintrc.json
to match your Python's.
# Add test hook npx husky add .husky/pre-commit "npm test" # Add commitlint npx husky add .husky/commit-msg 'npx --no-install commitlint --edit '
- Write your code and unit tests. Refer to
/javascript/hello-world.js
and/javascript/__tests__/hello-world.test.js
. - Stage your files and commit using using conventional commit messages.
- Unit testing video for Python using
unittest
Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs.
Hooks allow us to do something and/or run code (or script) before something else.