问题
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