{"record":{"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":"validation","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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-asymmetric-matchers.ts#L398-L434","documentation":"SchemaMatching's asymmetricMatch calls sample['~standard'].validate(other) and throws a TypeError if the result is a Promise. Asymmetric matchers are synchronous, so async schema validation (refinements, transforms that await) cannot be evaluated inline.","triggerScenarios":"Using expect.schemaMatching(schema) in a synchronous expect(...).toEqual(...) where the schema contains async refinements or async transforms, so validate() returns a Promise.","commonSituations":"Zod schemas with .refine(async ...), valibot schemas with async actions, or any schema whose validation pipeline is async; using schemaMatching inside toEqual instead of an async-aware assertion.","solutions":["Remove async refinements/transforms from the schema used in schemaMatching, or use a separate sync schema for the assertion.","Validate explicitly with await schema['~standard'].validate(value) inside an async test and assert on the issues array.","Use expect(await result).toEqual(...) patterns with a pre-validated value instead of the asymmetric matcher."],"exampleFix":"// before: schema has an async refinement\nconst schema = z.object({ email: z.string().email().refine(async (v) => await isUnique(v)) })\nexpect(payload).toEqual(expect.schemaMatching(schema))\n// after: validate explicitly in an async test\nconst result = await schema['~standard'].validate(payload)\nexpect(result.issues ?? []).toEqual([])","handlingStrategy":"validation","validationCode":"// Detect async validation up front and switch strategies.\nasync function validateSyncOrThrow(schema, value) {\n  const result = schema['~standard'].validate(value)\n  if (result instanceof Promise) {\n    throw new TypeError('Schema uses async validation; await it in the test instead.')\n  }\n  return result\n}","typeGuard":"function isSyncSchemaResult(result): boolean {\n  return !(result instanceof Promise)\n}","tryCatchPattern":"try {\n  expect(payload).toEqual(expect.schemaMatching(schema))\n} catch (e) {\n  if (/Async schema validation is not supported/.test(String(e?.message))) {\n    const r = await schema['~standard'].validate(payload)\n    expect(r.issues ?? []).toEqual([])\n    return\n  }\n  throw e\n}","preventionTips":["Keep schemas used in expect.schemaMatching synchronous (no async refinements/transforms).","Validate explicitly with await in async tests when async validation is required.","Unit-check that schema['~standard'].validate returns synchronously before using the matcher."],"tags":["expect","schema","async","asymmetric-matcher"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}