Jest Cheatsheet 1(type)

kelly woo
2 min readJul 19, 2020

--

Let’s make a cheat sheet for jest.

toBe | toEqual

  • toBe: same as ‘===
const a = {a:1};expect(a).toBe({...a}) // error
expect(a).toBe(a); // pass
  • toEqaul: compare values
const a = { a: 1, b: undefined }expect(a).toEqual({...a}) // pass
expect(a).toEqual({a: 1}) // pass
expect(a).toEqual(expect.objectContaining({a: 1})) // pass

toEqual compares enumerable properties, and undefined value is considered as not declared(or vice-versa). See the following.

class A { a=1; b=1;  hi(){console.log('hi')}};
const a = { a: 1, b: 1, c: undefined };
const b = { ...a };
const c = { ...a };
Object.defineProperty(b, "b", { value: 1, enumerable: true});
Object.defineProperty(c, "b", { value: 1, enumerable: false});
expect(new A()).toEqual(a) // pass
expect(a).toEqual(b) // pass
expect(a).toEqual(c) // error

toBeInstanceOf & expect.any

  • toBeInstanceOf(Constructor): same as ‘instanceof’
const obj = {};
const arr = [];
const fn=()=>{};
class A {}
expect(obj).toBeInstanceOf(Object); // pass
expect(arr).toBeInstanceOf(Array); // pass
expect(arr).toBeInstanceOf(Object); // pass
expect(fn).toBeInstanceOf(Function) // pass
expect(A).toBeInstanceOf(Function); // pass
expect(new A()).toBeInstanceOf(A); // pass
const num = NaN;
const str = "";
const bool = false;
expect(num).toBeInstanceOf(Number); // error
expect(str).toBeInstanceOf(String); // error
expect(bool).toBeInstanceOf(Boolean); // error
expect(null).toBeInstanceOf(Object); // error
const numInst = new Number();
const strInst = new String();
const boolInst = new Boolean();
expect(num).toBeInstanceOf(Number); // pass
expect(str).toBeInstanceOf(String); // pass
expect(bool).toBeInstanceOf(Boolean); // pass
expect(num).toBeInstanceOf(Object); // pass
expect(str).toBeInstanceOf(Object); // pass
expect(bool).toBeInstanceOf(Object); // pass
  • expect.any(Constructor): when you see the code, it validates ‘ typeof || instanceof’ (except Boolean instance and symbol)
    use it with toEqual => expect.toEqual(expect.any(…))
const obj = {};
const arr = [];
class A {}
expect(obj).toEqual(expect.any(Object)); // pass
expect(arr).toEqual(expect.any(Array)); // pass
expect(arr).toEqual(expect.any(Object)); // pass
expect(A).toEqual(expect.any(Function)); // pass
expect(new A()).toEqual(expect.any(A)); // pass
const num = NaN;
const str = "";
const bool = false;
expect(num).toEqual(expect.any(Number)); // pass
expect(str).toEqual(expect.any(String)); // pass
expect(bool).toEqual(expect.any(Boolean)); // pass
expect(null).toEqual(expect.any(Object)); // pass

const numInst = new Number();
const strInst = new String();
const boolInst = new Boolean();
expect(numInst).toEqual(expect.any(Number)); // pass
expect(strInst).toEqual(expect.any(String)); // pass
expect(boolInst).toEqual(expect.any(Boolean)); // error
expect(numInst).toEqual(expect.any(Object)); // pass
expect(strInst).toEqual(expect.any(Object)); // pass expect(boolInst).toEqual(expect.any(Object)); // pass

Symbol does not work with neither toBeInstanceOf nor expect.any(Symbol) (guess it is not updated yet at expect.any(Symbol) since typeof supports Symbol)

Match For Special Value

  • null
expect(null).toBe(null); // pass
expect(null).toEqual(null); // pass
expect(null).toBeNull(); //
  • undefined
expect(undefined).toBe(undefined); // pass
expect(undefined).toEqual(undefined); // pass
expect(undefined).toBeUndefined(); // pass (<=> toBeDefined)
  • NaN
expect(NaN).toBe(NaN); // error like NaN === NaN
expect(NaN).toEqual(NaN); // pass
expect(NaN).toBeNaN(); // pass
  • anything (!null && !undefined)
const falsy = [0, '', NaN];expect(null).toEqual(expect.anything()) // error
expect(undefined).toEqual(expect.anything()) // error
falsy.forEach((v)=>{expect(v).toEqual(expect.anything())}) // pass

toBeTruthy & toBeFalsy

  • toBeTruthy & toBeFalsy: like the name
const truthy = ['1', 1, true, {}, [], Symbol()];
const falsy = ['', 0, false, null, undefined, NaN];
truthy.forEach((v) => expect(v).toBeTruthy()); // all passfalsy.forEach((v) => expect(v).toBeFalsy()); // all pass

and this is for sample where you can test.

next is for object(or array) compare.

--

--