How do I focus on one spec in jasmine.js?

前端 未结 9 1867
借酒劲吻你
借酒劲吻你 2020-11-29 23:41

I have a bunch of failing specs from a rather large architectural change. I\'d like to work on fixing them one by one by tagging each one with \'focus\'.

Does jasmin

相关标签:
9条回答
  • 2020-11-30 00:08

    You can run a single spec by using the url for the spec

    describe("MySpec", function() { 
      it('function 1', function() { 
        //... 
      }) 
    
      it('function 2', function() { 
        //... 
      } 
    
    }) 
    

    Now you can run just the whole spec by this url http://localhost:8888?spec=MySpec and a the first test with http://localhost:8888?spec=MySpec+function+1

    0 讨论(0)
  • 2020-11-30 00:12

    When using Karma, you can enable only one test with fit or fdescribe (iit and ddescribe in Jasmine before 2.1).


    This only runs Spec1:

    // or "ddescribe" in Jasmine prior 2.1
    fdescribe('Spec1', function () {
        it('should do something', function () {
            // ...
        });
    });
    
    describe('Spec2', function () {
        it('should do something', function () {
            // ...
        });
    });
    

    This only runs testA:

    describe('Spec1', function () {
    
        // or "iit" in Jasmine prior 2.1
        fit('testA', function () {
            // ...
        });
    
        it('testB', function () {
            // ...
        });
    
    });
    
    0 讨论(0)
  • 2020-11-30 00:19

    There are a few ways you can do it.

    There is: Jasmine's feature Focused Specs (2.2): http://jasmine.github.io/2.2/focused_specs.html

    Focusing specs will make it so that they are the only specs that run. Any spec declared with fit is focused.

    describe("Focused specs", function() {
      fit("is focused and will run", function() {
        expect(true).toBeTruthy();
      });
    
      it('is not focused and will not run', function(){
        expect(true).toBeFalsy();
      });
    });
    

    However, I don't really like the idea of editing my tests (fit and fdescribe) to run them selectively. I prefer to use a test runner like karma which can filter out tests using a regular expression.

    Here's an example using grunt.

    $ grunt karma:dev watch --grep=mypattern
    

    If you're using gulp (which is my favourite task runner), you can pass args into gulp-karma with yargs and match patterns by setting karma's config.

    Kinda like this:

    var Args = function(yargs) {
      var _match = yargs.m || yargs.match;
      var _file = yargs.f || yargs.file;
      return {
        match: function() { if (_match) { return {args: ['--grep', _match]} } }
      };
    }(args.argv);
    
    
    var Tasks = function() {
      var test = function() {
        return gulp.src(Files.testFiles)
          .pipe(karma({ configFile: 'karma.conf.js', client: Args.match()}))
          .on('error', function(err) { throw err; });
      };
    
      return {
        test: function() { return test() }
      }
    }(Args);
    
    gulp.task('default', ['build'], Tasks.test);
    

    See my gist: https://gist.github.com/rimian/0f9b88266a0f63696f21

    So now, I can run a single spec using the description:

    My local test run: (Executed 1 of 14 (skipped 13))

    gulp -m 'triggers the event when the API returns success'
    [20:59:14] Using gulpfile ~/gulpfile.js
    [20:59:14] Starting 'clean'...
    [20:59:14] Finished 'clean' after 2.25 ms
    [20:59:14] Starting 'build'...
    [20:59:14] Finished 'build' after 17 ms
    [20:59:14] Starting 'default'...
    [20:59:14] Starting Karma server...
    INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
    INFO [launcher]: Starting browser Chrome
    WARN [watcher]: All files matched by "/spec/karma.conf.js" were excluded.
    INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket hivjQFvQbPdNT5Hje2x2 with id 44705181
    Chrome 42.0.2311 (Mac OS X 10.10.3): Executed 1 of 14 (skipped 13) SUCCESS (0.012 secs / 0.009 secs)
    [20:59:16] Finished 'default' after 2.08 s
    

    Also see: https://github.com/karma-runner/karma-jasmine

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