Skip to content

Commit

Permalink
Merge pull request #26 from unexpectedjs/ssimonsen/testing-snapshot-o…
Browse files Browse the repository at this point in the history
…n-shifted-subjects

Testing snapshot inspected snapshots
  • Loading branch information
papandreou committed Jun 13, 2019
2 parents d8af52e + f54c27e commit 6008e99
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
16 changes: 11 additions & 5 deletions lib/ensureAfterHookIsRegistered.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,20 @@ function ensureAfterBlockIsRegistered(topLevelFixes) {
}
const fixes = [];
let stringifiedSubject;
let newAssertionName = 'to equal snapshot';
if (typeof subject === 'string') {
let newAssertionName = assertionName;
if (
typeof subject === 'string' &&
assertionName === 'to equal snapshot'
) {
stringifiedSubject = stringify(
subject,
indentationWidth,
expectForRendering
).replace(/\n^(?=[^\n])/gm, `\n${indent}`);
} else if (isSimpleObjectTree(subject, expectForRendering)) {
} else if (
isSimpleObjectTree(subject, expectForRendering) &&
assertionName === 'to equal snapshot'
) {
expectForRendering.output.indentationWidth = indentationWidth;
stringifiedSubject = expectForRendering
.inspect(subject)
Expand All @@ -227,7 +233,7 @@ function ensureAfterBlockIsRegistered(topLevelFixes) {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
node.arguments[1],
node.arguments[node.arguments.length - 1],
`'${newAssertionName}', ${stringifiedSubject}`
)
);
Expand All @@ -243,7 +249,7 @@ function ensureAfterBlockIsRegistered(topLevelFixes) {
if (newAssertionName !== assertionName) {
fixes.unshift(
ruleFixer.replaceText(
node.arguments[1],
node.arguments[node.arguments.length - 1],
`'${newAssertionName}'`
)
);
Expand Down
9 changes: 7 additions & 2 deletions lib/unexpected-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,18 @@ module.exports = {
'<any> to inspect as snapshot <string?>'
],
(expect, subject, ...args) => {
let subjectForAssertion;
if (expect.testDescription === 'to inspect as snapshot') {
subject = expectForRendering.inspect(subject).toString('text');
subjectForAssertion = expectForRendering
.inspect(subject)
.toString('text');
} else {
subjectForAssertion = subject;
}
if (args.length === 1) {
return expect.withError(
() => {
expect(subject, 'to equal', args[0]);
expect(subjectForAssertion, 'to equal', args[0]);
},
err => {
if (isNode) {
Expand Down
91 changes: 91 additions & 0 deletions test/unexpected-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,74 @@ expect.addAssertion(
}
);

describe('inspect as snapshot', () => {
it('should fill in a missing snapshot', function() {
return expect(
() => {
it('should foo', function() {
expect(['a', 'b', 'c'], 'to inspect as snapshot');
});
},
'to come out as',
() => {
it('should foo', function() {
expect(
['a', 'b', 'c'],
'to inspect as snapshot',
"[ 'a', 'b', 'c' ]"
);
});
}
);
});

it('should update incorrect snapshots', function() {
return expect(
() => {
it('should foo', function() {
expect(['a', 'b', 'c'], 'to inspect as snapshot', "['a', 'b', 'c']");
});
},
'to come out as',
() => {
it('should foo', function() {
expect(
['a', 'b', 'c'],
'to inspect as snapshot',
"[ 'a', 'b', 'c' ]"
);
});
}
);
});

it('supports inspected snapshots on shifted subjects', () => {
return expect(
() => {
it('should foo', function() {
expect(
['c', 'a', 'b'],
'when sorted',
'to inspect as snapshot',
"['a', 'b', 'c']"
);
});
},
'to come out as',
() => {
it('should foo', function() {
expect(
['c', 'a', 'b'],
'when sorted',
'to inspect as snapshot',
"[ 'a', 'b', 'c' ]"
);
});
}
);
});
});

describe('to equal snapshot', function() {
it('should fill in a missing single line string', function() {
return expect(
Expand Down Expand Up @@ -458,6 +526,29 @@ it('should foo', function() {
);
});

it('supports matching snapshots on shifted subjects', () => {
return expect(
() => {
it('should foo', function() {
expect(['c', 'a', 'b'], 'when sorted', 'to equal snapshot', [
'a',
'b'
]);
});
},
'to come out as',
() => {
it('should foo', function() {
expect(['c', 'a', 'b'], 'when sorted', 'to equal snapshot', [
'a',
'b',
'c'
]);
});
}
);
});

describe.skip('with expect.it', function() {
it('should fill in a missing string', function() {
return expect(
Expand Down

0 comments on commit 6008e99

Please sign in to comment.