Skip to content

Commit

Permalink
chore(tests): fix example test
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Sep 9, 2024
1 parent 593cd64 commit dd53004
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---------------------------------------- SHOW ----------------------------------------
{
methodMode: 'post',
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': 'DYNAMIC_VALUE'
},
signal: undefined,
method: 'post',
url: 'https://countries.trevorblades.com/graphql',
body: '{"query":"{ languages { code } }"}'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1725741416691'
'x-sent-at-time': '1725894834399'
},
signal: undefined,
method: 'post',
Expand Down
7 changes: 3 additions & 4 deletions scripts/generate-examples-derivatives/generate-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export const generateTests = async () => {

await Promise.all(exampleFiles.map(async (file) => {
const encoderFilePath = encoderFilePaths.find((encoderFilePath) =>
encoderFilePath.match(new RegExp(`${file.name}.output-encoder.ts`)) !== null
encoderFilePath.includes(`${file.name}.output-encoder.ts`)
)
const snapshotFileName = `../../${file.path.dir}/${file.name}.output${encoderFilePath ? `.test` : ``}.txt`
const code = `// @vitest-environment node
// WARNING:
Expand All @@ -41,9 +42,7 @@ test(\`${file.name}\`, async () => {
const exampleResult = ${encoderFilePath ? `encode(stripAnsi(result.stdout))` : `stripAnsi(result.stdout)`}
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\\d+)/)?.[1] ?? \`unknown\`
await expect(exampleResult).toMatchFileSnapshot(\`../../${file.path.dir}/${file.name}.output${
encoderFilePath ? `.test` : ``
}.txt\`)
await expect(exampleResult).toMatchFileSnapshot(\`${snapshotFileName}\`)
})
`

Expand Down
7 changes: 2 additions & 5 deletions tests/examples/transport-http|transport-http_abort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_abort`, async () => {
const result = await execaCommand(`pnpm tsx ./examples/transport-http|transport-http_abort.ts`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
const exampleResult = stripAnsi(result.stdout)
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(
`../.././examples/transport-http|transport-http_abort.output.test.txt`,
)
await expect(exampleResult).toMatchFileSnapshot(`../.././examples/transport-http|transport-http_abort.output.txt`)
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_extension_fetch__custom-fetch`, async () => {
const result = await execaCommand(
`pnpm tsx ./examples/transport-http|transport-http_extension_fetch__custom-fetch.ts`,
)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
const exampleResult = stripAnsi(result.stdout)
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(
`../.././examples/transport-http|transport-http_extension_fetch__custom-fetch.output.test.txt`,
`../.././examples/transport-http|transport-http_extension_fetch__custom-fetch.output.txt`,
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'
import { encode } from '../../examples/transport-http|transport-http_extension_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_extension_headers__dynamicHeaders`, async () => {
const result = await execaCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_headers_raw__headers`, async () => {
const result = await execaCommand(`pnpm tsx ./examples/transport-http|transport-http_headers_raw__headers.ts`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
const exampleResult = stripAnsi(result.stdout)
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(
`../.././examples/transport-http|transport-http_headers_raw__headers.output.test.txt`,
`../.././examples/transport-http|transport-http_headers_raw__headers.output.txt`,
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_method-get`, async () => {
const result = await execaCommand(`pnpm tsx ./examples/transport-http|transport-http_method-get.ts`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
const exampleResult = stripAnsi(result.stdout)
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(
`../.././examples/transport-http|transport-http_method-get.output.test.txt`,
`../.././examples/transport-http|transport-http_method-get.output.txt`,
)
})
5 changes: 2 additions & 3 deletions tests/examples/transport-http|transport-http_raw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import { execaCommand } from 'execa'
import stripAnsi from 'strip-ansi'
import { expect, test } from 'vitest'
import { encode } from '../../examples/transport-http_headers__dynamicHeaders.output-encoder.js'

test(`transport-http|transport-http_raw`, async () => {
const result = await execaCommand(`pnpm tsx ./examples/transport-http|transport-http_raw.ts`)
expect(result.exitCode).toBe(0)
// Examples should output their data results.
const exampleResult = encode(stripAnsi(result.stdout))
const exampleResult = stripAnsi(result.stdout)
// If ever outputs vary by Node version, you can use this to snapshot by Node version.
// const nodeMajor = process.version.match(/v(\d+)/)?.[1] ?? `unknown`
await expect(exampleResult).toMatchFileSnapshot(`../.././examples/transport-http|transport-http_raw.output.test.txt`)
await expect(exampleResult).toMatchFileSnapshot(`../.././examples/transport-http|transport-http_raw.output.txt`)
})
2 changes: 1 addition & 1 deletion website/content/examples/transport-http-dynamic-headers.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ await graffle.rawString({ document: `{ languages { code } }` })
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1725741416691'
'x-sent-at-time': '1725894834399'
},
signal: undefined,
method: 'post',
Expand Down

0 comments on commit dd53004

Please sign in to comment.