Skip to content

Commit deadf4f

Browse files
author
Nathaniel Lin
committed
fixed test error #8
1 parent e400f65 commit deadf4f

File tree

13 files changed

+3592
-6741
lines changed

13 files changed

+3592
-6741
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
NODE_ENV=DEV
22
PORT=5000
3+
MONGO_URI="mongodb://localhost:27017/api_server_nodejs"
4+
SECRET="SuperS3cret_4277m"

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ module.exports = {
1515
'no-param-reassign': 'off',
1616
'no-underscore-dangle': 'off',
1717
'consistent-return': 'off',
18+
'no-console': 'off',
1819
},
1920
};

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module.exports = {
2+
transform: {
3+
'^.+\\.ts?$': 'ts-jest',
4+
},
5+
setupFiles: ['dotenv/config'],
26
testEnvironment: 'node',
37
coveragePathIgnorePatterns: ['node_modules'],
48
coverageReporters: ['text', 'lcov', 'clover', 'html'],
5-
};
9+
};

package-lock.json

Lines changed: 0 additions & 6425 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"author": "AppSeed.us",
77
"main": "src/index",
88
"scripts": {
9-
"start": "npm run build && node build/index.js",
9+
"start": "yarn build && node build/index.js",
1010
"dev": "ts-node-dev src/index.ts",
1111
"test": "jest -i --colors --verbose --detectOpenHandles",
12-
"lint": "eslint src",
12+
"lint": "eslint src --ext .ts",
1313
"build": "tsc -p tsconfig.build.json",
1414
"typecheck": "tsc --noEmit"
1515
},
@@ -28,12 +28,13 @@
2828
"passport-jwt": "^4.0.0"
2929
},
3030
"devDependencies": {
31+
"ts-jest": "^27.0.3",
32+
"mongodb-memory-server": "^7.2.0",
3133
"@types/bcrypt-nodejs": "0.0.31",
3234
"@types/compression": "^1.7.1",
3335
"@types/cors": "^2.8.11",
3436
"@types/express": "^4.17.13",
3537
"@types/jest": "^26.0.24",
36-
"@types/joi": "^17.2.3",
3738
"@types/node": "^16.0.1",
3839
"@types/passport-jwt": "^3.0.6",
3940
"@types/supertest": "^2.0.11",
@@ -48,4 +49,4 @@
4849
"ts-node-dev": "1.1.8",
4950
"typescript": "^4.3.5"
5051
}
51-
}
52+
}

src/config/keys.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/config/passport.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import passport from 'passport';
88
import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt';
99

1010
import User from '../models/user';
11-
import config from './keys';
1211

1312
export default (pass: passport.PassportStatic) => {
1413
const opts = {
1514
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('jwt'),
16-
secretOrKey: config.secret,
15+
secretOrKey: process.env.SECRET,
1716
};
1817

1918
pass.use(

src/routes/users.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import express from 'express';
88
import Joi from 'joi';
99
import jwt from 'jsonwebtoken';
1010

11-
import config from '../config/keys';
1211
import { checkToken } from '../config/safeRoutes';
1312
import ActiveSession from '../models/activeSession';
1413
import User from '../models/user';
@@ -87,7 +86,10 @@ router.post('/login', (req, res) => {
8786

8887
bcrypt.compare(password, user.password, (_err2, isMatch) => {
8988
if (isMatch) {
90-
const token = jwt.sign(user.toJSON(), config.secret, {
89+
if (!process.env.SECRET) {
90+
throw new Error('SECRET not provided');
91+
}
92+
const token = jwt.sign(user.toJSON(), process.env.SECRET, {
9193
expiresIn: 86400, // 1 week
9294
});
9395

src/server/database.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'dotenv/config';
2+
3+
import { MongoMemoryServer } from 'mongodb-memory-server';
4+
import mongoose from 'mongoose';
5+
6+
const mongooseOptions: mongoose.ConnectionOptions = {
7+
connectTimeoutMS: 30000,
8+
keepAlive: true,
9+
useNewUrlParser: true,
10+
useFindAndModify: false,
11+
useCreateIndex: true,
12+
useUnifiedTopology: true,
13+
};
14+
15+
export const prepareDB = async () => {
16+
const server: MongoMemoryServer = await MongoMemoryServer.create();
17+
18+
const start = () => {
19+
const mongoUri = server?.getUri();
20+
mongoose.connect(mongoUri, mongooseOptions, (err) => {
21+
if (err) console.error(err);
22+
});
23+
};
24+
25+
const stop = async () => {
26+
await mongoose.disconnect();
27+
return server.stop();
28+
};
29+
30+
return {
31+
start,
32+
stop,
33+
};
34+
};
35+
36+
export default {
37+
connect(): void {
38+
mongoose
39+
.connect(`${process.env.MONGO_URI}`, mongooseOptions)
40+
.then(() => console.log('MongoDB connected'))
41+
.catch((err: unknown) => console.error(err));
42+
},
43+
};

0 commit comments

Comments
 (0)