Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Testing that a Promise is resolved

Code Block
languagejs
// Remember to use the 'async', 'await', and 'expect' below.
it('tests that someMethodThatReturnsAPromise rejects with an error', async () => { 
  // first, set up data such that the promise should be resolved
  // second, call the method
  await expect(someMethodThatReturnsAPromise()).resolves.toBe(undefined);
});

Testing that a Promise is rejected

Code Block
languagejs
// Remember to use the 'async', 'await', and 'expect' below.
it('tests that someMethodThatReturnsAPromise rejects with an error', async () => { 
  // first, set up data such that the promise should be rejected
  // second, call the method  
  await expect(someMethodThatReturnsAPromise()).rejects
    .toThrow(new Error('The error sent to the reject.'));
});

Mock that a Promise was resolved

Code Block
languagejs
jest.spyOn(object, 'methodWithPromise').mockResolvedValue(undefined);

Mock that a Promise was rejected

Code Block
languagejs
jest.spyOn(object, 'methodWithPromise').mockRejectedValue(new Error('test'));

Mocking Top-level Objects (e.g. document)

Code Block
languagejs
jest.spyOn(global.document, 'referrer', 'get').mockReturnValue(loginUrl);

...

  1. add the line debugger; to your code where you want a breakpoint.
  2. From your project directory, run the following:

    Code Block
    languagebash
    node --inspect-brk node_modules/.bin/jest --runInBand


  3. Open chrome browser, and navigate to chrome://inspect and click the device.
  4. You may need to click Play to get to your debugger line.

...