add some unit tests

This commit is contained in:
Moriz Wahl 2023-12-04 15:31:58 +01:00
parent 04cff232e5
commit 32f54ef594
5 changed files with 61 additions and 27 deletions

View File

@ -7,7 +7,7 @@ module.exports = {
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
coverageThreshold: {
global: {
lines: 86,
lines: 78,
},
},
setupFiles: ['./test/testSetup.ts'],

View File

@ -21,7 +21,8 @@
"test:lint": "npm run test:lint:eslint && npm run test:lint:remark",
"test:lint:eslint": "eslint --ext .ts,.tsx,.js,.jsx,.json,.yml,.yaml --max-warnings 0 --ignore-path .gitignore .",
"test:lint:remark": "remark . --quiet --frail",
"test:unit": "TZ=UTC jest --runInBand --forceExit --detectOpenHandles"
"test:unit": "TZ=UTC jest --runInBand --forceExit --detectOpenHandles",
"test": "npm run test:lint && npm run test:unit"
},
"dependencies": {
"apollo-server-express": "^3.13.0",

View File

@ -1,30 +1,6 @@
// eslint-disable-next-line import/no-unassigned-import
import 'reflect-metadata'
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { schema } from './graphql/schema'
async function listen(port: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const app: any = express()
const server = new ApolloServer({
schema: await schema(),
})
await server.start()
server.applyMiddleware({ app, path: '/' })
return app.listen(port)
}
async function main() {
await listen(4000)
// eslint-disable-next-line no-console
console.log('🚀 Server is ready at http://localhost:4000/graphql')
}
import { main } from './server/server'
main().catch((e) => {
// eslint-disable-next-line no-console

33
src/server/server.spec.ts Normal file
View File

@ -0,0 +1,33 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { listen } from './server'
jest.mock('express', () => {
const originalModule = jest.requireActual('express')
return {
__esModule: true,
...originalModule,
default: jest.fn(() => {
return {
listen: jest.fn(),
}
}),
}
})
jest.mock('apollo-server-express')
describe('server', () => {
describe('listen', () => {
beforeEach(async () => {
jest.clearAllMocks()
await listen(4000)
})
it('calls express', () => {
expect(express).toBeCalled()
})
})
})

24
src/server/server.ts Normal file
View File

@ -0,0 +1,24 @@
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { schema } from '#graphql/schema'
export async function listen(port: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const app: any = express()
const server = new ApolloServer({
schema: await schema(),
})
await server.start()
server.applyMiddleware({ app, path: '/' })
return app.listen(port)
}
export async function main() {
await listen(4000)
// eslint-disable-next-line no-console
console.log('🚀 Server is ready at http://localhost:4000/graphql')
}