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

setup testing using jest #70

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ POSTGRES_PASSWORD=admin
POSTGRES_USER=admin
POSTGRES_DB=karmaDB
POSTGRES_HOST_PORT=5432
ACCESS_TOKEN_SECRET=rabeehisawesome
POSTGRES_TEST_PORT=5433
ACCESS_TOKEN_SECRET=rabeehisawesome
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"parser": "babel-eslint",
"extends": ["airbnb-base/legacy", "prettier", "plugin:node/recommended"],
"plugins": ["prettier"],
"plugins": ["prettier", "jest"],
"env": {
"jest/globals": true
},
"rules": {
"strict": "off",
"prettier/prettier": "error",
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
docker-compose up
```

If you plan to run tests use the following instead,
```bash
docker-compose -f docker-compose.yaml -f db-test-service.yaml up
```

- Migrate and seed the database

```bash
Expand Down
12 changes: 12 additions & 0 deletions db-test-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# optional - only for testing

version: '3.1'
services:
db-test:
image: postgres
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
ports:
- ${POSTGRES_TEST_PORT}:5432
14 changes: 14 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,18 @@ module.exports = {
},
debug: true,
},
test: {
client: 'pg',
connection: {
host: process.env.POSTGRES_HOST || '127.0.0.1',
port: process.env.POSTGRES_TEST_PORT,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
},
migrations: {
directory: './src/db/migrations',
},
debug: true,
},
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start": "nodemon src/server.js",
"dev": "nodemon -e js,gql src/server.js",
"lint": "eslint . --fix",
"test": "jest --verbose true",
"migrate": "knex migrate:latest",
"rollback": "knex migrate:rollback"
},
Expand Down Expand Up @@ -43,10 +44,12 @@
"eslint-config-node": "^4.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.4",
"faker": "^5.1.0",
"husky": "^4.2.5",
"jest": "^26.6.3",
"lint-staged": "^10.2.11",
"nodemon": "^2.0.4",
"prettier": "^2.0.5"
Expand Down
42 changes: 42 additions & 0 deletions src/graphql/People/people.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const app = require('../../app');
const connection = require('../../db/db');

beforeAll(async () => {
await connection.migrate.latest();
});

afterAll(async () => {
await app.close();
await connection.migrate.rollback();
await connection.destroy();
});

describe('people data', () => {
const query = `
query {
people {
first_name
}
}
`;

test('initial people list is empty', async () => {
await app
.inject({
method: 'POST',
url: '/graphql',
payload: { query: query },
headers: { 'Content-Type': 'application/json' },
})
.then((res) => {
expect(res.statusCode).toBe(200);
let data = res.json();
console.log(data);
expect(data).not.toHaveProperty('errors');
expect(data).toHaveProperty('data.people');
let peopleData = data.data.people;
expect(peopleData).not.toBe(null);
expect(peopleData.length).toBe(0);
});
});
});
Empty file removed src/graphql/People/people.tests.js
Empty file.
Loading