Skip to content

[Bug]: toMatchObject does not accept some common types #16195

Description

@dmurvihill

Version

[email protected], [email protected]

Steps to reproduce

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

microsoft/TypeScript#15300

Environment

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions