vitest-dev/vitest · error · Error

Unknown mock type: ${(mock as any).type}

Error message

Unknown mock type: ${(mock as any).type}

What it means

Thrown at packages/mocker/src/browser/interceptor-msw.ts:109 as the default case of the switch over mock.type inside the MSW request handler. The interceptor handles four known types (manual, automock, autospy, redirect); any other value means the registry produced a module with an unrecognized type, which is an internal contract violation.

Source

Thrown at packages/mocker/src/browser/interceptor-msw.ts:109

      const worker = setupWorker(
        http.get(/.+/, async ({ request }) => {
          const path = cleanQuery(request.url.slice(location.origin.length))
          if (!this.mocks.has(path)) {
            return passthrough()
          }

          const mock = this.mocks.get(path)!

          switch (mock.type) {
            case 'manual':
              return this.resolveManualMock(mock)
            case 'automock':
            case 'autospy':
              return Response.redirect(injectQuery(path, `mock=${mock.type}`))
            case 'redirect':
              return Response.redirect(mock.redirect)
            default:
              throw new Error(`Unknown mock type: ${(mock as any).type}`)
          }
        }),
      )
      return worker.start(this.options.mswOptions).then(() => worker)
    }).finally(() => {
      this.worker = worker
      this.startPromise = undefined
    })
    return await this.startPromise
  }
}

const trailingSeparatorRE = /[?&]$/
const timestampRE = /\bt=\d{13}&?\b/
const versionRE = /\bv=\w{8}&?\b/
function cleanQuery(url: string) {
  return url.replace(timestampRE, '').replace(versionRE, '').replace(trailingSeparatorRE, '')
}

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Reinstall @vitest/mocker and vitest to ensure consistent internal types.
  2. Open a Vitest issue with a reproduction as the error indicates an unhandled internal state.
  3. Avoid custom forks/patches of the mocker registry that introduce new type values.
Defensive patterns

Strategy: try-catch

Try / catch

// Internal state error; user code cannot pre-validate the mock type.
// Catch to surface and report.
try {
  // test code that triggers MSW interceptor mocking
} catch (e) {
  if (e instanceof Error && /Unknown mock type/.test(e.message)) {
    console.error('Vitest internal: unrecognized mock type in MSW interceptor. Report with reproduction.')
  }
  throw e
}

Prevention

When it happens

Trigger: A MockedModule with a type outside the known set reaching the MSW fetch handler at interceptor-msw.ts:100-110. This requires the registry to have registered a module under an unhandled type string, which the public API cannot do.

Common situations: Vitest internal regression where a new mock type was added to registry but not to the interceptor switch; version skew between @vitest/mocker builds; corrupted module object in the registry.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/989009ec131275ae.json. Report an issue: GitHub.