How to run a single test with Mocha?

前端 未结 12 1161
無奈伤痛
無奈伤痛 2020-12-04 05:46

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?

12条回答
  •  时光说笑
    2020-12-04 05:54

    For those who are looking to run a single file but they cannot make it work, what worked for me was that I needed to wrap my test cases in a describe suite as below and then use the describe title e.g. 'My Test Description' as pattern.

    describe('My Test Description', () => {
      it('test case 1', () => {
        // My test code
      })
      it('test case 2', () => {
      // My test code
      })
    })
    

    then run

    yarn test -g "My Test Description"

    or

    npm run test -g "My Test Description"

提交回复
热议问题