Jest Snapshot Serializers

Luis Vieira
Jul 25, 2017 · 3 min read
var ToDos = [
{ 'Id': '1', 'Title': 'Shop groceries', 'Status': 'done' },
{ 'Id': '2', 'Title': 'Go to gym', 'Status': 'pending' },
{ 'Id': '3', 'Title': 'Kill it with fire', 'Status': 'done' },
{ 'Id': '4', 'Title': 'Take snapshots', 'Status': 'done' },
{ 'Id': '5', 'Title': 'Be awesome', 'Status': 'pending' },
];
function filterTodos(arr, key, term) {
return arr.filter(function(obj) {
return obj[key] === term
});
}
test('it returns all todos with matching Id',() =>{
expect(filterTodos(ToDos,'Id', '1')).toMatchSnapshot()
})
test('it returns all todos with matching Title',() =>{
expect(
filterTodos(ToDos,'Title', 'Shop groceries')).toMatchSnapshot()

})
test('it returns all todos with matching Status',() =>{
expect(filterTodos(ToDos,'Done', 1)).toMatchSnapshot()
})
exports[`it returns all todos with matching Id 1`] = `
Array [
Object {
"Id": "1",
"Status": "done",
"Title": "Shop groceries",
},
]
`;
exports[`it returns all todos with matching Status 1`] = `
Array [
Object {
"Id": "1",
"Status": "done",
"Title": "Shop groceries",
},
Object {
"Id": "3",
"Status": "done",
"Title": "Kill it with fire",
},
Object {
"Id": "4",
"Status": "done",
"Title": "Take snapshots",
},
]
`;
exports[`it returns all todos with matching Title 1`] = `
Array [
Object {
"Id": "1",
"Status": "done",
"Title": "Shop groceries",
},
]
`;
expect.addSnapshotSerializer({
test:(val) => val.Id && val.Title && val.Status && val.expiration,
print:(val) => `ID: ${val.Id}
Title: ${val.Title}
Status: ${val.Status}
Expiration: ${val.expiration}`
})
// Jest Snapshot v1, https://goo.gl/fbAQLPexports[`it returns all todos with matching Id: 1 1`] = `
Array [
ID: 1
Title: Shop groceries
Status: done
Expiration: 23/03/17
,
]
`;
exports[`it returns all todos with matching Status: done 1`] = `
Array [
ID: 1
Title: Shop groceries
Status: done
Expiration: 23/03/17
,
ID: 3
Title: Kill it with fire
Status: done
Expiration: 23/03/17
,
ID: 4
Title: Take snapshots
Status: done
Expiration: 23/03/17
,
]
`;
exports[`it returns all todos with matching Title: Shop groceries 1`] = `
Array [
ID: 1
Title: Shop groceries
Status: done
Expiration: 23/03/17
,
]
`;

Luis Vieira

Written by

A frontend developer that can handle its dose of UX and design.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade