ITADN

[Bug]: toMatchObject does not accept some common types

#16195Closeddmurvihill 创建于 2026-05-18
Needs TriageBug Report
D
dmurvihillcommented
### Version jest@30.4.2, ts-jest@29.4.0 ### Steps to reproduce ```typescript export class Session { id: string; username: string; userId: number; expiresAt: Date; jwt: string; } it('should set request.session with all Session fields after successful validation', async () => { const session = plainToInstance(Session, { id: ''; username: ''; userId: 0; expiresAt: new Date(); jwt: ''; }); request = { cookies: { session: jwt } }; authService.getValidSession.mockResolvedValue(session); await authGuard.canActivate(context as ExecutionContext); expect(request.session).toMatchObject(session); // error }); ``` ### Expected behavior Type checker should accept test, provided authGuard and authService are implemented as expected ### Actual behavior ``` FAIL src/auth/auth.guard.spec.ts ● Test suite failed to run src/auth/auth.guard.spec.ts:94:45 - error TS2345: Argument of type 'Session' is not assignable to parameter of type 'Record<string, unknown> | Record<string, unknown>[]'. Type 'Session' is not assignable to type 'Record<string, unknown>'. Index signature for type 'string' is missing in type 'Session'. 94 expect(request.session).toMatchObject(session); ~~~~~~~ ``` ### Additional context https://github.com/microsoft/TypeScript/issues/15300 ### Environment ```shell dolan@Dolans-MacBook-Pro code % npx envinfo --preset jest System: OS: macOS 15.7.3 CPU: (8) arm64 Apple M1 Pro Binaries: Node: 24.11.0 - ~/.nvm/versions/node/v24.11.0/bin/node Yarn: 1.22.21 - ~/.nvm/versions/node/v24.11.0/bin/yarn npm: 11.11.1 - ~/.nvm/versions/node/v24.11.0/bin/npm pnpm: 8.15.4 - ~/.nvm/versions/node/v24.11.0/bin/pnpm ```
关闭于 2026-05-19 1 条评论