Jest Configuration and Setup Files Essentials Quiz

Delve into the fundamental concepts of configuring and initializing test environments using Jest. This quiz evaluates your skills in setting up Jest through configuration files, using setup scripts, and understanding key properties for optimal test performance.

  1. Jest Config File Discovery

    Which configuration file will Jest most commonly look for by default to load its settings in a JavaScript project?

    1. jest.config.js
    2. jest-setup.js
    3. package.config.json
    4. jest.settings.js

    Explanation: The default configuration file that Jest searches for is 'jest.config.js', which allows you to define custom settings. While 'jest-setup.js' might sound related, it is not the recognized default config file name. 'package.config.json' is not associated with Jest, and 'jest.settings.js' is not a standard default that Jest recognizes. Only 'jest.config.js' will be automatically loaded for configuration by default.

  2. Setup Files Timing

    If you want to execute custom code before each test suite runs in Jest, which property should you use in the configuration?

    1. setupFilesAfterEnv
    2. testEnvironment
    3. globalSetup
    4. moduleDirectories

    Explanation: 'setupFilesAfterEnv' allows you to specify scripts that run after the test environment has been set up but before the tests themselves are executed. 'testEnvironment' is used to define the testing environment, not to run custom code. 'globalSetup' runs before all test suites, not before each one. 'moduleDirectories' is unrelated to test setup and controls module resolution paths. Thus, 'setupFilesAfterEnv' is the correct property.

  3. Global Setup Scripts

    For initializing resources needed before all tests run and cleaning them up afterward, which pair of Jest configuration properties should be used?

    1. globalSetup and globalTeardown
    2. setupFilesAfterEnv and moduleFileExtensions
    3. preset and testPathIgnorePatterns
    4. restoreMocks and transform

    Explanation: 'globalSetup' and 'globalTeardown' are intended for preparing and cleaning up resources before and after tests, such as databases or servers. 'setupFilesAfterEnv' is for per-suite initialization, and 'moduleFileExtensions' relates to file resolution. 'preset' and 'testPathIgnorePatterns' configure other aspects but not global scripts. 'restoreMocks' manages mock resets, and 'transform' handles file transformations. Only 'globalSetup' and 'globalTeardown' serve this lifecycle.

  4. Setup Scripts Example

    Suppose you want to automatically import a custom matcher library for all tests. Which Jest configuration field should list the path to your setup script?

    1. setupFilesAfterEnv
    2. roots
    3. collectCoverage
    4. watchPlugins

    Explanation: 'setupFilesAfterEnv' is designed for specifying scripts that set up the testing framework after the environment is ready, such as importing custom matchers. 'roots' determines where Jest looks for tests, not setup scripts. 'collectCoverage' toggles coverage collection and does not involve setup logic. 'watchPlugins' is for enhancing watch mode, not for importing scripts. Only 'setupFilesAfterEnv' performs the necessary role.

  5. Configuration Format Support

    Which of the following file types can NOT be directly used as a Jest configuration file by default?

    1. jest.config.md
    2. jest.config.js
    3. jest.config.json
    4. jest.config.ts

    Explanation: Jest does not support markdown files such as 'jest.config.md' as configuration files by default. 'jest.config.js', 'jest.config.json', and even 'jest.config.ts' (with the right setup) are all accepted formats for Jest configurations. Only markdown is an invalid choice, as it is not a valid script or structured data file for configuration.