You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an unusual use case that I have struggled with for over a year, I have run Jasmine standalone through babel to support the Rhino.js engine (somewhere between ES5 and ES6 support). This is now parsing just fine and I have access to Jasmine, I can write a custom boot file!
How do I load up a test from string and have it run?
or
How do I create an entire test suite pragmatically
b. do I need to write a parser myself?
constjasmineRequire=require("./build/jasmine");this.global=this;constjasmine=jasmineRequire.core(jasmineRequire);// Object.assign(this.global, jasmineRequire.patch());this.global.console=jasmineRequire.patch().console;this.require=jasmineRequire.patch().require;// !!Patched!! custom fs and path modules powered by javaconstfs=require('fs');constpath=require('path');// TODO: COPY boot0Object.assign(this.global,jasmine.getGlobal());global.jasmine=jasmine;// it, describe and moreconstenv=jasmine.getEnv();constjasmineConfigFile=fs.readFileSync(path.resolve('.','spec','support','jasmine.json'));constjasmineConfigJSON=JSON.parse(jasmineConfigFile);env.configure(jasmineConfigJSON);varjasmineInterface=jasmineRequire.interface(jasmine,env);// custom more like jestjasmineInterface.test=jasmineInterface.it;// const Suite = jasmineInterface.jasmine.Suite;// A testconstx=jasmineInterface.it('does a thing',function(){jasmineInterface.expect(true).toBe(false);});// ADD TESTS HERE?env.topSuite().suite_.children.push(Object.assign({execute: env.execute.bind(null,false,function(args){console.log(args)}),},x));env.execute(['spec/a.test.js'],function(){console.log("");console.log("All tests complete:")}).then(function(doneInfo){console.log(doneInfo);});
The text was updated successfully, but these errors were encountered:
The only supported way to create specs and suites is by calling the describe and it functions. If you've got specs in a JavaScript file and you want to run them, the way to do that is to load and execute the file after you've created the Env but before you've executed the Env. How you do that depends on the JS runtime you're using. In the browser you'd add a script tag. In Node you'd call require. I don't know what the Rhino equivalent is.
In other words:
Create and boot an Env.
Copy the Jasmine globals (descrbe, it, expect, etc) to the global object so they can be referenced from spec files.
Tell Rhino to load and execute the spec files.
env.execute().
If Rhino doesn't have an equivalent of require, you could use your build process to insert the contents of the spec files before env.execute().
A few other things to be aware of:
env.execute() takes an array of spec/suite IDs, not filenames. (For the most part, jasmine-core doesn't have any concept of files.) In most cases you should pass null or undefined as the first parameter to execute().
env.execute() isn't reentrant. You can't call it again e.g. from inside suites.
Jasmine isn't designed to allow for suites/specs to be mutated via topSuite(). Doing that will corrupt Jasmine's internal state, in ways that could be sublte or catastrophic. Creating your own suite/spec objects and adding them via topSuite() will definitely not work. In general, you're better off sticking to the documented public API.
I did get this working but realized that it was better to just use node.js and run each test in its own Rhino process then extract the result by manipulating the code that I ran in the Rhino environment, I wrote a few test utils to introspect the code and return an object containing the value of variables.
Thank you so much for your pointers and I hope this helps future searches!
I have an unusual use case that I have struggled with for over a year, I have run Jasmine standalone through babel to support the Rhino.js engine (somewhere between ES5 and ES6 support). This is now parsing just fine and I have access to Jasmine, I can write a custom boot file!
How do I load up a test from string and have it run?
or
How do I create an entire test suite pragmatically
b. do I need to write a parser myself?
The text was updated successfully, but these errors were encountered: