Execute code after Jasmine test failure

前端 未结 4 540
故里飘歌
故里飘歌 2021-01-12 08:15

Is it possible to do something only if a Jasmine test fails? Juxtaposed with afterEach() which executes after an it() regardless of outcome, I\'m l

4条回答
  •  孤独总比滥情好
    2021-01-12 08:53

    I finally figured out how to grab a reference to the results of a failed spec with Jasmine 2.3.4, but I'm not sure it's going to be exactly what you're looking for.

    I used the vanilla PlayerSpec.js file that comes installed with the Jasmine setup. I ran the SpecRunner.html file to perform the test.

    Here is what I changed in the the PlayerSpec file:

    describe("Player", function() {
      var player;
      var song;
      var index = 0;
    
      beforeEach(function() {
        player = new Player();
        song = new Song();
        this.index = index++;
      });
    
      afterEach(function() {
        if (this.index > 0)
        {   var failed = jsApiReporter.specResults(this.index -1, 1)[0].failedExpectations;
            console.log('failed: ', failed);
            if (failed.length > 0)
            {
                console.log('After: ', this, failed[0].message);
                alert('ha');
            }
        }
      });
      it("should not fail", function()
      { expect(1).toEqual(2);
      });
    ...
    

    The rest of the file is the same as it originally was.

    Here is what I changed:

    Added an index variable to keep track of the current spec number.

    In the beforeEach function, I added the index value to the "this" object that gets passed between the it, beforeEach, and afterEach functions. This allows me to communicate between them all as necessary. I'm only using this mechanism to pass the index. Beware! If you just try to use the index value, it won't work! The functions process asynchronously, so there's a good possibility the index value will NOT be what you expect in the afterEach function.

    In the afterEach function, I check to make sure the index is greater than 0. In my local testing, the first spec fails, but that is not recognized until the SECOND time the afterEach is called. This is one of the reasons I'm not sure this will work llke you want. I then grab a reference to the failedExpectations and do some conditional processing if I recognize an error.

    The last change that is present is the addition of a new spec that will generate a failure.

    Here's a poor copy of my FireBug console results:

    failed: [Object { matcherName="toEqual",  message="Expected 1 to equal 2.",  stack="stack@http://localhost:4...ne-2.3.4/boot.js:110:5\n",  more...}]
    PlayerSpec.js (line 15)
    After: Object { index=1} Expected 1 to equal 2.
    PlayerSpec.js (line 18)
    failed: []
    PlayerSpec.js (line 15)
    failed: []
    PlayerSpec.js (line 15)
    failed: []
    PlayerSpec.js (line 15)
    failed: []
    PlayerSpec.js (line 15)
    

    This problem has been a journey for me. Sadly, I must move on to other things.

    I sincerely hope this either solves your problem or at least points you in the right direction. Hopefully, it will help others, too.

    Good Luck!

提交回复
热议问题