Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: node test runner & fastify v5 #871

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jobs:
strategy:
matrix:
node-version:
- 18
- 20
- 22
- 23
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
1 change: 0 additions & 1 deletion .taprc

This file was deleted.

16,793 changes: 5,483 additions & 11,310 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"prepare": "tsc && husky",
"build": "tsc",
"test": "NODE_ENV=test TAP_SNAPSHOT=1 tap --disable-coverage --allow-empty-coverage --jobs=1",
"test": "NODE_ENV=test node --test -r ts-node/register src/*.spec.ts",
"lint": "eslint .",
"example": "nodemon --watch './**/*.ts' --exec 'ts-node' ./examples/basic.ts"
},
Expand All @@ -33,17 +33,16 @@
"mercurius"
],
"peerDependencies": {
"fastify": "4.x",
"fastify": "5.x",
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
"mercurius": ">= 10.5.1"
"mercurius": ">= 15.0.0"
},
"devDependencies": {
"@commitlint/cli": "^19.0.3",
"@commitlint/config-conventional": "^19.0.3",
"@fastify/cors": "^10.0.1",
"@types/node": "^22.0.0",
"@types/sinon": "^17.0.0",
"@types/tap": "^15.0.7",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^6.17.0",
"dotenv": "^16.0.1",
Expand All @@ -56,15 +55,14 @@
"eslint-plugin-prettier": "^5.1.2",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-standard": "^5.0.0",
"fastify": "^4.3.0",
"fastify": "^5.0.0",
"graphql": "16.x",
"husky": "^9.0.11",
"lint-staged": "^15.0.1",
"mercurius": "^14.0.0",
"mercurius": "^15.0.0",
"nodemon": "^3.0.1",
"prettier": "^3.1.1",
"sinon": "^19.0.2",
"tap": "^21.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
},
Expand All @@ -88,4 +86,4 @@
"Jiří Špác <[email protected]>",
"Andrew Wood <[email protected]>"
]
}
}
19 changes: 10 additions & 9 deletions src/ApolloTraceBuilder.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DocumentNode, SelectionNode, Kind, OperationTypeNode } from 'graphql'
import tap from 'tap'
import { afterEach, describe, test, TestContext } from 'node:test'

import { DocumentNode, Kind, OperationTypeNode, SelectionNode } from 'graphql'
import sinon from 'sinon'

import { ApolloTraceBuilder } from './ApolloTraceBuilder'
Expand All @@ -26,17 +27,17 @@ const document: DocumentNode = {
]
}

tap.test('builds the query signature', async (t) => {
test('builds the query signature', async (t) => {
const traceBuilder = new ApolloTraceBuilder(document, {})
t.equal(traceBuilder.querySignature, '# -\n{test}')
t.assert.equal(traceBuilder.querySignature, '# -\n{test}')
})

tap.test('timers', async (t) => {
describe('timers', async () => {
let clock
t.afterEach(() => {
afterEach(() => {
clock.restore()
})
t.test('start and end times match time change', async (tt) => {
test('start and end times match time change', async (t: TestContext) => {
const startTime = 1483228800000
const tickTime = 10000
clock = sinon.useFakeTimers({ now: startTime })
Expand All @@ -47,11 +48,11 @@ tap.test('timers', async (t) => {

traceBuilder.stopTiming()

tt.same(traceBuilder.trace.startTime, {
t.assert.deepEqual(traceBuilder.trace.startTime, {
seconds: startTime / 1000,
nanos: 0
})
tt.same(traceBuilder.trace.endTime, {
t.assert.deepEqual(traceBuilder.trace.endTime, {
seconds: (startTime + tickTime) / 1000,
nanos: 0
})
Expand Down
91 changes: 44 additions & 47 deletions src/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
import tap from 'tap'
import { test, TestContext } from 'node:test'

import { Report } from 'apollo-reporting-protobuf'
import fastify from 'fastify'
import mercurius from 'mercurius'

import mercuriusMetrics from '../src/index'
import { basicResolvers, basicSchema } from '../examples/basicSchema'
import mercuriusMetrics from '../src/index'

import { createSimpleServer } from './createSimpleServer'

tap.cleanSnapshot = (s) => {
return s.replace(
// we need to replace any timings with 0, because they change from run to run
/"(nanos|seconds|startTime|endTime|durationNs)": [0-9]+,/gm,
'$1: 0,'
)
}

// import fp from 'fastify-plugin'
test('e2e metrics including "sample error" error are reported', async (t: TestContext) => {
const endpointUrl = 'http://localhost:3334'
const server = createSimpleServer((unzippedData) => {
const reportDecoded = Report.decode(unzippedData)
delete reportDecoded.header
t.assert.snapshot(reportDecoded)
})

tap.test(
'e2e metrics including "sample error" error are reported',
async (t) => {
const endpointUrl = 'http://localhost:3334'
const server = createSimpleServer((unzippedData) => {
const reportDecoded = Report.decode(unzippedData)
delete reportDecoded.header
t.matchSnapshot(reportDecoded)
})
const app = fastify()
app.register(mercurius, {
schema: basicSchema,
resolvers: basicResolvers,
graphiql: true
})

const app = fastify()
app.register(mercurius, {
schema: basicSchema,
resolvers: basicResolvers,
graphiql: true
})

app.register(mercuriusMetrics, {
apiKey: 'APOLLO_KEY',
endpointUrl,
graphRef: 'APOLLO_GRAPH_ID' + '@' + 'APOLLO_GRAPH_VARIANT'
// sendReportsImmediately: true // this is for lambda-like execution model
})
const query = `
app.register(mercuriusMetrics, {
apiKey: 'APOLLO_KEY',
endpointUrl,
graphRef: 'APOLLO_GRAPH_ID' + '@' + 'APOLLO_GRAPH_VARIANT'
// sendReportsImmediately: true // this is for lambda-like execution model
})
const query = `
query testQuery {
throwErr
post {
body
}
}`

const response = await app.inject({
method: 'POST',
url: '/graphql',
payload: { query }
})

t.equal(response.statusCode, 200)
const response = await app.inject({
method: 'POST',
url: '/graphql',
payload: { query }
})

t.matchSnapshot(JSON.parse(response.payload).errors)
t.assert.equal(response.statusCode, 200)

await app.apolloTracingStore.flushTracing()
t.assert.deepStrictEqual(JSON.parse(response.payload).errors, [
{
locations: [
{
column: 9,
line: 3
}
],
message: 'sample error',
path: ['throwErr']
}
])

server.close()
await app.apolloTracingStore.flushTracing()

t.end()
}
)
server.close()
})
Loading
Loading