Skip to content

Instantly share code, notes, and snippets.

@shts
Created March 5, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shts/ecaeaa09e7ae948eb47e9f22a85b423a to your computer and use it in GitHub Desktop.
Save shts/ecaeaa09e7ae948eb47e9f22a85b423a to your computer and use it in GitHub Desktop.
describe('Matchers', () => {
describe('Compare "toBe" to "toEqual"', () => {
it('number', () => {
expect(1 + 1).toBe(2) // ok
expect(1 + 1).toEqual(2) // ok
})
it('string', () => {
expect('d' + 'b').toBe('db') // ok
expect('d' + 'b').toEqual('db') // ok
})
it('object', () => {
const user = { name: 'Hoge', age: 30 }
// expect(user).toBe({ name: 'Hoge', age: 30 }) // Fail
expect(user).toEqual({ name: 'Hoge', age: 30 })
})
it('date', () => {
const date = new Date('2020/01/01 00:00:00')
// expect(date).toBe(new Date('2020/01/01 00:00:00')) // Fail
expect(date).toEqual(new Date('2020/01/01 00:00:00'))
})
it('class', () => {
class User {
age: number
constructor(age:number) {
this.age = age
}
}
const user = new User(30)
// expect(user).toBe(new User(30)) // Fail
expect(user).toEqual(new User(30))
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment