Pacane
Pacane

Reputation: 21471

Unit testing code that depends on dart:html

I'm trying to write unit tests on a piece of code that imports dart:html and I ended up having a test class that's using useHtmlConfiguration();

Do I really have to do this? Because it seems everytime I run my tests, it runs in a browser, dart2js gets called and it's taking much longer than if I were testing with the dartVM. I tried it with Dartium and it also recompiles.

In fact, the only reason my code uses dart:html is because it's using HttpRequest in the package. At the end I might just end up putting an interface in front of the class doing the http request and mocking it, but I was wondering if there were an efficient way of having a good (read quick) feedback loop without having to call dart2js everytime I want to run my tests?

Upvotes: 1

Views: 110

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657268

If your code imports dart:html the code and also tests importing this code can only be run in the browser.

I don't know why dart2js is called. You can run tests in Dartium or content_shell --dump-render-tree (headless Dartium) as Dart code without transpiling to JS first.

You might prefer using the http package which has some abstraction of HttpRequest which should on client and server (haven't tested it myself this way yet).

Upvotes: 1

Related Questions