lundi 28 janvier 2019

How to run jasmine tests from js script

Im making a javascript code playground, and want to test customers code using jasmine, that runs in vm.runInThisContext (or simply - eval).

I take user code + test code(it should be in jasmine)

const codeToEvaluate = localStorage.getItem("userCode") + this.props.testCode["testCode"]

And if user run his code, the code should pass the tests and log on my custom console error message if it will be

if (vm.runInThisContext(codeToEvaluate) === true) {
    this.log("Oh wow, you're not entirely hopeless after all. Good job.");
    this.unlockQuest();
}

Example

user code:

const userCode = "
const rectangle = {
    color: 'red',
    width:4,
    height:4,function isBlack(rectangle) {
    return rectangle.color == 'black';
}
}; 
function isBlack(rectangle) {
    return rectangle.color == 'black';
}"

test code:

const test = "
describe("quest1", function() { 

   it("should return true if it is black",function() { 
      expect(isBlack(rectangle)).toEqual(false); 
   }); 

});
"

Aucun commentaire:

Enregistrer un commentaire