{"id":"dc2501d4c85193fa","repo":"vitest-dev/vitest","slug":"async-schema-validation-is-not-supported-in-asymme","errorCode":null,"errorMessage":"Async schema validation is not supported in asymmetric matchers.","messagePattern":"Async schema validation is not supported in asymmetric matchers\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-asymmetric-matchers.ts","lineNumber":416,"sourceCode":"\nexport class SchemaMatching extends AsymmetricMatcher<StandardSchemaV1<unknown, unknown>> {\n  private result: StandardSchemaV1.Result<unknown> | undefined\n\n  constructor(sample: StandardSchemaV1<unknown, unknown>, inverse = false) {\n    if (!isStandardSchema(sample)) {\n      throw new TypeError(\n        'SchemaMatching expected to receive a Standard Schema.',\n      )\n    }\n    super(sample, inverse)\n  }\n\n  asymmetricMatch(other: unknown): boolean {\n    const result = this.sample['~standard'].validate(other)\n\n    // Check if the result is a Promise (async validation)\n    if (result instanceof Promise) {\n      throw new TypeError('Async schema validation is not supported in asymmetric matchers.')\n    }\n\n    this.result = result\n    const pass = !this.result.issues || this.result.issues.length === 0\n\n    return this.inverse ? !pass : pass\n  }\n\n  toString() {\n    return `Schema${this.inverse ? 'Not' : ''}Matching`\n  }\n\n  getExpectedType() {\n    return 'object'\n  }\n\n  toAsymmetricMatcher(): string {\n    const { utils } = this.getMatcherContext()","sourceCodeStart":398,"sourceCodeEnd":434,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-asymmetric-matchers.ts#L398-L434","documentation":"Thrown as a TypeError inside SchemaMatching.asymmetricMatch when the schema's validate() returns a Promise. Asymmetric matchers run synchronously within the equality check, so an async schema cannot be evaluated; Vitest refuses to silently return a wrong result.","triggerScenarios":"Using expect.schemaMatching(schema) where schema['~standard'].validate(value) returns a Promise (the validator does I/O, async refinements, or DB lookups). The match throws at comparison time, not construction.","commonSituations":"A zod/valibot schema with async refine/superRefine or async piped validators; a custom Standard Schema whose validate is async; moving a schema that worked in expect() into an asymmetric context.","solutions":["Use a synchronous schema variant for asymmetric matching (remove async refinements).","Validate explicitly with await schema['~standard'].validate(value) and assert on the result instead of using schemaMatching.","Refactor the async logic out of the schema used for matching."],"exampleFix":"// before\nconst schema = z.object({ email: z.string().email().refine(async () => checkDb()) })\nexpect(body).toEqual(expect.schemaMatching(schema)) // throws: async validate\n// after\nconst result = await schema['~standard'].validate(body)\nexpect(result.issues ?? []).toEqual([])","handlingStrategy":"validation","validationCode":"// detect async validation before using schemaMatching\nfunction isSyncSchema(schema: any): boolean {\n  const probe = {}\n  const result = schema['~standard'].validate(probe)\n  return !(result instanceof Promise)\n}\nif (!isSyncSchema(schema)) {\n  throw new TypeError('Use a sync schema for schemaMatching, or await validate() manually')\n}","typeGuard":"function isSyncValidator(schema: any): boolean {\n  // best-effort: probe with a sentinel once and cache\n  const r = schema['~standard'].validate(undefined)\n  return !(r instanceof Promise)\n}","tryCatchPattern":"try {\n  expect(value).toEqual(expect.schemaMatching(schema))\n} catch (e) {\n  if (/Async schema validation/i.test(String((e as Error).message))) {\n    const result = await schema['~standard'].validate(value)\n    expect(result.issues ?? []).toEqual([])\n  } else throw e\n}","preventionTips":["Avoid async refinements (refine/superRefine) in schemas used for asymmetric matching.","If async is required, validate with await and assert on the result instead.","Probe schemas once at module load to confirm validate() is synchronous."],"tags":["expect","asymmetric-matcher","standard-schema","async","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}