Skip to content
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

Jest to node test runner #103

Merged
merged 22 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3d357eb
ci: rename dependabot action to correct spelling
connorjburton Sep 17, 2023
b436bb2
ci: add run tests script for compiling ts and tear down
connorjburton Sep 17, 2023
c4a98c4
build: add test specific tsconfig that extends from base
connorjburton Sep 17, 2023
6ba7ff9
build: use latest tsconfig.json with simpler options
connorjburton Sep 17, 2023
7d897f4
docs: change package description
connorjburton Sep 17, 2023
03a1ef3
build(deps): remove jest, ts-node, ts-jest and upgrade typescript
connorjburton Sep 17, 2023
6ebfd8f
build: set package to type module
connorjburton Sep 17, 2023
26653d9
build: use prepack instead of prepare for tsc compilation
connorjburton Sep 17, 2023
0c09fe1
test: use new run-tests bash script to run tests rather than jset
connorjburton Sep 17, 2023
a57e040
docs: add info and instructions for tests
connorjburton Sep 17, 2023
d6893f2
chore: clean up gitignore and add tests/compiled folder
connorjburton Sep 17, 2023
28bc9ec
ci: run tests via new run-tests script and check coverage
connorjburton Sep 17, 2023
0268aaf
fix: use file extension to meet ts requirementnts
connorjburton Sep 17, 2023
0671f48
test: swap from jest to node test runner and assertion libraries
connorjburton Sep 17, 2023
a5c0a09
ci: use node v20 for ci action
connorjburton Sep 17, 2023
4c8c349
style: package.json prettier style
connorjburton Sep 17, 2023
3b0daa7
ci: make run-tests executable by gh actions
connorjburton Sep 17, 2023
89e97a6
chore: debug gh action for exit code
connorjburton Sep 17, 2023
ebde2b4
Revert "chore: debug gh action for exit code"
connorjburton Sep 17, 2023
730ecab
ci: upgrade actions to v3
connorjburton Sep 17, 2023
46017cc
build: ignroe scripts when packing for release
connorjburton Sep 17, 2023
fe02bb8
ci: print test-out to stdout before checking with awk
connorjburton Sep 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,39 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
node-version: [20.x]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
# - run: yarn audit
- run: yarn
- run: yarn lint:check
- run: yarn style:check
- run: yarn test --coverage
- name: Install Dependencies
run: yarn
- name: Check Linting
run: yarn lint:check
- name: Check Coding Style
run: yarn style:check
- name: Test with Coverage
run: |
# Check coverage, if coverage is below 100% for any files exit & print a message
yarn test --experimental-test-coverage > test-output.txt

cat test-output.txt

grep "tests/compiled/src" test-output.txt | awk -F'|' '
BEGIN {
status=0;
}
{
for (i=2; i<NF; i++) {
if ($i+0 < 100) {
status=1;
printf "%s has less than 100%% coverage in all criterias\n", $1;
exit status;
}
}
}
END {
exit status;
}'
6 changes: 2 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: "lts/*"
- name: Install dependencies
Expand Down
43 changes: 1 addition & 42 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,23 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next

# dist
/dist

# Can be removed once project is fully converted to typescript and everything can compile to the /dist folder
benchmark/*.js
tests/e2e.test.js
tests/compiled
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ benchmark
.gitattributes
.eslintrc.js
.eslintignore
commitlint.config.js
commitlint.config.js
scripts
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/dist
/coverage
/.nyc_output
/node_modules
/docs
CHANGELOG.md
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# HMAC Auth Express

[![npm](https://img.shields.io/npm/v/hmac-auth-express)](https://www.npmjs.com/package/hmac-auth-express)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/connorjburton/hmac-auth-express/Node.js%20CI)](https://github.com/connorjburton/hmac-auth-express/actions/workflows/ci.yml)
[![GitHub](https://img.shields.io/github/license/connorjburton/hmac-auth-express)](https://github.com/connorjburton/hmac-auth-express/blob/master/LICENSE)

This package provides [Express](https://expressjs.com/) middleware for [HMAC](https://en.wikipedia.org/wiki/HMAC) authentication.
Expand Down Expand Up @@ -243,7 +244,7 @@ You can provide your own order function with your own implementation if you wish

This package does not support plain text, form or multi part POST bodies and is primarily intended to be used for GET requests and JSON bodies. [Plain text support](https://github.com/connorjburton/hmac-auth-express/issues/61) is planned.

Be mindful of what algorithm you choose to use, this package will not stop you attempting to use an algorithm that is not supported by OpenSSL. [See the Node.js website for more information](https://nodejs.org/api/crypto.html#support-for-weak-or-compromised-algorithms).
Be mindful of what algorithm you choose to use, this package will not stop you attempting to use an algorithm that is not supported by OpenSSL. [See the Node.js website for more information](https://nodejs.org/en/knowledge/cryptography/how-to-use-crypto-module/#hash-algorithms-that-work-with-crypto).

## Performance

Expand Down Expand Up @@ -277,6 +278,16 @@ console.log(`HMAC is ${hmac}`);

In the future we may expose the core middleware logic standalone, so that you can inject this yourself in any middleware function signature of your choosing.

## Tests

100% code coveage is a hard requirement of this package, enforced with automated coverage checks. The [GitHub action](https://github.com/connorjburton/hmac-auth-express/blob/master/.github/workflows/ci.yml) will use `awk` to ensure that the action fails if any coverage criteria is less than 100% for any source file.

Running the tests requires Node.js v20 or higher.

To run tests run `yarn test`

To run tests with coverage run `yarn test --experimental-test-coverage`

## FAQs

_Why is HMAC uppercase?_ HMAC is an acronym for [hash-based message authentication code](https://en.wikipedia.org/wiki/HMAC). You can import the package as below if you need to conform to style conventions.
Expand Down
2 changes: 1 addition & 1 deletion benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
performance,
} from "perf_hooks";

import { HMAC } from "./../src/index";
import { HMAC } from "./../src/index.js";

interface TransformedEntry {
operations: number;
Expand Down
47 changes: 8 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "hmac-auth-express",
"type": "module",
"version": "8.3.4",
"description": "This package provides Express middleware for HMAC authentication",
"description": "Express middleware for HMAC authentication",
"keywords": [
"nodejs",
"javascript",
Expand All @@ -19,13 +20,13 @@
"main": "./dist/index.js",
"typings": "./dist",
"scripts": {
"prepare": "tsc",
"prepack": "tsc",
"docs": "yarn typedoc src",
"lint:check": "yarn eslint",
"lint:fix": "yarn eslint --fix",
"style:check": "yarn prettier . --check",
"style:fix": "yarn prettier . --write",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./tests",
"test": "./scripts/run-tests.sh",
"benchmark": "ts-node ./benchmark/index.ts"
},
"repository": {
Expand All @@ -38,34 +39,6 @@
"url": "https://github.com/connorjburton/hmac-auth-express/issues"
},
"homepage": "https://github.com/connorjburton/hmac-auth-express#readme",
"babel": {
"targets": {
"node": "latest"
}
},
"jest": {
"testEnvironment": "node",
"preset": "ts-jest",
"extensionsToTreatAsEsm": [
".ts"
],
"transform": {
"\\.ts?$": [
"ts-jest",
{
"useESM": true
}
]
},
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
},
"release": {
"branches": [
"master"
Expand Down Expand Up @@ -96,21 +69,17 @@
"@semantic-release/git": "^10.0.1",
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/express": "^4.17.17",
"@types/jest": "^29.4.0",
"@types/node": "^18.13.0",
"@types/node": "^20.6.0",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"eslint": "^8.34.0",
"express": "^4.18.2",
"got": "11.8.6",
"got": "^13.0.0",
"husky": "^8.0.3",
"jest": "^29.4.2",
"prettier": "^2.8.4",
"semantic-release": "^20.1.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typedoc": "^0.23.25",
"typescript": "^4.9.5"
"typedoc": "^0.25.1",
"typescript": "5.2.2"
},
"peerDependencies": {
"express": "^4.0.0"
Expand Down
8 changes: 8 additions & 0 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e

trap "rm -rf ./tests/compiled" EXIT

yarn tsc --project tests
node --test $1 ./tests/compiled/tests/**/*.test.js
2 changes: 1 addition & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash, createHmac, Hmac } from "crypto";
import { Order, UnknownObject, defaults } from "./index";
import { Order, UnknownObject, defaults } from "./index.js";

export interface GenerateOptions {
order?: Order;
Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import crypto from "crypto";
import { Request, RequestHandler, NextFunction } from "express";

import validateArguments from "./validateArguments";
import AuthError from "./errors";
import generate, { GenerateOptions } from "./generate";
import order from "./order";
import validateArguments from "./validateArguments.js";
import AuthError from "./errors.js";
import generate, { GenerateOptions } from "./generate.js";
import order from "./order.js";

const crypto = await import("node:crypto");

export { AuthError, generate, GenerateOptions, order };

Expand Down
2 changes: 1 addition & 1 deletion src/order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UnknownObject } from "./index";
import { UnknownObject } from "./index.js";

export default function order(o: UnknownObject): UnknownObject {
if (typeof o !== "object" || o === null || Array.isArray(o)) {
Expand Down
4 changes: 2 additions & 2 deletions src/validateArguments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import crypto from "crypto";
import { Options, DynamicSecret } from "./index.js";

import { Options, DynamicSecret } from "./index";
const crypto = await import("node:crypto");

export default function (secret: DynamicSecret, options: Options): void {
if (!secret || (typeof secret !== "string" && typeof secret !== "function")) {
Expand Down
Loading