Skip to content

Commit

Permalink
objectContaining matcher to output object representation when a test …
Browse files Browse the repository at this point in the history
…is failing
  • Loading branch information
srosato authored and NagRock committed Jul 1, 2021
1 parent 8f8d59a commit 0b18158
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/matcher/type/ObjectContainingMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export class ObjectContainingMatcher extends Matcher {
}

public toString(): string {
return `objectContaining(${this.expectedValue})`;
return `objectContaining(${JSON.stringify(this.expectedValue)})`;
}
}
47 changes: 42 additions & 5 deletions test/matcher/type/ObjectContainingMatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,49 @@ describe("ObjectContainingMatcher", () => {
});

describe("when given value doesn't contain given object", () => {
it("returns false", () => {
// when
const result = testObj.match({b: {c: "c"}});
describe("when given value is an object", () => {
it("returns false", () => {
// when
const result = testObj.match({b: {c: "c"}});

// then
expect(result).toBeFalsy();
// then
expect(result).toBeFalsy();
});

it("represents the object as a json string", () => {
// when
const result = testObj.toString();

// then
expect(result).toContain(`objectContaining({"b":{"c":"c","d":{}}`);
});
});

describe("when given value is a scalar", () => {
const testCases = [
{ scalar: 123, expected: 123 },
{ scalar: 123.456, expected: 123.456 },
{ scalar: "a", expected: '"a"' },
];

testCases.forEach(test => {
it(`represents the scalar value '${test.scalar}' as string`, () => {
// when
const testValue: Matcher = objectContaining(test.scalar);
const result = testValue.toString();

// then
expect(result).toContain(`objectContaining(${test.expected})`);
});

it("returns false", () => {
// when
const result = testObj.match(test.scalar);

// then
expect(result).toBeFalsy();
});
});
});
});
});
Expand Down

0 comments on commit 0b18158

Please sign in to comment.