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.

Categories
JavaScript Answers

How to test a single file using Jest?

To test a single file using Jest, we can specify the test file path after the jest command.

For instance, we run

npm test -- test.spec.js

assuming the test script runs jest.

test.spec.js is the path to the test file.

Categories
JavaScript Answers

How to run a single test using Jest?

To run a single test using Jest, we can run jest with the -t option.

For instance, we run:

jest -t 'order-test'

to run a test with the describe string set to 'order-test'.

In general, the format of the command is

jest -t '<describeString> <itString>'

Conclusion

To run a single test using Jest, we can run jest with the -t option.

Categories
JavaScript Answers React Native Answers

How to add a horizontal FlatList with multiple rows with React Native?

Sometimes, we want to add a horizontal FlatList with multiple rows with React Native

In this article, we’ll look at how to add a horizontal FlatList with multiple rows with React Native.

How to add a horizontal FlatList with multiple rows with React Native?

To add a horizontal FlatList with multiple rows with React Native, we can set the numColumns prop.

For instance, we write:

import * as React from 'react';
import { FlatList, Text, View, Dimensions } from 'react-native';
import Constants from 'expo-constants';
import { Card } from 'react-native-paper';

const flatListItems = Array(200)
  .fill()
  .map((_, i) => ({ title: i, id: i }));

const Item = (item) => {
  const { title } = item;
  return (
    <View
      style={{
        backgroundColor: '#f9c2ff',
        padding: 20,
        marginVertical: 8,
        marginHorizontal: 16,
      }}>
      <Text onPress={() => console.log(item)}>{title}</Text>
    </View>
  );
};

export default function App() {
  const renderItem = ({ item }) => <Item title={item.title} />;
  const keyExtractor = (item) => item.id;

  return (
    <View>
      <FlatList
        style={{ height: 300 }}
        data={flatListItems}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        numColumns={3}
      />
    </View>
  );
}

to set the numColumns prop of the FlatList to 3.

As a result, we should see 3 columns displayed in the FlatList.

Conclusion

To add a horizontal FlatList with multiple rows with React Native, we can set the numColumns prop.