Explore essential topics in introductory Jest testing. This quiz helps beginners strengthen knowledge on test syntax, configuration, assertions, and key terminology within the tools ecosystem using Jest.
Which Jest method is used to group related tests together under a shared description?
Explanation: The 'describe' method in Jest is specifically used to group related tests under a common description, making test suites more organized. 'itOnly' is not a method in Jest and is a common typo for 'it.only'. 'testcase' is not a recognized Jest keyword and may be confused with similar terms in other frameworks. 'runSuite' does not exist in Jest and is incorrect. Using 'describe' improves readability and test management.
Which assertion would you use in Jest to verify that a value is exactly equal to another value, including type?
Explanation: 'toBe' is used in Jest to check strict equality, comparing both value and type as in the JavaScript '===' operator. 'toEqual' compares values but allows for deep equality, so it is not as strict as 'toBe'. 'toContain' checks if an item is in a collection, not strict equality. 'toMatchObject' is for matching subset of properties in objects. Only 'toBe' ensures value and type match exactly.
For what main purpose are mock functions commonly used in Jest tests?
Explanation: Mock functions in Jest allow developers to simulate external modules or dependencies, making it possible to test units in isolation. Improving code performance is not the role of mock functions. They do not generate documentation automatically. Sorting test results is unrelated to mocks and handled elsewhere. Simulating dependencies ensures tests do not rely on real external systems.
Which of the following is an accepted naming convention for Jest test files within a project?
Explanation: 'example.test.js' follows the recommended naming pattern for Jest test files, which includes '.test.js'. 'example.testfile' is missing the '.js' extension and is not standard. 'test_example.js' does not follow the default Jest pattern, which expects the 'test' suffix or folder. 'examplecheck.js' is not recognized by Jest unless specified. Adhering to conventions ensures seamless test discovery.
What is the effect of running Jest in watch mode in an actively developed project?
Explanation: Jest's watch mode monitors file changes and re-executes relevant tests, enhancing developer productivity by providing immediate feedback. Running all tests once and exiting is the default mode, not the watch mode. Tests running in random order by default does not describe watch mode. Limiting execution to a single suite is incorrect, as watch mode focuses on changed tests. Automatic re-running is a key feature of watch mode.