jasmine library “no specs found” when running same test twice

泄露秘密 提交于 2019-12-10 20:22:11

问题


I want to be able to run the same test twice from a node express server, but noticed the second run of the same test always gives "no specs found".

Here is an example : jasmine-test.js :

function jasmineExecute(fileName) {
    var jasmine = new Jasmine({});
    jasmine.onComplete(function(x) {
        if (x) {
            jasmineExecute("./test.js"); // risk of infinite loop
        }
        else {
            console.log('Test failed : ' + fileName);
        }
    });
    jasmine.execute([
        fileName
    ]);
}
jasmineExecute("./test.js");

test.js :

describe("We test that ", function() {
    it("The return should be true", function() {
        expect(true).toBe(true);
    });
});

The result I have is as follow :

Randomized with seed 03122
Started
.


1 spec, 0 failures
Finished in 0.006 seconds
Randomized with seed 03122 (jasmine --random=true --seed=03122)
Randomized with seed 51883
Started


No specs found
Finished in 0.005 seconds
Incomplete: No specs found
Randomized with seed 51883 (jasmine --random=true --seed=51883)
Test failed : ./test.js

I am running on Jasmine 5.6.0 and node 8.9.4. Any help on this will be welcome.


回答1:


This is actually a known (though hard-to-find) issue with the jasmine package:

https://github.com/jasmine/jasmine-npm/issues/30

The gist is that Jasmine uses require() to load your spec files, and because Node.JS will only load a module once per "session", your spec files are not re-executed, so describe()/it() are not called.

If you have only a single spec file, you can fix this by removing the module from the cache using the decache package:

const Jasmine = require("jasmine")
const decache = require("decache")

function jasmineExecute(fileName) {
    var jasmine = new Jasmine({});
    jasmine.onComplete(function(x) {
        if (x) {
            decache("./test.js")
            jasmineExecute("./test.js"); // risk of infinite loop
        }
        else {
            console.log('Test failed : ' + fileName);
        }
    });
    jasmine.execute([
        fileName
    ]);
}
jasmineExecute("./test.js");


来源:https://stackoverflow.com/questions/49995731/jasmine-library-no-specs-found-when-running-same-test-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!