Categories
JavaScript Answers

How to test the type of a thrown exception in Jest?

Sometimes, we want to test the type of a thrown exception in Jest.

In this article, we’ll look at how to test the type of a thrown exception in Jest.

How to test the type of a thrown exception in Jest?

To test the type of a thrown exception in Jest, we can use the thThrow method.

For instance, we write:

test("a test", () => {
  const t = () => {
    throw new TypeError();
  };
  expect(t).toThrow(TypeError);
});

to throw a TypeError in t.

Then we check that t throws a TypeError when we call it with expect(t).toThrow(TypeError);.

Conclusion

To test the type of a thrown exception in Jest, we can use the thThrow method.

Categories
JavaScript Answers

How to use ESLint with Jest?

Sometimes, we want to use ESLint with Jest.

In this article, we’ll look at how to use ESLint with Jest.

How to use ESLint with Jest?

To use ESLint with Jest, we add some entries into .eslintrc.

We add

"env": {
  "jest/globals": true
}

into .eslintrc.

And we may need plugins: ["jest"] in out ESLint config.

We can add rule overrides with the rules section as we have below.

module.exports = {
  extends: "eslint:recommended",
  env: {
    es6: true
  },
  overrides: [
    {
      files: [
        "**/*.test.js"
      ],
      env: {
        jest: true // now **/*.test.js files' env has both es6 *and* jest
      },]
      plugins: ["jest"],
      rules: {
        "jest/no-disabled-tests": "warn",
        "jest/no-focused-tests": "error",
        "jest/no-identical-title": "error",
        "jest/prefer-to-have-length": "warn",
        "jest/valid-expect": "error"
      }
    }
  ],
};

Conclusion

To use ESLint with Jest, we add some entries into .eslintrc.

Categories
JavaScript Answers

How to mock an ES6 module import using Jest?

Sometimes, we want to mock an ES6 module import using Jest.

In this article, we’ll look at how to mock an ES6 module import using Jest.

How to mock an ES6 module import using Jest?

To mock an ES6 module import using Jest, we can call the jest.mock method.

For instance, we write:

import myModule from './myModule';
import dependency from './dependency';
jest.mock('./dependency', () => ({
  doSomething: jest.fn()
}))

describe('myModule', () => {
  it('calls the dependency with double the input', () => {
    myModule(2);
    expect(dependency.doSomething).toBeCalledWith(4);
  });
});

where './dependency' is the path to the module our code imports.

And the function returns an object that has the mocked items of the dependency module.

We mock doSomething by setting it to a stubbed function returned by jest.fn.

Then we can call dependency.doSomething as we do in our regular code and check how many times it’s called with toBeCalledWith.

Conclusion

To mock an ES6 module import using Jest, we can call the jest.mock method.

Categories
JavaScript Answers

How to fix the error “Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout” with Jest?

Sometimes, we want to fix the error "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout" with Jest.

In this article, we’ll look at how to fix the error "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout" with Jest.

How to fix the error "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout" with Jest?

To fix the error "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout" with Jest, we call jest.setTimeout to set the timeout of the tests.

For instance, we write:

jest.setTimeout(30000);

in jest.config.js

to set the timeout of the tests in the test suite to 30 seconds.

As a result, our tests will time out in 30 seconds if they don’t finish by then.

Conclusion

To fix the error "Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout" with Jest, we call jest.setTimeout to set the timeout of the tests.

Categories
JavaScript Answers

What is the difference between ‘it’ and ‘test’ in Jest?

In this article, we’ll look at what is the difference between ‘it’ and ‘test’ in Jest.

What is the difference between ‘it’ and ‘test’ in Jest?

it and test are the same since it is an alias of test according to https://jestjs.io/docs/api#testname-fn-timeout

They exist to make the code read like English.

For instance, we write:

describe('yourModule', () => {
  test('if it does this thing', () => {});
  test('if it does the other thing', () => {});
});

or

describe('yourModule', () => {
  it('should do this thing', () => {});
  it('should do the other thing', () => {});
});

and they do the same thing.

Conclusion

it and test are the same since it is an alias of test according to https://jestjs.io/docs/api#testname-fn-timeout

They exist to make the code read like English.