Skip to content

Commit e55043e

Browse files
committed
Update local testing setup
Add docker-compose.test.yml for a local test DB Add mocharc with default files to test so they can be omitted from the test command Update the knexfile to provide some defaults for dev/test envs Add lint and test to the README Disable lint/test in circleCI
1 parent b82d53a commit e55043e

File tree

10 files changed

+632
-563
lines changed

10 files changed

+632
-563
lines changed

.circleci/config.yml

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,6 @@ install_cf_cli: &install_cf_cli
3434
sudo dpkg -i cf8-cli-installer_8.7.4_x86-64.deb
3535
3636
jobs:
37-
lint_js:
38-
docker:
39-
- image: cimg/node:20.11.0-browsers
40-
steps:
41-
- checkout
42-
- *restore_npm_cache
43-
- *install_npm_packages
44-
- *save_npm_cache
45-
- run:
46-
name: lint javascript
47-
command: npm run lint
48-
test:
49-
docker:
50-
- image: cimg/node:20.11.0-browsers
51-
environment:
52-
POSTGRES_USER: postgres
53-
NODE_ENV: test
54-
- image: circleci/postgres:9.5-alpine
55-
environment:
56-
POSTGRES_DB: analytics-api-test
57-
steps:
58-
- checkout
59-
- *restore_npm_cache
60-
- *install_npm_packages
61-
- *save_npm_cache
62-
- run:
63-
name: unit test javascript
64-
command: npm test
6537
development_env_deploy:
6638
docker:
6739
- image: cimg/node:20.11.0-browsers

.github/workflows/ci.yml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
path: ./node_modules
3030
key: v1-dependencies-${{ env.PACKAGE_CHECKSUM }}
31-
- name: Lint JavaScript
31+
- name: lint javascript
3232
run: npm run lint
3333
test:
3434
needs: lint
@@ -39,8 +39,8 @@ jobs:
3939
image: postgres:latest
4040
env:
4141
POSTGRES_DB: analytics_reporter_test
42-
POSTGRES_USER: postgres
43-
POSTGRES_PASSWORD: postgres
42+
POSTGRES_USER: analytics
43+
POSTGRES_PASSWORD: 123abc
4444
ports:
4545
- 5432:5432
4646
options:
@@ -71,10 +71,5 @@ jobs:
7171
with:
7272
path: ./node_modules
7373
key: v1-dependencies-${{ env.PACKAGE_CHECKSUM }}
74-
- name: Run tests
75-
env:
76-
POSTGRES_USER: postgres
77-
POSTGRES_PASSWORD: postgres
78-
POSTGRES_DB: analytics_reporter_test
79-
POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }}
74+
- name: run tests
8075
run: npm test

.mocharc.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
diff: true
2+
extension: ['js']
3+
package: './package.json'
4+
slow: '75'
5+
spec:
6+
- 'test/**/*.js'
7+
timeout: '2000'
8+
ui: 'bdd'
9+
watch-files:
10+
- 'src/**/*.js'
11+
- 'test/**/*.js'

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,41 @@ An enum which describes the session. Possible values:
149149
'Direct', 'Organic Search', 'Paid Social', 'Organic Social', 'Email',
150150
'Affiliates', 'Referral', 'Paid Search', 'Video', and 'Display'
151151

152-
# Running the Tests
152+
# Linting
153153

154-
The Analytics API application is backed by a test suite that uses
155-
[Mocha](https://mochajs.org/) to run tests.
154+
This repo uses Eslint and Prettier for code static analysis and formatting. Run
155+
the linter with:
156156

157-
Before running the test suite, a database for the tests will need to be created.
157+
```shell
158+
npm run lint
159+
```
160+
161+
Automatically fix lint issues with:
158162

159163
```shell
160-
createdb analytics-api-test
164+
npm run lint:fix
161165
```
162166

163-
Then the tests can be invoked via NPM. The test script has a pretest hook that
164-
migrates the database.
167+
# Running the unit tests
168+
169+
The unit tests for this repo require a local PostgreSQL database. You can run a
170+
local DB server or create a docker container using the provided test compose
171+
file. (Requires docker and docker-compose to be installed)
172+
173+
Starting a docker test DB:
165174

175+
```shell
176+
docker-compose -f docker-compose.test.yml up
166177
```
178+
179+
Once you have a PostgreSQL DB running locally, you can run the tests. The test
180+
DB connection in knexfile.js has some default connection config which can be
181+
overridden with environment variables. If using the provided docker-compose DB
182+
then you can avoid setting the connection details.
183+
184+
Run the tests (pre-test hook runs DB migrations):
185+
186+
```shell
167187
npm test
168188
```
169189

docker-compose.test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3.0"
2+
services:
3+
db:
4+
image: postgres:16
5+
environment:
6+
- POSTGRES_DB=analytics_reporter_test
7+
- POSTGRES_USER=analytics
8+
- POSTGRES_PASSWORD=123abc
9+
ports:
10+
- "5432:5432"

knexfile.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ module.exports = {
2626
test: {
2727
client: "postgresql",
2828
connection: {
29-
user: process.env.CIRCLECI ? "postgres" : undefined,
30-
database: "analytics-api-test",
29+
host: process.env.POSTGRES_HOST || "localhost",
30+
user: process.env.POSTGRES_USER || "analytics",
31+
password: process.env.POSTGRES_PASSWORD || "123abc",
32+
database: process.env.POSTGRES_DATABASE || "analytics_reporter_test",
3133
},
3234
migrations: {
3335
tableName: "knex_migrations",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dev": "nodemon src/index.js",
99
"postinstall": "",
1010
"pretest": "NODE_ENV=test npm run migrate",
11-
"test": "NODE_ENV=test `npm bin`/mocha test/**/*.test.js --exit",
11+
"test": "NODE_ENV=test mocha",
1212
"migrate": "knex migrate:latest",
1313
"lint": "eslint .",
1414
"lint:fix": "eslint . --fix"

src/db.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,4 @@ const query = ({
125125
);
126126
};
127127

128-
module.exports = { query, queryDomain, buildTimeQuery };
128+
module.exports = { query, queryDomain, buildTimeQuery, dbClient: db };

0 commit comments

Comments
 (0)