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

HPC-7811 set up test environment #217

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ jobs:
run: npm run check-types
- name: Linting checks
run: npm run lint
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout repository
uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install packages
run: yarn install --frozen-lockfile
- name: Run Unit Tests
run: ./bin/test.sh
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ package-lock.json
.smbdelete*
test-data/*
.env
.vscode
*.sublime-project
*.sublime-workspace
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Jest Tests",
"type": "node",
"preLaunchTask": "start-containers",
"request": "launch",
"runtimeArgs": [
"--inspect-brk",
"${workspaceRoot}/node_modules/.bin/jest",
"--runInBand"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
13 changes: 13 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "start-containers",
"type": "shell",
"command": "${workspaceRoot}/bin/test.sh",
"args": ["-oc"]
}
]
}
94 changes: 94 additions & 0 deletions bin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env bash

root=$(pwd)

# Global variables
USAGE="Usage: $(basename "$0") [options] [-- [options]].

Options:
-oc, --only-containers: Only start Docker containers
-sc, --stop-containers: Stop Docker containers
-k, --keep: Keep Jest running after the completion of the test suites
-c: Run tests with coverage
-h, --help: Show this help message
--: Pass extra options"
KEEP=0
FORCE_STOP_JEST='--forceExit'
ONLY_CONTAINERS=0
STOP_CONTAINERS=0
COMMAND_ARGS=''

function moveToTestDir {
cd ${root}/tests
}

function moveToRootDir {
cd ${root}
}

# Obtain options
while [ "$1" != "" ]; do
case $1 in
-oc | --only-containers ) ONLY_CONTAINERS=1
;;
-sc | --stop-containers ) STOP_CONTAINERS=1
;;
-k | --keep ) KEEP=1
;;
-c) shift
COMMAND_ARGS="${COMMAND_ARGS} --coverage"
;;
-h | --help ) echo "$USAGE"
exit
;;
--) shift
while [ "$1" != "" ]; do
COMMAND_ARGS="${COMMAND_ARGS} -- $1"
shift
done
;;
* ) echo "$USAGE"
exit 1
esac
shift
done

# ONLY_CONTAINERS must be 1 and STOP_CONTAINERS must be 0
if [ $ONLY_CONTAINERS -eq 1 ] && [ "$STOP_CONTAINERS" -eq 1 ]; then
echo "$USAGE"
exit 1
fi

# STOP_CONTAINERS is a final option
if [[ $STOP_CONTAINERS -eq 1 ]]; then
echo "Stopping Docker containers"
moveToTestDir
docker compose down
exit 0
fi


echo "Starting Docker containers"
moveToTestDir
docker compose up -d

if [ $ONLY_CONTAINERS -eq 1 ]; then
exit 0
fi

# Run tests
echo "Running tests"
moveToRootDir

if [ $KEEP -eq 0 ]; then
FORCE_STOP_JEST=''
fi

yarn jest "$COMMAND_ARGS" $FORCE_STOP_JEST

if [ $KEEP -eq 0 ]; then
# Stop Docker containers
echo "Stopping Docker containers"
moveToTestDir
docker compose down
fi
20 changes: 20 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
testMatch: ['<rootDir>/**/*.spec.ts'],
coveragePathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/test/'],
clearMocks: true,
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['node_modules/(?!(@unocha)/)'],
modulePathIgnorePatterns: ['<rootDir>/test/'],
setupFilesAfterEnv: ['<rootDir>/tests/test-environment-setup.ts'],
testTimeout: 100_000,
};

export default config;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
"@hapi/hapi": "^20.2.1",
"@types/bunyan": "^1.8.8",
"@types/hapi__hapi": "^20.0.9",
"@types/jest": "^29.5.12",
"@types/node": "^20.8.10",
"@types/pg": "^8.11.3",
"@unocha/hpc-repo-tools": "^4.0.0",
"eslint": "^8.52.0",
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.0.2",
"prettier": "3.0.3",
"ts-jest": "^29.1.2",
"ts-node-dev": "^2.0.0"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions src/data-providers/postgres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CONFIG } from '../../../config';
/**
* Initialize a new Postgres provider
*/
export async function createDbConnetion() {
export async function createDbConnection() {
const knex = Knex({
client: 'pg',
connection: CONFIG.db.connection,
Expand All @@ -28,4 +28,4 @@ export async function createDbConnetion() {
}
}

export default { createDbConnetion };
export default { createDbConnection };
4 changes: 2 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import v4Models from '@unocha/hpc-api-core/src/db';
import { CONFIG } from '../config';
import { getTokenFromRequest } from './common-libs/auth';
import { initializeLogging } from './common-libs/logging';
import { createDbConnetion } from './data-providers/postgres';
import { createDbConnection } from './data-providers/postgres';

declare module '@hapi/hapi' {
interface ServerApplicationState {
Expand All @@ -38,7 +38,7 @@ async function startServer() {
container: Container, // Register the 3rd party IOC container
});

const dbConnection = await createDbConnetion();
const dbConnection = await createDbConnection();

const hapiServer = Hapi.server({
port: CONFIG.httpPort,
Expand Down
11 changes: 11 additions & 0 deletions tests/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
db:
image: postgres:14.8-alpine3.18
ports:
- 6432:5432
environment:
- POSTGRES_DB=hpc-test
- POSTGRES_USER=postgres
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- ./migration/schema-2024-03-13.sql:/docker-entrypoint-initdb.d/init.sql
Loading
Loading