How do I run a single test using Jest?

后端 未结 14 1837
轮回少年
轮回少年 2020-12-12 10:22

I have a test \'works with nested children\' within the file fix-order-test.js.

Running the below runs all the tests in the file.

jest fix-order-test         


        
相关标签:
14条回答
  • 2020-12-12 10:37

    Full command to run a single Jest test

    Command:

    node <path-to-jest> -i <you-test-file> -c <jest-config> -t "<test-block-name>"

    • <path-to-jest>:
      • Windows: node_modules\jest\bin\jest.js
      • Others: node_modules/.bin/jest
    • -i <you-test-file>: path to the file with tests (js or ts)
    • -c <jest-config>: path to a separate Jest config file (JSON), if you keep your Jest configuration in package.json, you don't have to specify this parameter (Jest will find it without your help)
    • -t <the-name-of-test-block>: actually it's a name (the first parameter) of describe(...), it(...), or test(...) block.

    Example:

    describe("math tests", () => {
    
      it("1 + 1 = 2", () => {
        expect(1 + 1).toBe(2);
      });
    
      it("-1 * -1 !== -1", () => {
        expect(-1 * -1).not.toBe(-1);
      });
    
    });
    

    So, the command

    node node_modules/jest/bin/jest.js -i test/math-tests.js -c test/tests-config.json -t "1 + 1 = 2"
    

    will test it("1 + 1 = 2", ...), but if you change the -t parameter to "math tests" then it will run both tests from the describe("math tests",...) block.

    Remarks:

    1. For Windows, replace node_modules/.bin/jest with node_modules\jest\bin\jest.js.
    2. This approach allows you to debug the running script. To enable debugging add '--inspect-brk' parameter to the command.

    Running a single Jest test via NPM scripts in 'package.json'

    Having Jest installed, you can simplify the syntax of this command (above) by using NPM scripts. In "package.json" add a new script to the "scripts" section:

    "scripts": {
      "test:math": "jest -i test/my-tests.js -t \"math tests\"",
    }
    

    In this case, we use an alias 'jest' instead of writing the full path to it. Also, we don't specify the configuration file path since we can place it in "package.json" as well and Jest will look into it by default. Now you can run the command:

    npm run test:math
    

    And the "math tests" block with two tests will be executed. Or, of course, you can specify one particular test by its name.

    Another option would be to pull the <the-name-of-test-block> parameter outside the "test:math" script and pass it from the NPM command:

    package.json:

    "scripts": {
      "test:math": "jest -i test/my-tests.js -t",
    }
    

    Command:

    npm run test:math "math tests"

    Now you can manage the name of the run test(s) with a much shorter command.

    Remarks:

    1. The 'jest' command will work with NPM scripts because

    npm makes "./node_modules/.bin" the first entry in the PATH environment variable when running any lifecycle scripts, so this will work fine, even if your program is not globally installed (NPM blog) 2. This approach doesn't seem to allow debugging because Jest is run via its binary/CLI, not via node.


    Running a selected Jest test in Visual Studio Code

    If you are using Visual Studio Code you can take advantage of it and run the currently selected test (in the code editor) by pressing the F5 button. To do this, we will need to create a new launch configuration block in the ".vscode/launch.json" file. In that configuration, we will use predefined variables which are substituted with the appropriate (unfortunately not always) values when running. Of all available we are only interested in these:

    • ${relativeFile} - the current opened file relative to ${workspaceFolder}
    • ${selectedText} - the current selected text in the active file

    But before writing out the launch configuration we should add the 'test' script in our 'package.json' (if we haven't done it yet).

    File package.json:

    "scripts": {
      "test": "jest"
    }
    

    Then we can use it in our launch configuration.

    Launch configuration:

    {
      "type": "node",
      "request": "launch",
      "name": "Run selected Jest test",
      "runtimeExecutable": "npm",
      "runtimeArgs": [
        "run-script",
        "test"
      ],
      "args": [
        "--",
        "-i",
        "${relativeFile}",
        "-t",
        "${selectedText}"
      ],
      "console": "integratedTerminal",
    }
    

    It actually does the same as the commands described earlier in this answer. Now that everything is ready, we can run any test we want without having to rewrite the command parameters manually.

    Here's all you need to do:

    1. Select currently created launch config in the debug panel:

    2. Open the file with tests in the code editor and select the name of the test you want to test (without quotation marks):

    3. Press F5 button.

    And voilà!

    Now to run any test you want. Just open it in the editor, select its name, and press F5.

    Unfortunately, it won't be "voilà" on a Windows machines because they substitute (who knows why) the ${relativeFile} variable with the path having reversed slashes and Jest wouldn't understand such a path.

    Remarks:

    1. To run under the debugger, don't forget to add the '--inspect-brk' parameter.
    2. In this configuration example, we don't have the Jest configuration parameter assuming that it's included in 'package.json'.
    0 讨论(0)
  • 2020-12-12 10:38

    npm test __tests__/filename.test.ts - to run a single file.

    test.only('check single test', () => { expect(true).toBe(true)}); - to run a single test case

    test.skip('to skip testcase, () => {expect(false).toBe(false_}); - to skip a test case

    0 讨论(0)
  • 2020-12-12 10:41

    Jest documentation recommends the following:

    If a test is failing, one of the first things to check should be whether the test is failing when it's the only test that runs. In Jest it's simple to run only one test - just temporarily change that test command to a test.only

    test.only('this will be the only test that runs', () => {
       expect(true).toBe(false);
    });
    

    or

    it.only('this will be the only test that runs', () => {
       expect(true).toBe(false);
    });
    
    0 讨论(0)
  • 2020-12-12 10:45

    If you have jest running as a script command, something like npm test, you need to use the following command to make it work:

    npm test -- -t "fix order test"
    
    0 讨论(0)
  • 2020-12-12 10:46

    Just a little add-on, because it seems there was kind of a fight if to use ./node_modules/.bin/jest -i ... or just jest -i ... or npm test -- -i ...

    1. Just calling jest works if you have it installed globally (as with npm install -g jest), a not-so-clean way of handling dependencies
    2. If you have Jest installed only locally in the package and want to call the Jest script without the npm script detour, you can use npx jest -i ... => this is exactly what npx is for. It saves you from writing ./node_modules/.bin/....
    0 讨论(0)
  • 2020-12-12 10:52

    There is now a nice Jest plugin for this called jest-watch-typeahead it makes this process much simpler.

    0 讨论(0)
提交回复
热议问题