Skip to content

Instantly share code, notes, and snippets.

@megamaddu
Last active June 2, 2017 16:18
Show Gist options
  • Save megamaddu/6547652e16be5d99a663ed657343c022 to your computer and use it in GitHub Desktop.
Save megamaddu/6547652e16be5d99a663ed657343c022 to your computer and use it in GitHub Desktop.
Comparing your usual unit tests to snapshot tests. The output on failure is the same. If I'd been lazier on the normal version and only checked a couple properties the snapshot test would be much more thorough by comparison. You can see why it becomes more beneficial as the data size grows (like a React element tree or an API response).
import * as sinon from 'sinon'
import { setCookie, clearCookie } from './cookies'
describe('cookies', () => {
it('setCookie (defaults)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test', 'val')
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test',
'val',
{
domain: 'test.host',
httpOnly: true,
maxAge: 10000,
overwrite: true
}
])
})
it('setCookie (config overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test-overrides', 'val')
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test-overrides',
'val',
{
domain: 'overridden.host',
httpOnly: true,
maxAge: 10000,
overwrite: false
}
])
})
it('setCookie (direct overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test', 'val', { domain: undefined, httpOnly: false })
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test',
'val',
{
domain: undefined,
httpOnly: false,
maxAge: 10000,
overwrite: true
}
])
})
it('setCookie (both overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test-overrides', 'val', { domain: undefined })
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test-overrides',
'val',
{
domain: undefined,
httpOnly: true,
maxAge: 10000,
overwrite: false
}
])
})
it('clearCookie (defaults)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test')
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test',
undefined,
{
domain: 'test.host',
httpOnly: true,
maxAge: 10000,
overwrite: true
}
])
})
it('clearCookie (config overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test-overrides')
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test-overrides',
undefined,
{
domain: 'overridden.host',
httpOnly: true,
maxAge: 10000,
overwrite: false
}
])
})
it('clearCookie (direct overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test', { domain: undefined, httpOnly: false })
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test',
undefined,
{
domain: undefined,
httpOnly: false,
maxAge: 10000,
overwrite: true
}
])
})
it('clearCookie (both overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test-overrides', { domain: undefined })
expect(spy.callCount).toBe(1)
expect(spy.firstCall.args).toEqual([
'test-overrides',
undefined,
{
domain: undefined,
httpOnly: true,
maxAge: 10000,
overwrite: false
}
])
})
})
import * as sinon from 'sinon'
import { setCookie, clearCookie } from './cookies'
describe('cookies', () => {
it('setCookie (defaults)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test', 'val')
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('setCookie (config overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test-overrides', 'val')
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('setCookie (direct overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test', 'val', { domain: undefined, httpOnly: false })
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('setCookie (both overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
setCookie(ctx as any, 'test-overrides', 'val', { domain: undefined })
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('clearCookie (defaults)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test')
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('clearCookie (config overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test-overrides')
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('clearCookie (direct overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test', { domain: undefined, httpOnly: false })
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
it('clearCookie (both overrides)', () => {
const spy = sinon.spy()
const ctx = { cookies: { set: spy } }
clearCookie(ctx as any, 'test-overrides', { domain: undefined })
expect(spy.callCount).toMatchSnapshot()
expect(spy.firstCall.args).toMatchSnapshot()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment