Skip to content Skip to sidebar Skip to footer

Testing If A Jasmine Test Fails

I'm trying to write a plugin for Jasmine that allows you to return a promise from a spec and will pass or fail that spec depending on whether or not the promise is fulfilled or rej

Solution 1:

After a conversation with the developers who work on Jasmine, we've come up with this:

varFAILED = 'failed'varPASSED = 'passed'describe('My Test Suite', function () {
    var env

    beforeEach(function () {
        // Create a secondary Jasmine environment to run your sub-specs in
        env = new jasmine.Env()
    })

    it('should work synchronously', function () {
        var spec

        // use the methods on `env` rather than the global ones for sub-specs// (describe, it, expect, beforeEach, etc)
        env.describe('faux suite', function () {
            spec = env.it('faux test', function (done) {
                env.expect(true).toBe(true)
            })
        })

        // this will fire off the specs in the secondary environment
        env.execute()

        // put your expectations here if the sub-spec is synchronous// `spec.result` has the status information we needexpect(spec.result.status).toBe(FAILED)
    })

    // don't forget the `done` argument for asynchronous specs it('should work asynchronously', function (done) {
        var spec

        // use the methods on `env` rather than the global ones.
        env.describe('faux suite', function () {
            // `it` returns a spec object that we can use later
            spec = env.it('faux test', function (done) {
                Promise.reject("FAIL").then(done)
            })
        })

        // this allows us to run code after we know the spec has finished
        env.addReporter({jasmineDone: function() {
            // put your expectations in here if the sub-spec is asynchronous// `spec.result` has the status information we needexpect(spec.result.status).toBe(FAILED)
            // this is how Jasmine knows you've completed something asynchronous// you need to add it as an argument to the main `it` call abovedone()
        }})

        // this will fire off the specs in the secondary environment
        env.execute()
    })
})

Solution 2:

Going off Joe's answer, I moved the fake test context into a single function. Since the code under test is making use of jasmine expectations, I load the inner Env into jasmine.currentEnv_ and call it explicitly with jasmine.currentEnv_.expect(). Note that currentEnv_ is an internal variable set by jasmine itself, so I can't guarantee that this won't be broken in a future jasmine version.

functioninternalTest(testFunc) {
    var outerEnvironment = jasmine.currentEnv_;
    var env = new jasmine.Env();
    jasmine.currentEnv_ = env;
    var spec;
    env.describe("fake suite", function () {
        spec = env.it("fake test", function () {
            func();
        });
    });

    env.execute();

    jasmine.currentEnv_ = outerEnvironment;

    return spec.result;
}

Then each test looks like

it("does something", function () {
    //Arrange//Actvar result = internalTest(function () {
        //Perform action
    });

    //Assertexpect(result.status).toBe("failed"); //Or "success"expect(result.failedExpectations.length).toBe(1);
    expect(result.failedExpectations[0].message).toBe("My expected error message");
});

Post a Comment for "Testing If A Jasmine Test Fails"