Skip to content

Commit

Permalink
Add test.sh usage
Browse files Browse the repository at this point in the history
Update README with testing usage
  • Loading branch information
manelcecs committed Nov 9, 2023
1 parent 5163f4c commit 7b6836f
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 356 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,29 @@ particular:
If you are contributing to the development of this library,
more information can be found in our [CONTRIBUTING](CONTRIBUTING.md) document.

## Testing

In order maintain a good test coverage in this project,
we must write unit test of the entities we have and the new ones we create.

The test must be written using the 'testContext' entity which stores the models,
the knex connection and the database transactions.
The aim to have the transactions is to be able to rollback the queries after the test suite.

### Running the tests

The test can be run with the bash script: bin/test.sh

This will compose up the docker containers needed to run the tests, then will run the test suites.
After the tests, will set down the containers.

### Debug the tests

Assuming the use of VSCode, you can find two files inside '.vscode' folder.
This tasks describes the behaviour needed to debug the tests.

Just simply add the breakpoints in the code editor and run 'Debug Jest' inside 'Debug and Run' tab.

## License

Copyright 2020 United Nations Office for the Coordination of Humanitarian Affairs
Expand Down
11 changes: 1 addition & 10 deletions test.sh → bin/test.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
root=$(pwd)

#Global variables
USAGE='this is the usage'
DEBUG_USAGE='this is the debug usage'
USAGE='Usage: test.sh [options] [-- [options]].\n Options:\n -oc, --only-containers: only start docker containers\n -sc, --stop-containers: stop docker containers\n -k, --keep: keep jest runing after the completion of the tests suites\n -c: run tests with coverage\n -h, --help: show this help message\n --: pass extra options'
KEEP=0
FORCE_STOP_JEST='--forceExit'
ONLY_CONTAINERS=0
STOP_CONTAINERSq=0
COMMAND_ARGS=''
DOCKER_EXEC_ARGS=''

function moveToTestDir {
echo 'Moving to tests dir'
Expand All @@ -23,10 +21,6 @@ function moveToRootDir {
## obtain options
while [ "$1" != "" ]; do
case $1 in
-d | --debug ) echo "Debug usage"
echo "$DEBUG_USAGE"
exit 0
;;
-oc | --only-containers ) ONLY_CONTAINERS=1
;;
-sc | --stop-containers ) STOP_CONTAINERS=1
Expand All @@ -36,9 +30,6 @@ while [ "$1" != "" ]; do
-c) shift
COMMAND_ARGS="${COMMAND_ARGS} --coverage"
;;
--no-tty) shift
DOCKER_EXEC_ARGS="${DOCKER_EXEC_ARGS} -it"
;;
-h | --help ) echo "$USAGE"
exit
;;
Expand Down
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const config: Config.InitialOptions = {
transformIgnorePatterns: ['node_modules/(?!(@unocha)/)'],
modulePathIgnorePatterns: ['<rootDir>/test/'],
setupFilesAfterEnv: ['<rootDir>/tests/test-environment-setup.ts'],
testTimeout: 100000,
testTimeout: 100_000,
};

export default config;
2 changes: 1 addition & 1 deletion src/db/util/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function createDbConnetion(config: t.TypeOf<typeof CONFIG>) {
await knex.raw('SELECT now()');

return knex;
} catch (error) {
} catch {
throw new Error(
'Unable to connect to Postgres via Knex. Ensure a valid connection.'
);
Expand Down
6 changes: 3 additions & 3 deletions src/db/util/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export const genericValidator = <T>(type: t.Type<T>, object: T) => {
const decoded = type.decode(object);
if (isRight(decoded)) {
return decoded.right;
} else {
const errors = ioTsErrorFormatter(decoded);
throw new Error(`Invalid data: ${errors.join(', ')}`);
}

const errors = ioTsErrorFormatter(decoded);
throw new Error(`Invalid data: ${errors.join(', ')}`);
};
10 changes: 5 additions & 5 deletions tests/testContext.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Knex from 'knex';
import v4Models, { Database } from '../src/db';
import type Knex from 'knex';
import v4Models, { type Database } from '../src/db';
import { createDbConnetion } from './utils/connection';

interface IContext {
Expand All @@ -24,10 +24,10 @@ export default class ContextProvider implements IContext {
public static get Instance(): ContextProvider {
if (this._instance) {
return this._instance;
} else {
this._instance = new ContextProvider();
return this._instance;
}

this._instance = new ContextProvider();
return this._instance;
}

public async setUpContext(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function createDbConnetion(config: t.TypeOf<typeof CONFIG>) {
await knex.raw('SELECT now()');

return knex;
} catch (error) {
} catch {
throw new Error(
'Unable to connect to Postgres via Knex. Ensure a valid connection.'
);
Expand Down
Loading

0 comments on commit 7b6836f

Please sign in to comment.