Skip to content

Instantly share code, notes, and snippets.

@arnaudrenaud
Created June 28, 2019 15:59
Show Gist options
  • Save arnaudrenaud/e0e36c30fa640df6e58edd5de06cf289 to your computer and use it in GitHub Desktop.
Save arnaudrenaud/e0e36c30fa640df6e58edd5de06cf289 to your computer and use it in GitHub Desktop.
Jest inline snapshots example
const parseDate = (year, fullDate) => {
if (!year) {
throw new Error('arg year is missing');
}
if (!fullDate) {
return new Date(year);
}
return new Date(fullDate);
};
module.exports = parseDate;
const parseDate = require('./index');
describe('index', () => {
describe('parseDate', () => {
describe('When no params passed', () => {
it('throws error', () => {
expect(() => parseDate()).toThrowErrorMatchingInlineSnapshot(
`"arg year is missing"`
);
});
});
describe('When passed year without fullDate', () => {
it('returns 01/01/year', () => {
expect(parseDate('1987')).toMatchInlineSnapshot(
`1987-01-01T00:00:00.000Z`
);
});
});
describe('When passed year and fullDate', () => {
it('returns full date', () => {
expect(parseDate('1987', '1987-07-12')).toMatchInlineSnapshot(
`1987-07-12T00:00:00.000Z`
);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment