Testing in the absence of a jasmine test

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

Of course, I want to write tests to make sure my plugin works correctly, and to be thorough, I need to make sure the tests fail when the promise is rejected ... since I can make a test pass when I need to make sure does the test succeed?

+4
source share
2 answers

After talking with the developers who work on Jasmine, we came to the following:

var FAILED = 'failed'
var PASSED = '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 need
        expect(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 need
            expect(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 above
            done()
        }})

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

Going away from Joe's answer, I moved the fake test context to one function. Since the code under test uses jasmine expectations, I load the internal one Envin jasmine.currentEnv_and call it explicitly with jasmine.currentEnv_.expect(). Please note that this currentEnv_is an internal variable set by jasmine itself, so I can not guarantee that this will not be violated in a future version of jasmine.

function internalTest(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

    //Act
    var result = internalTest(function () {
        //Perform action
    });

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

Source: https://habr.com/ru/post/1607709/


All Articles