Categories
JavaScript Answers

How to stop test suite after first fail with Jest?

Sometimes, we want to stop test suite after first fail with Jest.

In this article, we’ll look at how to stop test suite after first fail with Jest.

How to stop test suite after first fail with Jest?

To stop test suite after first fail with Jest, we can define our own test function.

For instance w ewrite:

let hasTestFailed = false
const sequentialTest = (name, action) => {
  test(name, async () => {
    if (hasTestFailed) {
      console.warn(`[skipped]: ${name}`)
    } else {
      try {
        await action()
      } catch (error) {
        hasTestFailed = true
        throw error
      }
    }
  })
}
describe('Test scenario 1', () => {
  sequentialTest('that item can be created', async () => {
    expect(true).toBe(false)
  })

  sequentialTest('that item can be deleted', async () => {
    //...
  })
})

to define the sequentialTest function that takes the same arguments as test.

In it, we call test with a function that check a test failed by catching the action test callback.

If an error is thrown, then it’s failed and hasTestFailed is set to true.

And we rethrow the error so the test suite will stop running.

Conclusion

To stop test suite after first fail with Jest, we can define our own test function.

Categories
JavaScript Answers

How to pass an object to expect().toBeCalledWith() with Jest?

Sometimes, we want to pass an object to expect().toBeCalledWith() with Jest.

In this article, we’ll look at how to pass an object to expect().toBeCalledWith() with Jest.

How to pass an object to expect().toBeCalledWith() with Jest?

To pass an object to expect().toBeCalledWith() with Jest, we should use the object as the argument of the expect.objectContaining method.

For instance, we write:

expect(api.submitForm).toBeCalledWith(
  expect.objectContaining({
    foo: 'foo',
    bar: 'bar'
  }),
);

to call expect.objectContaining with the object that we want to check api.submitForm is called with.

api.submitForm is the function being spied on.

Conclusion

To pass an object to expect().toBeCalledWith() with Jest, we should use the object as the argument of the expect.objectContaining method.

Categories
JavaScript Answers

How to mock a dependency’s constructor with Jest?

To mock a dependency’s constructor with Jest, we can call jest.mock.

For instance, we write

import * as AWS from 'aws-sdk';

jest.mock('aws-sdk', () => {
  return {
    CloudWatch: jest.fn().mockImplementation(() => {
      return {}
    })
  }
});

test('AWS.CloudWatch is called', () => {
  new AWS.CloudWatch();
  expect(AWS.CloudWatch).toHaveBeenCalledTimes(1);
});

to mock the CloudWatch constructor with a function that returns an empty object.

We call jest.fn to return a mocked function and we mock the return value of the function with mockImplementation.

Then we can use the AWS.CloudWatch constructor to run the mocked version of the constructor.

We still have to import the real dependency with

import * as AWS from 'aws-sdk';

before we can mock AWS.CloudWatch.

Categories
JavaScript Answers

How to run Jest tests only for current folder?

Sometimes, we want to run Jest tests only for current folder.

In this article, we’ll look at how to run Jest tests only for current folder.

How to run Jest tests only for current folder?

To run Jest tests only for current folder, we can run jest with the path of the folder with the tests we want to run.

For instance, we run

npm test dev/app

to run the tests in the dev/app folder given that the test script runs jest.

We can also set the testPathPattern option to the folder with the tests we want to run.

For instance, we run

jest --watchAll --testPathPattern=dev/app

to run the tests in dev/app.

Conclusion

To run Jest tests only for current folder, we can run jest with the path of the folder with the tests we want to run.

Categories
JavaScript Answers

How to mock platform detection in Jest and React Native?

Sometimes, we want to mock platform detection in Jest and React Native.

In this article, we’ll look at how to mock platform detection in Jest and React Native.

How to mock platform detection in Jest and React Native?

To mock platform detection in Jest and React Native, we can call jest.mock to mock the Platform module.

For instance, we write:

jest.mock('Platform', () => {
  const Platform = require.requireActual('Platform');
  Platform.OS = 'android';
  return Platform;
});

to call jest.mock with 'Platform and a function that return the Platform object.

Before we return it, we set the Platform.OS property to 'android' so Platform.OS would be 'android' in our tests.

Conclusion

To mock platform detection in Jest and React Native, we can call jest.mock to mock the Platform module.