Skip to content

Commit

Permalink
🧪 [tests] Update test documentation and procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacInsoll committed Nov 7, 2024
1 parent 5fd1c0b commit 37949ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
19 changes: 10 additions & 9 deletions docs/development/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

- See [Initial Setup](initial-setup.md) if you are setting up a new environment.
- Check out the [Basic Tutorial](basic-tutorial.md) if you are set up but don't know how to get started.
- [Testing](testing.md) how to create/run tests using vitest.
- [Troubleshooting](troubleshooting.md) might help if you get stuck.

## CLI Commands
| Command | Description | When to use |
|--------------------------------|-------------------------------------------|--------------------------------------------------------------------------------------------------------|
| `npm start` | Run Server + Frontend in Dev Mode | When doing any dev and wanting instant reloads on changes of server or client files |
| `npm run build` | Build Server (including client) | Before making docker image |
| `cd frontend && npm run build` | Build frontend | Update front end build (before doing backend build) |
| `npm run gql` | Build GQL files | Run after updating any GQL on server to "see" new stuff, <br/>or after updating a query on client side |
| `npm run release` | Tag new version, build+push docker images | Run when we have something 'release worthy' |
| Command | Description | When to use |
|--------------------------------|----------------------------------------------|--------------------------------------------------------------------------------------------------------|
| `npm start` | Run Server + Frontend in Dev Mode | When doing any dev and wanting instant reloads on changes of server or client files |
| `cd frontend && npm run build` | Build frontend | Update front end build (before doing backend build) |
| `npm run build` | Build Server (including client) | Before making docker image |
| `npm run gql` | Build GQL files | Run after updating any GQL on server to "see" new stuff, <br/>or after updating a query on client side |
| `npm run test` | Build server, create docker image, run tests | Check that you haven't broken anything on the backend before a commit or release |
| `npm run test-only` | Create docker image, run tests | Same as above, but doesn't do `npm run build` first so it's faster if you have already built |
| `npm run release` | Tag new version, build+push docker images | Run when we have something 'release worthy' |

## Dev Server
Two ports are exposed during development:
Expand All @@ -30,5 +33,3 @@ For front end development you definitely want to use the 6969 address. For backe
| `dist` | *Compiled* Node Server Source |
| `frontend` | React frontend source |
| `public` | *Compiled* React front end and any other static assets |


32 changes: 32 additions & 0 deletions docs/development/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Testing

## Current test procedure
Currently the testing is only targeted at the backend. You can run a test by `npm run test` which:
- Download example files (if not already downloaded) and extract into `/media` folder
- Build backend (not frontend), build docker image
- Start `test-picr` container on port `9001` and `test-db` image with temporary storage (so it's wiped every run)
- Run all tests in `/tests` folder (see below) targetting the `test-picr` container
- Stop docker containers

## Current Tests

Currently we don't have much test coverage so this is more a "you didn't completely break it" rather than "perfect in every way"
- Server is online (IE: nothing so bad that the express web server won't start)
- Admin user can log in with default user/pass and has correct permissions

Obviously this is not exhaustive coverage so the short term plan is to cover all the GraphQL
endpoints on the backend, and if we have more developer time in future we may add front end testing.


## Creating Tests
You can find tests in the `tests` folder and the files should be named starting with a 2 digit number as the existing tests are.
You are welcome to rename them to change the order if it makes sense to you
(EG: 'create shared folder' happens before 'can access shared folder')

## When are tests run?
- Tests are run before each release as part of the `release-it` configuration.
- Tests are no run before each commit.
- You can manually run a `npm run test` before commit to make sure it works*

\* I didn't want to force this on you as it's a bit of time "wasted" doing builds/tests if just commiting a small change.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"run": " node dist/backend/app.js",
"gql": "graphql-codegen --config codegen.ts",
"release": "dotenv -- release-it --only-version",
"test": "vitest run"
"test": "npm run build && vitest run",
"test-only": "vitest run"
},
"author": "Isaac Insoll",
"license": "ISC",
Expand Down Expand Up @@ -85,9 +86,9 @@
},
"hooks": {
"before:init": [
"npm run test",
"cd frontend && npm run build",
"npm run build"
"npm run build",
"npm run test-only"
],
"after:git:release": [
"docker build . -t isaacinsoll/picr:latest -t isaacinsoll/picr:${version} -t ghcr.io/isaacinsoll/picr:latest -t ghcr.io/isaacinsoll/picr:${version}",
Expand Down
17 changes: 8 additions & 9 deletions tests/testSetup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import type { GlobalSetupContext } from 'vitest/node';
import { Readable } from 'node:stream';
import fs from 'fs';
import { existsSync, mkdirSync, rmSync } from 'node:fs';
import decompress from 'decompress';
import { IPicrConfiguration } from '../backend/config/IPicrConfiguration';
import path from 'path';
import { getVersion } from '../backend/config/configFromEnv';
import { picrConfig } from '../backend/config/picrConfig';
import { downMany, upMany } from 'docker-compose';
import { buildOne, downMany, upMany } from 'docker-compose';

const services = ['test-db', 'test-picr'];
// note: service[0] is built, rest are just started
const services = ['test-picr', 'test-db'];

const composeOpts = { cwd: path.join(__dirname), log: true };

export async function setup({ provide }: GlobalSetupContext) {
// provide('wsPort', 3000)
Expand All @@ -27,7 +25,8 @@ export async function setup({ provide }: GlobalSetupContext) {
mkdirSync(cache);
await decompress(samplePath, media);

await upMany(services, { cwd: path.join(__dirname), log: true });
await buildOne(services[0], composeOpts);
await upMany(services, composeOpts);

//we need to wait a bit longer for everything to finish booting
await new Promise((r) => setTimeout(r, 2000));
Expand All @@ -40,7 +39,7 @@ export async function setup({ provide }: GlobalSetupContext) {
}

export async function teardown() {
await downMany(services, { cwd: path.join(__dirname), log: true });
await downMany(services, composeOpts);
console.log('Test Teardown Complete');
}

Expand Down

0 comments on commit 37949ea

Please sign in to comment.