How do I run a single test using Jest?

后端 未结 14 1836
轮回少年
轮回少年 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:28

    In Visual Studio Code, this lets me run/debug only one Jest test, with breakpoints: Debugging tests in Visual Studio Code

    My launch.json file has this inside:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Jest All",
          "program": "${workspaceFolder}/node_modules/.bin/jest",
          "args": ["--runInBand"],
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen",
          "windows": {
            "program": "${workspaceFolder}/node_modules/jest/bin/jest",
          }
        },
        {
          "type": "node",
          "request": "launch",
          "name": "Jest Current File",
          "program": "${workspaceFolder}/node_modules/.bin/jest",
          "args": ["${relativeFile}"],
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen",
          "windows": {
            "program": "${workspaceFolder}/node_modules/jest/bin/jest",
          }
        }
      ]
    }
    

    And this in file package.json:

      "scripts": {
        "test": "jest"
      }
    
    • To run one test, in that test, change test (or it) to test.only (or it.only). To run one test suite (several tests), change describe to describe.only.
    • Set breakpoint(s) if you want.
    • In Visual Studio Code, go to Debug View (Shift + Cmd + D or Shift + Ctrl + D).
    • From the dropdown menu at top, pick Jest Current File.
    • Click the green arrow to run that test.
    0 讨论(0)
  • 2020-12-12 10:28

    Use:

    npm run test -- test-name
    

    This will only work if your test specification name is unique.

    The code above would reference a file with this name: test-name.component.spec.ts

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

    Here is my take:

    ./node_modules/.bin/jest --config test/jest-unit-config.json --runInBand src/components/OpenForm/OpenForm.spec.js -t 'show expanded'
    

    Notes:

    • ./node_modules/.bin/... is a wonderful way, to access the locally installed Jest (or Mocha or...) binary that came with the locally installed package. (Yes, in your npm scripts you can jest with nothing before, but this is handy on command line... (that's also a good start for your debugging config, whichever IDE you are using...)
    • Your project might not have a set of configuration options. But if it does (peek into the scripts in package.json), this is, what you need.
    • --runInBand – as said, don't know about your configuration, but if you concentrate on developing/fixing a single test, you rather do not want to deal with web workers...
    • Yes, you can give the whole, explicit path to your file
    • Optionally, you can use -t to not run all tests in that file, but only a single one (here: the one, that has something with ‘show expanded’ in its name). Same effect can be achieved by glueing .only() into that file.
    0 讨论(0)
  • 2020-12-12 10:34

    You can also use f or x to focus or exclude a test. For example

    fit('only this test will run', () => {
       expect(true).toBe(false);
    });
    
    it('this test will not run', () => {
       expect(true).toBe(false);
    });
    
    xit('this test will be ignored', () => {
       expect(true).toBe(false);
    });
    
    0 讨论(0)
  • 2020-12-12 10:35

    As mentioned in other answers, test.only merely filters out other tests in the same file. So tests in other files would still run.

    So to run a single test, there are two approaches:

    • Option 1: If your test name is unique, you can enter t while in watch mode and enter the name of the test you'd like to run.

    • Option 2:

      1. Hit p while in watch mode to enter a regex for the filename you'd like to run. (Relevant commands like this are displayed when you run Jest in watch mode).
      2. Change it to it.only on the test you'd like to run.

    With either of the approaches above, Jest will only run the single test in the file you've specified.

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

    As said a previous answer, you can run the command

    jest -t 'fix-order-test'
    

    If you have an it inside of a describe block, you have to run

    jest -t '<describeString> <itString>'
    
    0 讨论(0)
提交回复
热议问题