Merge pull request #6 from blalor/no_envjs
Add ability to run Jasmine without EnvJS
This commit is contained in:
commit
c0d0644af2
|
@ -103,6 +103,7 @@ You can still use Firebug to debug when generating a specRunner HTML file (see b
|
|||
* specs: one or more spec file to run. Default behavior: use java Class name (replaces Test with Spec, see example)
|
||||
* sources: one or more JS production file which your spec needs (included before specs, d'uh)
|
||||
* generateSpecRunner: (the HTML output, useful for firefox/firebug debugging etc)
|
||||
* envJs: load EnvJS support (defaults to true)
|
||||
|
||||
## Requirements
|
||||
|
||||
|
|
|
@ -17,4 +17,9 @@ public @interface JasmineSuite {
|
|||
boolean generateSpecRunner() default false;
|
||||
|
||||
boolean debug() default false;
|
||||
|
||||
/**
|
||||
* If set to false, does not load EnvJS support.
|
||||
*/
|
||||
boolean envJs() default true;
|
||||
}
|
||||
|
|
|
@ -27,8 +27,7 @@ public class JasmineTestRunner extends Runner {
|
|||
private final Class<?> testClass;
|
||||
|
||||
@JasmineSuite
|
||||
private class DefaultSuite {
|
||||
}
|
||||
private class DefaultSuite { }
|
||||
|
||||
public JasmineTestRunner(Class<?> testClass) {
|
||||
this.testClass = testClass;
|
||||
|
@ -51,16 +50,21 @@ public class JasmineTestRunner extends Runner {
|
|||
|
||||
pre(context);
|
||||
|
||||
context.loadEnv(suiteAnnotation.jsRootDir());
|
||||
if (suiteAnnotation.envJs()) {
|
||||
context.loadEnv(suiteAnnotation.jsRootDir());
|
||||
} else {
|
||||
context.load(suiteAnnotation.jsRootDir(), "/lib/no-env.js");
|
||||
}
|
||||
|
||||
setUpJasmine(context);
|
||||
|
||||
context.load(suiteAnnotation.sourcesRootDir() + "/", suiteAnnotation.sources());
|
||||
context.load(suiteAnnotation.jsRootDir() + "/specs/", getJasmineSpecs(suiteAnnotation));
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
protected void pre(RhinoContext context) {
|
||||
}
|
||||
protected void pre(RhinoContext context) { }
|
||||
|
||||
private void setUpJasmine(RhinoContext context) {
|
||||
context.load(getJsLibDir() + "jasmine.js");
|
||||
|
@ -138,7 +142,10 @@ public class JasmineTestRunner extends Runner {
|
|||
}
|
||||
|
||||
reportSpecResultToNotifier(notifier, spec);
|
||||
resetEnvjsWindowSpace();
|
||||
|
||||
if (suiteAnnotation.envJs()) {
|
||||
resetEnvjsWindowSpace();
|
||||
}
|
||||
} finally {
|
||||
fireMethodsWithSpecifiedAnnotationIfAny(testClassInstance, After.class);
|
||||
}
|
||||
|
@ -151,7 +158,6 @@ public class JasmineTestRunner extends Runner {
|
|||
this.rhinoContext.exit();
|
||||
}
|
||||
|
||||
|
||||
private Object createTestClassInstance() {
|
||||
try {
|
||||
return testClass.newInstance();
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.mockito.runners.MockitoJUnitRunner;
|
|||
|
||||
import be.klak.junit.jasmine.JasmineTestRunner;
|
||||
import be.klak.junit.jasmine.classes.JasmineTestRunnerSuccessSpec;
|
||||
import be.klak.junit.jasmine.classes.JasmineTestRunnerDoesNotLoadEnvJS;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class JasmineFinishedSpecsTest {
|
||||
|
@ -38,4 +39,21 @@ public class JasmineFinishedSpecsTest {
|
|||
assertThat(startedDescription.getDisplayName()).isEqualTo("will always run");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotLoadEnvJsWhenSoConfigured() {
|
||||
new JasmineTestRunner(JasmineTestRunnerDoesNotLoadEnvJS.class).run(notifierMock);
|
||||
|
||||
ArgumentCaptor<Description> descriptionStartedCaptor = ArgumentCaptor.forClass(Description.class);
|
||||
ArgumentCaptor<Description> descriptionFinishedCaptor = ArgumentCaptor.forClass(Description.class);
|
||||
verify(notifierMock).fireTestStarted(descriptionStartedCaptor.capture());
|
||||
verify(notifierMock).fireTestFinished(descriptionFinishedCaptor.capture());
|
||||
verifyNoMoreInteractions(notifierMock);
|
||||
|
||||
Description startedDescription = descriptionStartedCaptor.getValue();
|
||||
Description finishedDescription = descriptionFinishedCaptor.getValue();
|
||||
|
||||
assertThat(startedDescription).isSameAs(finishedDescription);
|
||||
assertThat(startedDescription.getDisplayName()).isEqualTo("is not loaded");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
package be.klak.junit.jasmine.classes;
|
||||
|
||||
import be.klak.junit.jasmine.JasmineSuite;
|
||||
|
||||
@JasmineSuite(specs = { "doesNotLoadEnvJSSpec.js" }, envJs = false)
|
||||
public class JasmineTestRunnerDoesNotLoadEnvJS { }
|
|
@ -12,7 +12,7 @@ import be.klak.rhino.RhinoContext;
|
|||
public class RhinoContextEnvjsLoadingTest {
|
||||
|
||||
@Test
|
||||
public void loadEnvShouldSetWindowSpaceAndBeES5Complaint() {
|
||||
public void loadEnvJSShouldSetWindowSpaceAndBeES5Complaint() {
|
||||
RhinoContext context = new RhinoContext();
|
||||
|
||||
context.loadEnv("src/test/javascript");
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
// shim to allow jasmine to run in Rhino without EnvJS loaded
|
||||
// inspired by
|
||||
// https://groups.google.com/d/msg/jasmine-js/waor9RXUsDw/LY6gNLcMxkcJ
|
||||
// implementation stolen from env.utils.js
|
||||
|
||||
(function(global) {
|
||||
var threadTimeoutPool = new java.util.HashMap();
|
||||
|
||||
global.setTimeout = function(closure, timeout) {
|
||||
var thread = spawn(function() {
|
||||
try {
|
||||
java.lang.Thread.sleep(timeout);
|
||||
closure();
|
||||
} catch(e) {
|
||||
// ignore InterruptedExceptions, is probably due to clearTimeout
|
||||
if (!(e.javaException instanceof java.lang.InterruptedException)) {
|
||||
throw(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
threadTimeoutPool.put(thread.getId(), thread);
|
||||
return thread.getId();
|
||||
};
|
||||
|
||||
global.setInterval = function(closure, timeout) {
|
||||
var thread = spawn(function() {
|
||||
try {
|
||||
while(true) {
|
||||
java.lang.Thread.sleep(timeout);
|
||||
closure();
|
||||
}
|
||||
} catch(e) {
|
||||
// ignore InterruptedExceptions, is probably due to clearTimeout
|
||||
if (!(e.javaException instanceof java.lang.InterruptedException)) {
|
||||
throw(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
threadTimeoutPool.put(thread.getId(), thread);
|
||||
return thread.getId();
|
||||
};
|
||||
|
||||
global.clearTimeout = function(threadId) {
|
||||
if (threadId) {
|
||||
if(threadTimeoutPool.containsKey(threadId)) {
|
||||
threadTimeoutPool.remove(threadId).interrupt();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
global.clearInterval = global.clearTimeout;
|
||||
})(this);
|
0
src/test/javascript/runners/JasmineSuiteGeneratorClassWithRunnerRunner.html
Executable file → Normal file
0
src/test/javascript/runners/JasmineSuiteGeneratorClassWithRunnerRunner.html
Executable file → Normal file
|
@ -0,0 +1,5 @@
|
|||
describe("EnvJS", function() {
|
||||
it("is not loaded", function() {
|
||||
expect(typeof(window)).toBe('undefined');
|
||||
});
|
||||
});
|
Reference in New Issue