Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoytenko committed Aug 10, 2023
1 parent a99b682 commit 95b0449
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { defineConfig } from 'next/experimental/testmode/playwright'

export default defineConfig({
webServer: {
command: 'pnpm dev -- --experimental-test-proxy',
command: 'npm dev -- --experimental-test-proxy',
url: 'http://localhost:3000',
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function handleRoute(
) {
const request = route.request()

// COntinue the navigation and non-fetch requests.
// Continue the navigation and non-fetch requests.
if (request.isNavigationRequest() || request.resourceType() !== 'fetch') {
return route.continue()
}
Expand Down
15 changes: 13 additions & 2 deletions packages/next/src/experimental/testmode/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ function buildResponse(proxyResponse: ProxyFetchResponse): Response {
})
}

export function createTestFetch(originalFetch: Fetch): Fetch {
return async function testFetch(
function interceptFetch() {
const originalFetch = global.fetch
global.fetch = async function testFetch(
input: FetchInputArg,
init?: FetchInitArg
): Promise<Response> {
Expand Down Expand Up @@ -107,6 +108,16 @@ export function createTestFetch(originalFetch: Fetch): Fetch {
}
}

export function interceptTestApis(): () => void {
const originalFetch = global.fetch
interceptFetch()

// Cleanup.
return () => {
global.fetch = originalFetch
}
}

export function wrapRequestHandler(
handler: NodeRequestHandler
): NodeRequestHandler {
Expand Down
6 changes: 3 additions & 3 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ export default class NextNodeServer extends BaseServer {
// ensure options are set when loadConfig isn't called
setHttpClientAndAgentOptions(this.nextConfig)

// Intercept fetch
// Intercept fetch and other testmode apis.
if (this.serverOptions.experimentalTestProxy) {
const { createTestFetch } = require('../experimental/testmode/server')
global.fetch = createTestFetch(global.fetch)
const { interceptTestApis } = require('../experimental/testmode/server')
interceptTestApis()
}
}

Expand Down

0 comments on commit 95b0449

Please sign in to comment.