feat: Setup Unit Tests for Resolvers

This commit is contained in:
Moriz Wahl 2021-10-07 00:11:23 +02:00
parent 501a23ede1
commit 2696a7eb5b
5 changed files with 78 additions and 5 deletions

View File

@ -18,6 +18,7 @@
"dependencies": {
"@types/jest": "^27.0.2",
"apollo-server-express": "^2.25.2",
"apollo-server-testing": "^2.25.2",
"axios": "^0.21.1",
"class-validator": "^0.13.1",
"cors": "^2.8.5",

View File

@ -0,0 +1,39 @@
import { createTestClient } from 'apollo-server-testing'
import createTestServer from '../../test-server'
let query: any
beforeAll(async () => {
const apollo = createTestServer()
query = createTestClient(await apollo).query
})
describe('CommunityResolver', () => {
const getCommunityInfoQuery = `
query {
getCommunityInfo() {
name
description
url
registerUrl
}
}
`
describe('getCommunityInfo', () => {
it('returns the default values', async () => {
await expect(query({ query: getCommunityInfoQuery })).resolves.toMatchObject({
data: {
getCommunityInfo: {
name: 'Gradido Entwicklung',
description: 'Die lokale Entwicklungsumgebung von Gradido.',
url: 'http://localhost/vue/',
registerUrl: 'http://localhost/vue/register',
},
},
})
})
})
})

View File

@ -19,9 +19,7 @@ export class CommunityResolver {
@Query(() => [Community])
async communities(): Promise<Community[]> {
const communities: Community[] = []
communities.push(
return [
new Community({
id: 1,
name: 'Gradido Entwicklung',
@ -43,7 +41,6 @@ export class CommunityResolver {
url: 'https://gradido.net',
registerUrl: 'https://gdd1.gradido.com/vue/register-community',
}),
)
return communities
]
}
}

View File

@ -0,0 +1,29 @@
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import cors from './server/cors'
import context from './server/context'
import plugins from './server/plugins'
import CONFIG from './config'
// graphql
import schema from './graphql/schema'
const createTestServer = async () => {
// Express Server
const server = express()
// cors
server.use(cors)
// Apollo Server
const apollo = new ApolloServer({
schema: await schema(),
playground: CONFIG.GRAPHIQL,
context,
plugins,
})
apollo.applyMiddleware({ app: server })
return apollo
}
export default createTestServer

View File

@ -1328,6 +1328,13 @@ apollo-server-plugin-base@^0.13.0:
dependencies:
apollo-server-types "^0.9.0"
apollo-server-testing@^2.25.2:
version "2.25.2"
resolved "https://registry.yarnpkg.com/apollo-server-testing/-/apollo-server-testing-2.25.2.tgz#0043e98b1a03720352e94b409215fb4782ae2e50"
integrity sha512-HjQV9wPbi/ZqpRbyyhNwCbaDnfjDM0hTRec5TOoOjurEZ/vh4hTPHwGkDZx3kbcWowhGxe2qoHM6KANSB/SxuA==
dependencies:
apollo-server-core "^2.25.2"
apollo-server-types@^0.9.0:
version "0.9.0"
resolved "https://registry.npmjs.org/apollo-server-types/-/apollo-server-types-0.9.0.tgz"