Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: symbol key could not be enum. #14414

Merged
merged 9 commits into from Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions e2e/__tests__/__snapshots__/symbolKey.test.ts.snap
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`run test with symbol key 1`] = `
"Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites."
`;
19 changes: 19 additions & 0 deletions e2e/__tests__/symbolKey.test.ts
@@ -0,0 +1,19 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {extractSummary} from '../Utils';
import runJest from '../runJest';

test('run test with symbol key', () => {
const {exitCode, stderr} = runJest('symbol-key', [], {
nodeOptions: '--no-warnings',
});
eryue0220 marked this conversation as resolved.
Show resolved Hide resolved

const {summary} = extractSummary(stderr);

expect(exitCode).toBe(0);
expect(summary).toMatchSnapshot();
});
7 changes: 7 additions & 0 deletions e2e/symbol-key/__tests__/symbol.test.js
@@ -0,0 +1,7 @@
const foo = Symbol('foo');
const bar = Symbol('bar');
const obj = {[foo]: 'foo'};

it('symbol: objectNotContaining should pass', () => {
expect(obj).toEqual(expect.not.objectContaining({[bar]: 'bar'}));
});
5 changes: 5 additions & 0 deletions e2e/symbol-key/package.json
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
30 changes: 27 additions & 3 deletions packages/expect/src/asymmetricMatchers.ts
Expand Up @@ -52,7 +52,10 @@ function getPrototype(obj: object) {
return obj.constructor.prototype;
}

export function hasProperty(obj: object | null, property: string): boolean {
export function hasProperty(
obj: object | null,
property: string | symbol,
): boolean {
if (!obj) {
return false;
}
Expand Down Expand Up @@ -216,8 +219,10 @@ class ArrayContaining extends AsymmetricMatcher<Array<unknown>> {
}
}

class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
constructor(sample: Record<string, unknown>, inverse = false) {
class ObjectContaining extends AsymmetricMatcher<
Record<string | symbol, unknown>
> {
constructor(sample: Record<string | symbol, unknown>, inverse = false) {
super(sample, inverse);
}

Expand All @@ -232,6 +237,8 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
let result = true;

const matcherContext = this.getMatcherContext();
const objectSymbolKeys = Object.getOwnPropertySymbols(this.sample);

for (const property in this.sample) {
eryue0220 marked this conversation as resolved.
Show resolved Hide resolved
if (
!hasProperty(other, property) ||
Expand All @@ -246,6 +253,23 @@ class ObjectContaining extends AsymmetricMatcher<Record<string, unknown>> {
}
}

if (objectSymbolKeys.length > 0) {
for (let i = 0; i < objectSymbolKeys.length; i++) {
const property = objectSymbolKeys[i];
if (
!hasProperty(other, property) ||
!equals(
this.sample[property],
!other[property],
matcherContext.customTesters,
)
) {
result = false;
break;
}
}
}

return this.inverse ? !result : result;
}

Expand Down