First Steps with Jest: Writing and Running Your Initial Test Quiz

Explore essential concepts for creating, structuring, and executing your first test using the Jest testing framework. This quiz covers syntax, configuration, and best practices for new users aiming to verify JavaScript code with Jest.

  1. Identifying the Correct Test Syntax

    Which of the following shows the correct basic syntax for writing a test case in Jest to check if a function 'add' returns 5 when given 2 and 3?

    1. test('adds numbers', () => { expect(add(2, 3)).toBe(5); });
    2. check('adds numbers', { expect(add(2, 3)).equals(5); });
    3. it('adds numbers', () => expect(add(2, 3)) == 5);
    4. describe('add numbers', () => { test(add(2, 3), toBe(5)); });

    Explanation: The correct syntax uses the 'test' function with a string description and a callback containing an 'expect' statement with 'toBe' for exact value checking. 'check' and 'equals' are not valid Jest keywords, making option B incorrect. Option C incorrectly uses '==' instead of 'toBe', and omits braces for clarity. Option D incorrectly places arguments in the test call and misuses 'describe' syntax. Only option A demonstrates the correct structure.

  2. Locating Test Files by Convention

    According to common conventions in Jest, which file name is most likely to be automatically recognized as a test file?

    1. math.test.js
    2. test-mathfile.js
    3. mathSpec.js
    4. maths-checklist.js

    Explanation: Jest automatically detects files with '.test.js' as test files. 'test-mathfile.js' does not match the convention, so it is not automatically picked up. 'mathSpec.js' uses a different naming scheme not recognized by default, and 'maths-checklist.js' is a generic name rather than a test identifier. This makes 'math.test.js' the most appropriate for automatic recognition.

  3. Running Tests from the Command Line

    If you have installed Jest locally, which command would you typically run in the terminal to execute your tests?

    1. npx jest
    2. run jest
    3. npm run unit
    4. jest-start

    Explanation: Using 'npx jest' invokes the local Jest executable directly, which is the common method to run all tests. 'run jest' and 'jest-start' are not standard commands in this context. 'npm run unit' would only work if a script named 'unit' is defined in your configuration, which is not the default. 'npx jest' ensures Jest is located and executed properly.

  4. Understanding Assertions in Jest

    When writing your first expectation in Jest, what is the purpose of using the 'expect' function?

    1. To declare the result you want to check in your test case
    2. To initialize a test file for execution
    3. To execute your main application code
    4. To simulate asynchronous operations in the test

    Explanation: The 'expect' function is used to define the value you are checking, so it serves as the basis for each assertion in your test. It does not initialize test files (B), run the main application code (C), or specifically handle asynchronous logic (D)—though it can be used in async tests. The primary goal is to specify and verify expected outcomes.

  5. Arrangement of Test Functions and Descriptions

    In a simple Jest test, which element should directly describe the behavior or outcome you are testing?

    1. The first argument (description) of the test function
    2. The file name containing the test
    3. A comment above the test function
    4. The callback function inside the test

    Explanation: The first argument of the 'test' function is a descriptive string explaining what is being tested, making your test readable and maintainable. The file name provides context but doesn't specify individual test behavior. Comments can help but are not part of Jest's reporting system. The callback function holds the code to execute the test, not the description.