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

CHE-195 Create BE Test Setup #154

Merged
merged 21 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
43600a3
Merge branch 'CHE-167/story/BE-Refactor-Error-Handling' of https://gi…
seantokuzo Jun 20, 2024
b50fdb3
initial test setup
seantokuzo Jun 20, 2024
323221f
rename mongo test service and mongo uri env var
seantokuzo Jun 20, 2024
981c9f1
update jest config for server tests - needs more work
seantokuzo Jun 20, 2024
dae3e18
add initial test-solo config and scripts - needs refactor
seantokuzo Jun 20, 2024
3030e0c
temp test to test setup
seantokuzo Jun 20, 2024
a205685
adjust config to only handle client files with ignore patterns
seantokuzo Jun 20, 2024
35ad808
add 'server or client' prompt to solo test script
seantokuzo Jun 20, 2024
66fac52
adjust config to only handle server tests
seantokuzo Jun 20, 2024
9656c3c
fix global login helper and remove comments
seantokuzo Jun 20, 2024
ef06399
fix depends on to renamed mongo service and remove unused volume
seantokuzo Jun 20, 2024
513fff2
add prompt loop
seantokuzo Jun 20, 2024
6f8384f
add alt exit
seantokuzo Jun 20, 2024
2dd2aaf
skip tests up for refactoring
seantokuzo Jun 20, 2024
c748707
delete temporary test
seantokuzo Jun 20, 2024
02c7253
Merge branch 'dev' of https://github.com/Code-Hammers/code-hammers in…
seantokuzo Jun 22, 2024
40351e2
add .DS_Store to ignore
seantokuzo Jun 22, 2024
7d84c2d
remove unused env vars
seantokuzo Jun 22, 2024
e265c37
fix return type for global login test helper
seantokuzo Jun 22, 2024
014c25d
fix JWT_SECRET env var name
seantokuzo Jun 22, 2024
f8aaa8e
remove log
seantokuzo Jun 22, 2024
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
dist/
coverage/
/client/build/
/server/config/serviceKey.json
/server/config/serviceKey.json
.DS_Store
2 changes: 1 addition & 1 deletion __tests__/profileController.test.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seantokuzo This file and userController can probably just be deleted at this point. The new ticket suite will encompass any testing that existed here.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jest.mock('../server/models/profileModel', () => ({
find: jest.fn(),
}));

describe('Profile Controller Tests', () => {
xdescribe('Profile Controller Tests', () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
let mockNext: NextFunction = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/userController.tests.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as profile controller. Lets axe it.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('../server/models/userModel', () => ({
}));
jest.mock('../server/utils/generateToken', () => () => 'someFakeToken');

describe('User Controller Tests', () => {
xdescribe('User Controller Tests', () => {
let mockRequest: Partial<Request>;
let mockResponse: Partial<Response>;
//TODO Add some error test for global error handler
Expand Down
12 changes: 8 additions & 4 deletions client/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require('path');

module.exports = {
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
Expand All @@ -8,10 +6,16 @@ module.exports = {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
},
setupFilesAfterEnv: [path.resolve(__dirname, './setupTests.ts')],
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
testRegex: '(\\.(test|spec))\\.(j|t)sx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testPathIgnorePatterns: [
'<rootDir>/node_modules',
'<rootDir>/build',
'<rootDir>/coverage',
'<rootDir>/src/assets',
],
};
38 changes: 38 additions & 0 deletions docker-compose-test-solo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
version: '3'
services:
test:
image: codehammers/ch-dev-dep-v3:latest
container_name: ch-test
ports:
- '3000:3000'
volumes:
- ./:/usr/src/app
- node_modules:/usr/src/app/node_modules
- client_node_modules:/usr/src/app/client/node_modules
depends_on:
- ch-mongo-test
environment:
- JWT_SECRET=${JWT_SECRET}
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-dev-database
- POSTGRES_PASSWORD=ch-dev
# suppress aws sdk v2 deprecation warning
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
- TEST_CMD=${TEST_CMD}
- TEST_FILE=${TEST_FILE}
command: npm run ${TEST_CMD} ${TEST_FILE}

ch-mongo-test:
image: mongo
container_name: ch-mongo-test
ports:
- '27017:27017'
environment:
- MONGO_INITDB_DATABASE=ch-testdb
# mute mongo container logs
command: mongod --quiet --logpath /dev/null

volumes:
node_modules:
client_node_modules:
8 changes: 4 additions & 4 deletions docker-compose-test.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good rename.

Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ services:
- node_modules:/usr/src/app/node_modules
- client_node_modules:/usr/src/app/client/node_modules
depends_on:
- mongo
- ch-mongo-test
environment:
- JWT_SECRET=${JWT_SECRET}
- MONGO_URI=mongodb://mongo:27017/ch-testdb
- MONGO_URI=mongodb://ch-mongo-test:27017/ch-testdb
- POSTGRES_USER=postgres
- POSTGRES_DB=ch-dev-database
- POSTGRES_PASSWORD=ch-dev
# suppress aws sdk v2 deprecation warning
- AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1;
command: npm run test:all

mongo:
ch-mongo-test:
image: mongo
container_name: ch-mongo-test
ports:
- '27017:27017'
environment:
Expand All @@ -34,4 +35,3 @@ services:
volumes:
node_modules:
client_node_modules:
mongodata:
17 changes: 11 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
module.exports = {
roots: ['<rootDir>/__tests__'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
preset: 'ts-jest',
testEnvironment: 'node',
setupFilesAfterEnv: ['<rootDir>/server/test/setup.ts'],
moduleFileExtensions: ['ts', 'js', 'json'],
testPathIgnorePatterns: [
'<rootDir>/scripts',
'<rootDir>/node_modules',
'<rootDir>/dist',
'<rootDir>/coverage',
'<rootDir>/client',
],
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"docker-dev": "docker-compose -f docker-compose-dev.yml up --build",
"docker-lint": "LINT_COMMAND=lint docker-compose -f docker-compose-lint.yml up --abort-on-container-exit",
"docker-lint:fix": "LINT_COMMAND=lint:fix docker-compose -f docker-compose-lint.yml up --abort-on-container-exit",
"docker-test:solo": "bash ./scripts/test-solo.sh",
"docker-test:all": "docker-compose -f docker-compose-test.yml up --abort-on-container-exit",
"docker-remove-all": "bash ./scripts/docker-remove-all.sh ch codehammers code-hammers",
"docker-build-check": "bash ./scripts/build-check.sh",
Expand Down
40 changes: 40 additions & 0 deletions scripts/test-solo.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derp derp 🤣

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

RED='\033[1;31m'
YELLOW='\033[1;33m'
GREEN='\033[1;32;1m'
CORNBLUE="\033[1;3;38;5;69m"
NC='\033[0m' # No color

# Check to see if they passed in which file they want to test
if [ -z $1 ]; then
echo -e "${RED}Test name not found. Please make sure to format the command properly:${NC}"
echo -en '\n'
echo -e "${YELLOW}Correct Syntax:${NC} 'npm run docker-test:solo <file name>' ";
echo -en '\n'
exit
fi

printf $"${YELLOW}\nAre you running a server or client test? (Enter S for server or C for client) [S/C]${NC}\n"
read -r SC

while [[ $SC != 'S' && $SC != 's' && $SC != '' && $SC != 'C' && $SC != 'c' && $SC != 'derp' ]]; do
echo -e "${RED}\nCommand not recognized\nPlease choose ${GREEN}S${RED} for server test or ${GREEN}C${RED} for client test (or Ctrl C to cancel)${NC}\n"
printf $"${YELLOW}\nAre you running a server or client test? (Enter S for server or C for client) [S/C]${NC}\n"
read -r SC
done

if [[ $SC == "S" || $SC == "s" || $SC == "" ]]; then
echo -e "${GREEN}\nGotcha. Looking for test files matching \"$1\" in the server${NC}\n"
TEST_CMD="test" TEST_FILE=$1 docker-compose -f docker-compose-test-solo.yml up --abort-on-container-exit
fi

if [[ $SC == "C" || $SC == "c" ]]; then
echo -e "${GREEN}\nGotcha. Looking for test files matching \"$1\" in the client${NC}\n"
TEST_CMD="test:client" TEST_FILE=$1 docker-compose -f docker-compose-test-solo.yml up --abort-on-container-exit
fi

if [[ $SC == "derp" ]]; then
echo -e "${CORNBLUE}\nWhy are you the way you are? ${RED}Goodbye${NC}\n"
exit 1
fi
49 changes: 49 additions & 0 deletions server/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import mongoose from 'mongoose';
import app from '../app';
import request from 'supertest';
import User from '../models/userModel';

declare global {
function login(): Promise<string[]>;
}

const testUserEmail = '[email protected]';
const testUserPassword = 'jackieTreehorn';

beforeAll(async () => {
process.env.JWT_SECRET = 'asdfasdfasdf';
await mongoose.connect('mongodb://ch-mongo-test:27017/ch-testdb', {});
});

beforeEach(async () => {
const collections = await mongoose.connection.db.collections();

for (const collection of collections) {
await collection.deleteMany({});
}
});

afterAll(async () => {
await mongoose.connection.close();
});

global.login = async () => {
await User.create({
firstName: 'Sean',
lastName: 'Kelly',
email: testUserEmail,
password: testUserPassword,
});

const response = await request(app)
.post('/api/users/login')
.send({
email: testUserEmail,
password: testUserPassword,
})
.expect(200);

const cookie = response.get('Set-Cookie') as string[];

return cookie;
};
Loading