Categories
JavaScript Answers

How to run Jest tests sequentially?

Sometimes, we want to run Jest tests sequentially.

In this article, we’ll look at how to run Jest tests sequentially.

How to run Jest tests sequentially?

To run Jest tests sequentially, we can use the --runInBand option.

We can run all tests sequentially with

jest --runInBand

Conclusion

To run Jest tests sequentially, we can use the --runInBand option.

Categories
JavaScript Answers

How to write async tests that expect toThrow with Jest?

Sometimes, we want to write async tests that expect toThrow with Jest.

In this article, we’ll look at how to write async tests that expect toThrow with Jest.

How to write async tests that expect toThrow with Jest?

To write async tests that expect toThrow with Jest, we can put await before expect and call toThrow.

For instance, we write

it('should test async errors', async () => {
  await expect(failingAsyncTest())
    .rejects
    .toThrow('I should fail');
});

to call failingAsyncTest in the test function.

We use await and rejects to get the rejected value if any and call thThrow to check what error message is given with the promise rejection.

Conclusion

To write async tests that expect toThrow with Jest, we can put await before expect and call toThrow.

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.