How to debug jest test cases(React)? – User friendly Tech help

Adding test cases to your code always helps in the long run, today we’re going to talk about how to debug test cases.

n

n

Do something today that your future self will thank you for

n

n

    n

  1. Using our all-time favorite Console.logn
     it('should call touch & asyncValidate methods', () => {n      const component = shallow(n        ,n      );nn      const mockFunction = jest.fn();n      console.log(mockFunction);n      component.instance().touchAndAsyncValidate('test', '1');nn      expect(mockFunction).toBeCalledWith('test');n    });

    n

    Using the debug() method of the wrapper with console.log

    n

    const component = shallow();nconsole.log(component.debug());
  2. n

  3. Using the Jest recommend way by using noden
      n

    1. n
        n

      1. Add debugger; where we want to break our test case
      2. n

      3. Run the command in terminaln
        node --inspect-brk node_modules/.bin/jest --runInBand

        n

        Debugger listening on ws://127.0.0.1:9229/c5ce34bb-64f3-4a59-8246-e21168ba8e26nFor help see https://nodejs.org/en/docs/inspector
      4. n

      5. Open the link in chrome chrome://inspect
      6. n

      7. Click on Open Dedicated DevTools for Node
      8. n

      9. Our script will break at the debugger
      10. n

      n

    2. n

    n

    n

  4. n

Was this article helpful?
YesNo

Similar Posts