mirror of
https://github.com/IT4Change/boilerplate-backend.git
synced 2025-12-13 10:25:49 +00:00
add some unit tests
This commit is contained in:
parent
04cff232e5
commit
32f54ef594
@ -7,7 +7,7 @@ module.exports = {
|
||||
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
|
||||
coverageThreshold: {
|
||||
global: {
|
||||
lines: 86,
|
||||
lines: 78,
|
||||
},
|
||||
},
|
||||
setupFiles: ['./test/testSetup.ts'],
|
||||
|
||||
@ -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",
|
||||
|
||||
26
src/index.ts
26
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
|
||||
|
||||
33
src/server/server.spec.ts
Normal file
33
src/server/server.spec.ts
Normal 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
24
src/server/server.ts
Normal 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')
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user