Run only ONE test with Jest

后端 未结 4 1152
借酒劲吻你
借酒劲吻你 2020-12-28 11:10

Very simple, I want to run just one test with Jest.

I put it.only or describe.only but it still runs a whole lot of tests. I think it runs

相关标签:
4条回答
  • 2020-12-28 11:47

    it's like this:

    jest sometest.test.js -t "some expression to match a describe or a test"
    

    it will test all files with the name sometest.test.js and matching based on -t option, if you only want to test a specific file you can do this:

    jest src/user/.../sometest.test.js
    
    0 讨论(0)
  • 2020-12-28 11:49

    Jest parallelizes test runs and it doesn't know upfront which tests it should run and which it shouldn't run. This means when you use "fit", it will only run one test in that file but still run all other test files in your project.

    fit, fdescribe and it.only, describe.only have the same purpose, skip other tests, run only me.

    Source: https://github.com/facebook/jest/issues/698#issuecomment-177673281


    Use jest filtering mechanism, when you run your tests like

    jest --config=jest.config.json --watch
    

    You can filter tests by a testname or filename, just follow instructions in the terminal

    Press p, then type a filename

    Then you can use describe.only and it.only which will skip all other tests from filtered, tested file.

    0 讨论(0)
  • 2020-12-28 11:57

    For me it works if I use 2 params like this:

    yarn test --watch -f "src/...fullpath.../reducer.spec.js" -t "Name of the test"
    

    Flags:

    --watch: is optional
    
    -f: will do filtering of your files, so if you have a lot of tests with the same name, specify the full path to the exact file
    
    -t: works with 'describe' name or 'it' name of your test
    
    0 讨论(0)
  • 2020-12-28 12:03

    it.only and describe.only work only for the module they are in. If you are having problems to filter tests in multiple files, you can use jest -t name-of-spec, filtering tests that match the spec name (match against the name in describe or test).
    Source: https://facebook.github.io/jest/docs/en/cli.html

    For example, I focus the test which I'm currently writing like this (with the test script in the package.json):
    npm test -- -t "test foo"

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