diff --git a/jest.config.js b/jest.config.js index d682eff..732c108 100644 --- a/jest.config.js +++ b/jest.config.js @@ -7,7 +7,7 @@ module.exports = { collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'], coverageThreshold: { global: { - lines: 86, + lines: 78, }, }, setupFiles: ['./test/testSetup.ts'], diff --git a/package.json b/package.json index c2ee811..ecc0d2c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 0506e2d..68ca8f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/server/server.spec.ts b/src/server/server.spec.ts new file mode 100644 index 0000000..afbb886 --- /dev/null +++ b/src/server/server.spec.ts @@ -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() + }) + }) +}) diff --git a/src/server/server.ts b/src/server/server.ts new file mode 100644 index 0000000..f00681b --- /dev/null +++ b/src/server/server.ts @@ -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') +}