Skip to content

Commit 31774ce

Browse files
committed
updating deps; various updates
1 parent 92cbdfe commit 31774ce

File tree

11 files changed

+5486
-12257
lines changed

11 files changed

+5486
-12257
lines changed

Diff for: jest.config.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ module.exports = {
88
collectCoverage: true,
99
coverageReporters: ['lcov', 'text', 'html'],
1010
collectCoverageFrom: [
11-
'<rootDir>/src/**/*.{ts,js}',
12-
'!<rootDir>/src/index.{ts,js}',
13-
'!<rootDir>/src/database/**',
11+
'<rootDir>/src/**/*.{ts,js}'
1412
],
1513
coverageThreshold: {
1614
global: {
@@ -21,5 +19,13 @@ module.exports = {
2119
},
2220
},
2321
testMatch: ["<rootDir>/tests/**/*.spec.ts", "<rootDir>/tests/**/*.test.ts"],
22+
transform: {
23+
'^.+\\.ts?$': [
24+
'ts-jest',
25+
{
26+
tsconfig: 'tsconfig.json', // Specify your TypeScript config file
27+
},
28+
],
29+
},
2430
clearMocks: true
2531
};

Diff for: package-lock.json

+5,347-12,199
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+18-18
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,37 @@
1414
"author": "Samuel Caçador",
1515
"license": "MIT",
1616
"dependencies": {
17-
"apollo-server": "^3.6.3",
18-
"apollo-server-express": "^3.6.3",
17+
"@apollo/server": "^4.11.2",
1918
"class-validator": "^0.14.1",
2019
"dotenv": "^16.0.0",
2120
"express": "^4.17.3",
2221
"graphql": "^16.9.0",
2322
"graphql-playground-middleware-express": "^1.7.23",
23+
"graphql-scalars": "^1.23.0",
2424
"mysql2": "^3.11.4",
2525
"reflect-metadata": "^0.1.13",
2626
"sqlite3": "^5.0.11",
2727
"type-graphql": "^2.0.0-rc.2",
2828
"typeorm": "^0.3.20"
2929
},
3030
"devDependencies": {
31-
"@types/express": "^4.17.13",
32-
"@types/jest": "^28.1.6",
33-
"@types/node": "^16.11.10",
34-
"@typescript-eslint/eslint-plugin": "^5.12.0",
35-
"@typescript-eslint/parser": "^5.12.0",
36-
"eslint": "^7.32.0",
37-
"eslint-config-prettier": "^8.3.0",
38-
"eslint-config-standard": "^16.0.3",
39-
"eslint-plugin-import": "^2.25.4",
31+
"@types/express": "^5.0.0",
32+
"@types/jest": "^29.5.14",
33+
"@types/node": "^22.9.3",
34+
"@typescript-eslint/eslint-plugin": "^8.15.0",
35+
"@typescript-eslint/parser": "^8.15.0",
36+
"eslint": "^8.57.1",
37+
"eslint-config-prettier": "^9.1.0",
38+
"eslint-config-standard": "^17.1.0",
39+
"eslint-plugin-import": "^2.31.0",
4040
"eslint-plugin-node": "^11.1.0",
41-
"eslint-plugin-prettier": "^4.0.0",
42-
"eslint-plugin-promise": "^5.2.0",
43-
"jest": "^28.1.3",
44-
"prettier": "2.5.1",
45-
"ts-jest": "^28.0.7",
46-
"ts-node": "10.7.0",
41+
"eslint-plugin-prettier": "^5.2.1",
42+
"eslint-plugin-promise": "^6.6.0",
43+
"jest": "^29.7.0",
44+
"prettier": "3.3.3",
45+
"ts-jest": "^29.2.5",
46+
"ts-node": "10.9.2",
4747
"ts-node-dev": "^2.0.0",
48-
"typescript": "4.5.2"
48+
"typescript": "5.7.2"
4949
}
5050
}

Diff for: src/controllers/IndexController.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import { Request, Response } from 'express';
1+
import { NextFunction, Request, Response } from 'express';
22

3-
const IndexController = (request: Request, response: Response): Response => {
4-
return response.json(['ok']);
3+
const IndexController = (request: Request, response: Response, next: NextFunction) => {
4+
try {
5+
response.json({
6+
status: 200,
7+
message: 'Hello, world! ' + Date.now(),
8+
});
9+
} catch (error) {
10+
next(error);
11+
}
512
};
6-
713
export { IndexController };

Diff for: src/index.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
import 'reflect-metadata';
22
import 'dotenv/config';
33
import express from 'express';
4+
import bodyParser from 'body-parser';
5+
import { expressMiddleware } from '@apollo/server/express4';
46
import router from './router';
5-
import { createApolloServer } from './services/ApolloServices/ApolloServerService';
67
import { initialiseDataSource } from './database';
8+
import { createApolloServer } from './services/ApolloServices/ApolloServerService';
79

810
const app = express();
911

10-
createApolloServer()
11-
.then(async (apolloServer: { start: () => void; getMiddleware: () => any }) => {
12-
await apolloServer.start();
13-
await initialiseDataSource();
12+
const startServer = async () => {
13+
await initialiseDataSource();
14+
15+
const apolloServer = await createApolloServer();
16+
await apolloServer.start();
1417

15-
app.use(express.json());
16-
app.use(apolloServer.getMiddleware());
17-
app.use(router);
18+
app.use('/graphql', bodyParser.json(), expressMiddleware(apolloServer));
19+
app.use(express.json());
20+
app.use(router);
1821

19-
app.listen(process.env.PORT || 3333, () => {
20-
console.log(`🚀 App started on port ${process.env.PORT || 3333} - Hello ${process.env.HELLO}`);
21-
});
22-
})
23-
.catch(err => {
24-
console.log(err);
22+
app.listen(process.env.PORT || 3333, () => {
23+
console.log(`🚀 App started on port ${process.env.PORT || 3333} - Hello ${process.env.HELLO}`);
2524
});
25+
};
26+
27+
startServer().catch(err => {
28+
console.error('Server failed to start', err);
29+
});
30+
31+
export { startServer };

Diff for: src/services/ApolloServices/ApolloServerService.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
import { ApolloServer } from 'apollo-server-express';
1+
import { ApolloServer } from '@apollo/server';
22
import { buildSchema } from 'type-graphql';
33
import { RecipeResolver } from '../../graphql/resolvers/RecipeResolver';
44

5-
async function createApolloServer(): Promise<ApolloServer> {
6-
const schema = await buildSchema({
5+
async function getSchema() {
6+
return buildSchema({
77
resolvers: [RecipeResolver],
88
validate: true,
99
});
10+
}
11+
12+
async function createApolloServer(): Promise<ApolloServer> {
1013
return new ApolloServer({
11-
schema: schema,
14+
schema: await getSchema(),
1215
});
1316
}
1417

15-
export { createApolloServer };
18+
export { getSchema, createApolloServer };

Diff for: tests/database/index.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { AppDataSource, initialiseDataSource, destroyDataSource } from '../../src/database';
2+
3+
describe('DataSource Utility', () => {
4+
const originalConsoleError = console.error;
5+
6+
beforeEach(async () => {
7+
console.error = jest.fn();
8+
9+
if (!AppDataSource.isInitialized) {
10+
await initialiseDataSource();
11+
}
12+
});
13+
14+
afterEach(async () => {
15+
console.error = originalConsoleError;
16+
17+
if (AppDataSource.isInitialized) {
18+
await destroyDataSource();
19+
}
20+
});
21+
22+
it('should initialize the data source successfully', async () => {
23+
expect(AppDataSource.isInitialized).toBe(true);
24+
});
25+
26+
it('should destroy the data source successfully', async () => {
27+
await destroyDataSource();
28+
expect(AppDataSource.isInitialized).toBe(false);
29+
});
30+
31+
it('should throw an error if initialize is called twice without destroying', async () => {
32+
await expect(AppDataSource.initialize()).rejects.toThrow();
33+
});
34+
35+
it('should allow reinitialization after destruction', async () => {
36+
await destroyDataSource();
37+
expect(AppDataSource.isInitialized).toBe(false);
38+
39+
await initialiseDataSource();
40+
expect(AppDataSource.isInitialized).toBe(true);
41+
});
42+
43+
it('should log an error if initialization fails', async () => {
44+
jest.spyOn(AppDataSource, 'initialize').mockImplementationOnce(() => {
45+
throw new Error('Mock Initialization Error');
46+
});
47+
48+
await initialiseDataSource();
49+
50+
expect(console.error).toHaveBeenCalledWith('Error during Data Source initialization', expect.any(Error));
51+
});
52+
});

Diff for: tests/graphql/RecipeResolver.spec.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'reflect-metadata';
2-
import { createApolloServer } from '../../src/services/ApolloServices/ApolloServerService';
32
import { AppDataSource, initialiseDataSource, destroyDataSource } from '../../src/database';
43
import { Recipe } from '../../src/entities/Recipe';
4+
import { testGraphQLQuery } from '../utils';
55

66
jest.mock('../../src/database');
77

@@ -16,7 +16,6 @@ describe('RecipeResolver', () => {
1616

1717
it('adds a recipe', async () => {
1818
const title = 'another one';
19-
const apolloServer = await createApolloServer();
2019
const query = `mutation {
2120
addRecipe(data: {title: "${title}"}) {
2221
id,
@@ -25,15 +24,13 @@ describe('RecipeResolver', () => {
2524
}
2625
}`;
2726

28-
const result = await apolloServer.executeOperation({
29-
query,
30-
});
27+
const result = await testGraphQLQuery<{ addRecipe: Recipe }>(query);
3128

3229
const addedRecipe = result.data?.addRecipe;
3330

3431
expect(addedRecipe).not.toBeNull();
35-
expect(addedRecipe.title).toEqual(title);
36-
expect(addedRecipe.description).toBeNull();
32+
expect(addedRecipe?.title).toEqual(title);
33+
expect(addedRecipe?.description).toBeNull();
3734
});
3835

3936
it('gets recipes', async () => {
@@ -45,7 +42,6 @@ describe('RecipeResolver', () => {
4542
const repository = AppDataSource.getRepository(Recipe);
4643
await repository.save(recipe);
4744

48-
const apolloServer = await createApolloServer();
4945
const query = `{
5046
recipes {
5147
id
@@ -54,9 +50,7 @@ describe('RecipeResolver', () => {
5450
}
5551
}`;
5652

57-
const result = await apolloServer.executeOperation({
58-
query,
59-
});
53+
const result = await testGraphQLQuery<{ recipes: Recipe[] }>(query);
6054

6155
expect(result.data?.recipes).toHaveLength(1);
6256
});

Diff for: tests/services/ApolloServices/ApolloServerService.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'reflect-metadata';
2-
import { ApolloServer } from 'apollo-server-express';
2+
import { ApolloServer } from '@apollo/server';
33
import { createApolloServer } from '../../../src/services/ApolloServices/ApolloServerService';
44

55
describe('ApolloServiceService', () => {

Diff for: tests/utils.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ExecutionResult, graphql } from 'graphql';
2+
import { getSchema } from '../src/services/ApolloServices/ApolloServerService';
3+
4+
async function testGraphQLQuery<T>(query: string, variables?: Record<string, any>) {
5+
const schema = await getSchema();
6+
7+
return graphql({
8+
schema,
9+
source: query,
10+
variableValues: variables,
11+
}) as unknown as Promise<ExecutionResult<T>>;
12+
}
13+
14+
export { testGraphQLQuery };

Diff for: tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
3+
"target": "ES2021",
44
"module": "commonjs",
55
"rootDir": "./src",
66
"allowJs": false,
77
"outDir": "./dist",
8-
"lib": ["ES2020", "esnext.asynciterable"],
8+
"lib": ["ES2021", "esnext.asynciterable"],
99
"esModuleInterop": true,
1010
"forceConsistentCasingInFileNames": true,
1111
"strict": true,

0 commit comments

Comments
 (0)