joescii / sbt-js-test

sbt plugin for running JavaScript tests on the JVM with browser APIs
Apache License 2.0
13 stars 9 forks source link

Support asynchronously-loaded JS assets from RequireJS #6

Closed Jhara closed 8 years ago

Jhara commented 8 years ago

I have a project with Play Framework + AngularJs + RequireJs, This plugin works with them?, I need write a few AngularJs test unit. Thanks

joescii commented 8 years ago

Sorry for my slow reply. I was out of town this weekend. It absolutely should work. I used it at my previous employer where we were using CoffeeScript and Angular. The design of this plugin is to work with minimal limitations on the JS libraries and tooling you use.

jhonrope commented 8 years ago

@Jhara and I are working in the same project. When we try to run the tests, loaded asynchronously with requireJs, the test report show "success" even when we write failing test cases. We try to force the re-running of the tests after all spec.js files are loaded with requireJs callback. This time every test is executed but there is no way SBTknows about this execution.

Can you give us any lead about the problem?

Thank you

joescii commented 8 years ago

I do t have any experience with using require to asynchronously load JavaScript. The problem is this plugin has no way to know it should wait for that to load before proceeding. Does require give any mechanism to either disable async load when testing, or to get a callback notification that all assets are loaded?

Joe

Sent from my iPhone

On Jun 10, 2016, at 06:22, jhonrope notifications@github.com wrote:

@Jhara and I are working in the same project. When we try to run the tests, loaded asynchronously with requireJs, the test report show "success" even when we write failing test cases. We try to force re-running the tests after every spec.js file is loaded with requireJs callback. This time every test is executed but there is no way and knows about this execution.

Can you give us any lead about the problem?

Thank you

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

jhonrope commented 8 years ago

Hi Joe, requireJs has a config property called "deps", where you specify all scripts you need to load before call another config function called "callback". I think there is no way no tell requireJs to block until all deps are loaded.

In callback was where we put the jasmine.getEnv().execute(). We think that there we could pass a property to the plugin that notifies all dependencies has been loaded.

joescii commented 8 years ago

Ok, this sounds like something we could reasonably support in the plugin.

First create a small sample project among the test-projects which fails as an integration test using require. You can run these with it:test or it:testOnly <classname>

The plugin already calls jasmine.getEnv().execute();. You can see I already do some JS hackery to send values from the plugin to the JS here and here. What you need is a boolean to the effect of window.sbtJsTest.readyForTestsToRun which is set true by your JS. Of course, this needs to be configurable via an sbt SettingKey since not everyone needs to do this. Then before calling jasmine, you can busy wait until it's ready to proceed.

joescii commented 8 years ago

Reopening issue as a feature request.

VeryBueno commented 8 years ago

I also have a similar use case. I'm using Angular + TypeScript + SystemJS and your sbt-js-test to run unit tests. The hack-ish way that I defer the jasmine.getEnv().execute() is by assigning it to something else. Please correct me if this seems very wrong but it works for me:

// SystemConfig.js
var executeJasmine = jasmine.getEnv().execute;
jasmine.getEnv().execute = function () {};

// AllTests imports every other test.
System.import("test/AllTests")
    .then(function () {
            console.log("System loaded. Begin running tests.");
            executeJasmine();
        }
    )
    .catch(function(err){
        console.error(err);
        executeJasmine();
    });

In my build.sbt, I only load this file and the required System libraries for jsResources. I do not use jsTestResources.

The biggest downside to this is that you'll have to import (or require) all your tests in a single file so that the loader can fetch them for you before executing jasmine.

joescii commented 8 years ago

@VeryBueno Can you try this PR to see if it suits your needs?

joescii commented 8 years ago

Closed via PR #7