How to run a single test with Mocha?

前端 未结 12 1121
無奈伤痛
無奈伤痛 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 06:01

    Just use .only before 'describe', 'it' or 'context'. I run using "$npm run test:unit", and it executes only units with .only.

    describe.only('get success', function() {
     // ...
    });
    
    it.only('should return 1', function() {
      // ...
    });
    
    0 讨论(0)
  • 2020-12-04 06:04

    You can try "it.only"

     it.only('Test one ', () => {
    
                expect(x).to.equal(y);
            });
    it('Test two ', () => {
    
                expect(x).to.equal(y);
            });
    

    in this the first one only will execute

    0 讨论(0)
  • 2020-12-04 06:07

    Not sure why the grep method is not working for me when using npm test. This works though. I also need to specify the test folder also for some reason.

    npm test -- test/sometest.js
    
    0 讨论(0)
  • 2020-12-04 06:09

    Hi above solutions didn't work for me. The other way of running a single test is

    mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
    

    This helps to run a test case from a single file and with specific name.

    0 讨论(0)
  • 2020-12-04 06:09

    Consolidate all your tests in one test.js file & your package json add scripts as:

      "scripts": {
      "api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
    },
    

    Type command on your test directory:

    npm run api:test
    
    0 讨论(0)
  • 2020-12-04 06:13

    Depending on your usage pattern, you might just like to use only. We use the TDD style; it looks like this:

    test.only('Date part of valid Partition Key', function (done) {
        //...
    }
    

    Only this test will run from all the files/suites.

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