Skip to content

Commit

Permalink
test: add integration test for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Nov 15, 2024
1 parent 1ddc187 commit 469b0f4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
"fs-extra": "^11.2.0",
"fs-teardown": "^0.3.0",
"glob": "^11.0.0",
"graphql-ws": "^5.16.0",
"jsdom": "^25.0.1",
"json-bigint": "^1.0.0",
"lint-staged": "^15.2.10",
Expand Down
13 changes: 12 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions test/node/graphql-api/graphql-subscription.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// @vitest-environment node
import { graphql } from 'msw'
import { setupServer } from 'msw/node'
import { createClient } from 'graphql-ws'

const server = setupServer()

beforeAll(() => {
server.listen()
})

afterEach(() => {
server.resetHandlers()
})

afterAll(() => {
server.close()
})

it('intercepts and mocks a GraphQL subscription', async () => {
const api = graphql.link('http://localhost:4000/graphql')

server.use(
api.pubsub.handler,
api.subscription('OnCommendAdded', () => {
api.pubsub.publish({
data: {
commentAdded: {
id: '1',
text: 'Hello world',
},
},
})
}),
)

const client = createClient({

Check failure on line 37 in test/node/graphql-api/graphql-subscription.test.ts

View workflow job for this annotation

GitHub Actions / build

test/node/graphql-api/graphql-subscription.test.ts > intercepts and mocks a GraphQL subscription

Error: WebSocket implementation missing; on Node you can `import WebSocket from 'ws';` and pass `webSocketImpl: WebSocket` to `createClient` ❯ Module.createClient node_modules/.pnpm/[email protected][email protected]/node_modules/graphql-ws/lib/client.mjs:73:15 ❯ test/node/graphql-api/graphql-subscription.test.ts:37:18
url: 'ws://localhost:4000/graphql',
})

const subscription = client.iterate({
query: `
subscription OnCommendAdded {
commentAdded {
id
text
}
}
`,
})

await expect(subscription.next()).resolves.toMatchObject({
value: {
data: {
commentAdded: {
id: '1',
text: 'Hello world',
},
},
},
})
})

0 comments on commit 469b0f4

Please sign in to comment.