mirror of
https://github.com/hanzoai/jest.git
synced 2026-09-24 16:59:57 +00:00
fix(expect): accept class instances in toMatchObject and objectContaining (#16196)
This commit is contained in:
@@ -33,6 +33,7 @@ yarn test-ci-partial:parallel --max-workers <N> --shard=<M>/<N> # CI-mode shar
|
||||
- New test files are `.ts` (some legacy `.js` remain).
|
||||
- Each `__tests__/` directory under packages covered by `yarn typecheck:tests` has its own `tsconfig.json` extending `tsconfig.test.json`. Add `"node"` to its `types` array when using Node globals like `Console`/`Stats`/`__dirname`.
|
||||
- **`yarn typecheck:tests` is gated in CI** — must exit 0. Adding a new package's tests means appending it to the glob in `package.json`.
|
||||
- **Type tests for `expect` matchers belong in `packages/jest-types/__typetests__/expect/`**, not in `packages/expect/__typetests__/`. The `jest-types` suite tests the public `@jest/globals` surface (what users import); `packages/expect/__typetests__/` covers internal `expect`-package concerns only (e.g. `MatcherFunction`, `JestExpect` shape). When adding type tests for matcher signatures, add them to `jest-types`.
|
||||
- **E2E tests (`e2e/__tests__/`) can't use `jest.mock`/`jest.fn`** — ESLint enforces this. Use fixture files instead.
|
||||
- Some e2e tests need Mercurial: `brew install hg`.
|
||||
- **Docblock pragmas** in test files: `@jest-environment <name>` overrides the test environment; `@jest-environment-options {"key": value}` merges into `testEnvironmentOptions`. Both are extracted by `jest-runner` and apply only to that file.
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
### Fixes
|
||||
|
||||
- `[expect, jest-message-util, jest-pattern, jest-regex-util, jest-util]` Revert `node:` protocol imports to restore webpack/browser-bundle compatibility ([#16167](https://github.com/jestjs/jest/pull/16167))
|
||||
- `[expect]` Widen `toMatchObject` and `objectContaining` parameter type from `Record<string, unknown>` to `object` so class instances are accepted ([#16196](https://github.com/jestjs/jest/pull/16196))
|
||||
- `[@jest-environment/jsdom-abstract]` Make `@types/jsdom` a peer dependency ([#16166](https://github.com/jestjs/jest/pull/16166))
|
||||
- `[jest-runtime]` Fall back to native ESM when a `.js` file contains ESM syntax but has no `"type":"module"` marker ([#16152](https://github.com/jestjs/jest/pull/16152))
|
||||
- `[jest-runtime]` Support older test environments whose `moduleMocker` does not implement `clearMocksOnScope` ([#16169](https://github.com/jestjs/jest/pull/16169))
|
||||
|
||||
@@ -119,7 +119,7 @@ export interface AsymmetricMatchers {
|
||||
arrayContaining(sample: Array<unknown>): AsymmetricMatcher;
|
||||
arrayOf(sample: unknown): AsymmetricMatcher;
|
||||
closeTo(sample: number, precision?: number): AsymmetricMatcher;
|
||||
objectContaining(sample: Record<string, unknown>): AsymmetricMatcher;
|
||||
objectContaining(sample: object): AsymmetricMatcher;
|
||||
stringContaining(sample: string): AsymmetricMatcher;
|
||||
stringMatching(sample: string | RegExp): AsymmetricMatcher;
|
||||
}
|
||||
@@ -298,9 +298,7 @@ export interface Matchers<R extends void | Promise<void>, T = unknown> {
|
||||
/**
|
||||
* Used to check that a JavaScript object matches a subset of the properties of an object
|
||||
*/
|
||||
toMatchObject(
|
||||
expected: Record<string, unknown> | Array<Record<string, unknown>>,
|
||||
): R;
|
||||
toMatchObject(expected: object | Array<object>): R;
|
||||
/**
|
||||
* Use to test that objects have the same types as well as structure.
|
||||
*/
|
||||
|
||||
@@ -69,6 +69,16 @@ expect(
|
||||
expect(
|
||||
jestExpect({a: 1}).toEqual(jestExpect.objectContaining({a: 1})),
|
||||
).type.toBe<void>();
|
||||
|
||||
class SessionForContaining {
|
||||
id = '';
|
||||
}
|
||||
expect(
|
||||
jestExpect({id: ''}).toEqual(
|
||||
jestExpect.objectContaining(new SessionForContaining()),
|
||||
),
|
||||
).type.toBe<void>();
|
||||
|
||||
expect(
|
||||
jestExpect({a: 1}).toEqual(jestExpect.objectContaining(1)),
|
||||
).type.toRaiseError();
|
||||
@@ -266,6 +276,16 @@ expect(jestExpect({a: 1, b: 2}).toMatchObject({b: 2})).type.toBe<void>();
|
||||
expect(
|
||||
jestExpect([{a: 1}, {b: 2, c: true}]).toMatchObject([{a: 1}, {b: 2}]),
|
||||
).type.toBe<void>();
|
||||
|
||||
class Session {
|
||||
id = '';
|
||||
username = '';
|
||||
userId = 0;
|
||||
}
|
||||
const session = new Session();
|
||||
expect(jestExpect({}).toMatchObject(session)).type.toBe<void>();
|
||||
expect(jestExpect({}).toMatchObject([session, session])).type.toBe<void>();
|
||||
|
||||
expect(jestExpect({c: true}).toMatchObject(true)).type.toRaiseError();
|
||||
expect(jestExpect({c: true}).toMatchObject()).type.toRaiseError();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user