Skip to content

Commit

Permalink
Take compound assertions into account when replacing 'to equal snapsh…
Browse files Browse the repository at this point in the history
…ot' with 'to inspect as snapshot'
  • Loading branch information
papandreou committed Jun 16, 2019
1 parent 5f62694 commit c893771
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 29 deletions.
28 changes: 19 additions & 9 deletions lib/ensureAfterHookIsRegistered.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,38 @@ function ensureAfterBlockIsRegistered(topLevelFixes) {
inspect
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
}
// Take compound assertions into account:
const assertionArgument =
node.arguments[
node.arguments.length - (status === 'missing' ? 1 : 2)
];
const newAssertionNameWithPrefix = assertionArgument.value.replace(
assertionName,
newAssertionName
);

if (status === 'missing') {
if (newAssertionName !== assertionName) {
if (newAssertionName === assertionName) {
fixes.unshift(
ruleFixer.replaceText(
node.arguments[node.arguments.length - 1],
`'${newAssertionName}', ${stringifiedSubject}`
ruleFixer.insertTextAfter(
assertionArgument,
`, ${stringifiedSubject}`
)
);
} else {
fixes.unshift(
ruleFixer.insertTextAfter(
node.arguments[node.arguments.length - 1],
`, ${stringifiedSubject}`
ruleFixer.replaceText(
assertionArgument,
`'${newAssertionNameWithPrefix}', ${stringifiedSubject}`
)
);
}
} else if (status === 'mismatch') {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
node.arguments[node.arguments.length - 1],
`'${newAssertionName}'`
assertionArgument,
`'${newAssertionNameWithPrefix}'`
)
);
}
Expand Down
110 changes: 90 additions & 20 deletions test/unexpected-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,28 +487,98 @@ it('should foo', function() {
);
});

it('should switch to "to inspect as snapshot" when the subject contains a circular reference', function() {
return expect(
() => {
it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(foo, 'to equal snapshot');
});
},
'to come out as',
function() {
it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(
foo,
'to inspect as snapshot',
'{ bar: 123, quux: [Circular] }'
describe('when the subject contains a circular reference', function() {
it('should switch to "to inspect as snapshot"', function() {
return expect(
() => {
it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(foo, 'to equal snapshot');
});
},
'to come out as',
function() {
it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(
foo,
'to inspect as snapshot',
'{ bar: 123, quux: [Circular] }'
);
});
}
);
});

describe('with a compound assertion', function() {
it('should switch to "to inspect as snapshot"', function() {
return expect(
() => {
expect.addAssertion('<any> noop <assertion>', expect =>
expect.shift()
);

it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(foo, 'noop to equal snapshot', { bar: 456 });
});
},
'to come out as',
function() {
expect.addAssertion('<any> noop <assertion>', expect =>
expect.shift()
);

it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(
foo,
'noop to inspect as snapshot',
'{ bar: 123, quux: [Circular] }'
);
});
}
);
});

describe('and no current snapshot', function() {
it('should switch to "to inspect as snapshot"', function() {
return expect(
() => {
expect.addAssertion('<any> noop <assertion>', expect =>
expect.shift()
);

it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(foo, 'noop to equal snapshot');
});
},
'to come out as',
function() {
expect.addAssertion('<any> noop <assertion>', expect =>
expect.shift()
);

it('should foo', function() {
const foo = { bar: 123 };
foo.quux = foo;
expect(
foo,
'noop to inspect as snapshot',
'{ bar: 123, quux: [Circular] }'
);
});
}
);
});
}
);
});
});
});

it('should switch to "to inspect as snapshot" when the subject contains an object that does not have Object.prototype as its constructor', function() {
Expand Down

0 comments on commit c893771

Please sign in to comment.