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.
Which configuration file will Jest most commonly look for by default to load its settings in a JavaScript project?
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.
If you want to execute custom code before each test suite runs in Jest, which property should you use in the configuration?
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.
For initializing resources needed before all tests run and cleaning them up afterward, which pair of Jest configuration properties should be used?
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.
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?
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.
Which of the following file types can NOT be directly used as a Jest configuration file by default?
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.