Skip to content

Commit

Permalink
fix: use default remote port number
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Sep 5, 2024
1 parent 661113d commit cb50d9a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 17 deletions.
19 changes: 14 additions & 5 deletions src/node/SetupServerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import type { RequestHandler } from '~/core/handlers/RequestHandler'
import type { ListenOptions, SetupServer } from './glossary'
import { SetupServerCommonApi } from './SetupServerCommonApi'
import { Socket } from 'socket.io-client'
import { SyncServerEventsMap, createSyncClient } from './setupRemoteServer'
import {
MSW_REMOTE_SERVER_PORT,
SyncServerEventsMap,
createSyncClient,
} from './setupRemoteServer'
import { RemoteRequestHandler } from '~/core/handlers/RemoteRequestHandler'
import {
onAnyEvent,
Expand Down Expand Up @@ -97,15 +101,20 @@ export class SetupServerApi
// If the "remotePort" option has been provided to the server,
// run it in a special "remote" mode. That mode ensures that
// an extraneous Node.js process can affect this process' traffic.
if (this.resolvedOptions.remotePort != null) {
if (typeof this.resolvedOptions.remote !== 'undefined') {
const remotePort =
typeof this.resolvedOptions.remote === 'object'
? this.resolvedOptions.remote.port || MSW_REMOTE_SERVER_PORT
: MSW_REMOTE_SERVER_PORT

invariant(
typeof this.resolvedOptions.remotePort === 'number',
typeof remotePort === 'number',
'Failed to enable request interception: expected the "remotePort" option to be a valid port number, but got "%s". Make sure it is the same port number you provide as the "port" option to "remote.listen()" in your tests.',
this.resolvedOptions.remotePort,
remotePort,
)

// Create the WebSocket sync client immediately when starting the interception.
this.socketPromise = createSyncClient(this.resolvedOptions.remotePort)
this.socketPromise = createSyncClient(remotePort)

// Once the sync server connection is established, prepend the
// remote request handler to be the first for this process.
Expand Down
6 changes: 5 additions & 1 deletion src/node/glossary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import type {
} from '~/core/sharedOptions'

export interface ListenOptions extends SharedOptions {
remotePort?: number
remote?:
| boolean
| {
port?: number
}
}

export interface SetupServerCommon {
Expand Down
16 changes: 11 additions & 5 deletions src/node/setupRemoteServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
RequestIntention,
} from '~/core/utils/internal/requestUtils'

export const MSW_REMOTE_SERVER_PORT = 56957

const kSyncServer = Symbol('kSyncServer')
type SyncServerType = WebSocketServer<SyncServerEventsMap> | undefined

Expand All @@ -40,7 +42,7 @@ export function setupRemoteServer(
}

export interface SetupRemoteServerListenOptions {
port: number
port?: number
}

export interface SetupRemoteServer {
Expand Down Expand Up @@ -73,15 +75,19 @@ export class SetupRemoteServerApi
super(...handlers)
}

public async listen(options: SetupRemoteServerListenOptions): Promise<void> {
public async listen(
options: SetupRemoteServerListenOptions = {},
): Promise<void> {
const port = options.port || MSW_REMOTE_SERVER_PORT

invariant(
typeof options.port === 'number',
typeof port === 'number',
'Failed to initialize remote server: expected the "port" option to be a valid port number but got "%s". Make sure it is the same port number you provide as the "remotePort" option to "server.listen()" in your application.',
options.port,
port,
)

const dummyEmitter = new Emitter<LifeCycleEventsMap>()
const wssUrl = createWebSocketServerUrl(options.port)
const wssUrl = createWebSocketServerUrl(port)
const server = await createSyncServer(wssUrl)

server.removeAllListeners()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function spyOnLifeCycleEvents(setupApi: SetupApi<LifeCycleEventsMap>) {
}

beforeAll(async () => {
await remote.listen({ port: 56789 })
await remote.listen()
await httpServer.listen()
await testApp.start()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const remote = setupRemoteServer()
const testApp = new TestNodeApp(require.resolve('./use.app.js'))

beforeAll(async () => {
await remote.listen({ port: 56789 })
await remote.listen()
await testApp.start()
})

Expand Down
2 changes: 1 addition & 1 deletion test/node/msw-api/setup-remote-server/use.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const server = setupServer(
)

server.listen({
remotePort: 56789,
remote: true,
})

// Spawn a Node.js application.
Expand Down
4 changes: 1 addition & 3 deletions test/node/msw-api/setup-remote-server/use.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const remote = setupRemoteServer()
const testApp = new TestNodeApp(require.resolve('./use.app.js'))

beforeAll(async () => {
await remote.listen({
port: 56789,
})
await remote.listen()
await testApp.start()
})

Expand Down

0 comments on commit cb50d9a

Please sign in to comment.