Skip to content

Commit

Permalink
test: ensure operation is not reported if context creation raises error
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jan 16, 2024
1 parent 7156621 commit 8dc51b4
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions packages/libraries/client/tests/yoga.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import { GraphQLError } from 'graphql';
// eslint-disable-next-line import/no-extraneous-dependencies
import { createSchema, createYoga } from 'graphql-yoga';
// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -328,3 +329,104 @@ it('does not report usage for operation that does not pass validation', async ()
}, 1000);
});
});

it('does not report usage if context creating raises an error', async () => {
const callback = vi.fn();
const graphqlScope = nock('http://localhost')
.post('/graphql')
.reply(200, {
data: {
__typename: 'Query',
tokenInfo: {
__typename: 'TokenInfo',
token: {
name: 'brrrt',
},
organization: {
name: 'mom',
cleanId: 'ur-mom',
},
project: {
name: 'projecto',
type: 'FEDERATION',
cleanId: 'projecto',
},
target: {
name: 'projecto',
cleanId: 'projecto',
},
canReportSchema: true,
canCollectUsage: true,
canReadOperations: true,
},
},
});

const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
hi: String
}
`,
}),
plugins: [
{
onContextBuilding() {
throw new GraphQLError('Not authenticated.');
},
},
useHive({
enabled: true,
debug: true,
token: 'brrrt',
selfHosting: {
applicationUrl: 'http://localhost/foo',
graphqlEndpoint: 'http://localhost/graphql',
usageEndpoint: 'http://localhost/usage',
},
usage: {
endpoint: 'http://localhost/usage',
clientInfo() {
return {
name: 'brrr',
version: '1',
};
},
},
agent: {
maxSize: 1,
},
}),
],
});

// eslint-disable-next-line no-async-promise-executor
await new Promise<void>(async (resolve, reject) => {
nock.emitter.once('no match', (req: any) => {
reject(new Error(`Unexpected request was sent to ${req.path}`));
});

const res = await yoga.fetch('http://localhost/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: /* GraphQL */ `
{
hi
}
`,
}),
});
expect(res.status).toBe(200);
expect(await res.text()).toMatchInlineSnapshot(`{"errors":[{"message":"Not authenticated."}]}`);

setTimeout(() => {
graphqlScope.done();
expect(callback).not.toHaveBeenCalled();
resolve();
}, 1000);
});
});

0 comments on commit 8dc51b4

Please sign in to comment.