diff --git a/packages/browser/src/client/tester/tester-utils.ts b/packages/browser/src/client/tester/tester-utils.ts index b53794ae5799..5578a8f0c55b 100644 --- a/packages/browser/src/client/tester/tester-utils.ts +++ b/packages/browser/src/client/tester/tester-utils.ts @@ -133,7 +133,6 @@ export class CommandsManager { const rpc = state.rpc as any as BrowserRPC const { sessionId, traces } = getBrowserState() const filepath = state.filepath || state.current?.file?.filepath - args = args.filter(arg => arg !== undefined) // remove optional fields const actionTraceGroupName = ACTION_TRACE_COMMANDS.has(command) ? `vitest:${command.slice('__vitest_'.length)}` diff --git a/packages/browser/src/node/rpc.ts b/packages/browser/src/node/rpc.ts index c5ee14b2b45e..30aa998a5adf 100644 --- a/packages/browser/src/node/rpc.ts +++ b/packages/browser/src/node/rpc.ts @@ -339,6 +339,7 @@ export function setupBrowserRpc(globalServer: ParentBrowserProject, defaultMocke } }, async triggerCommand(sessionId, command, testPath, payload) { + payload = payload.map(value => value === null ? undefined : value) debug?.('[%s] Triggering command "%s"', sessionId, command) const provider = project.browser!.provider if (!provider) { diff --git a/test/browser/test/commands.test.ts b/test/browser/test/commands.test.ts index 0661dd513d8a..1e7b5ade6ccb 100644 --- a/test/browser/test/commands.test.ts +++ b/test/browser/test/commands.test.ts @@ -45,19 +45,39 @@ it('can manipulate files', async () => { }) it('can run custom commands', async () => { - const result = await myCustomCommand('arg1', 'arg2') - expect(result).toEqual({ - testPath: expect.stringMatching('test/browser/test/commands.test.ts'), - arg1: 'arg1', - arg2: 'arg2', - }) + { + const result = await myCustomCommand('arg1', 'arg2') + expect(result).toEqual({ + testPath: expect.stringMatching('test/browser/test/commands.test.ts'), + arg1: 'arg1', + arg2: 'arg2', + }) + } + + { + const result = await myCustomCommand(undefined, 'arg2') + expect(result).toEqual({ + testPath: expect.stringMatching('test/browser/test/commands.test.ts'), + arg1: undefined, + arg2: 'arg2', + }) + } + + { + const result = await myCustomCommand(null, 'arg2') + expect(result).toEqual({ + testPath: expect.stringMatching('test/browser/test/commands.test.ts'), + arg1: null, + arg2: 'arg2', + }) + } }) declare module 'vitest/browser' { interface BrowserCommands { - myCustomCommand: (arg1: string, arg2: string) => Promise<{ + myCustomCommand: (arg1: string | undefined | null, arg2: string) => Promise<{ testPath: string - arg1: string + arg1: string | undefined | null arg2: string }>