I have a library which uses CanvasElement
and therefor is dependent on dart:html
. Now I'm trying to write unit-tests for that same library. However, I get the following error when trying to run them:
The built-in library 'dart:html' is not available on the stand-alone VM.
Here is how my test file looks like:
library PiflexUmlTest;
import 'package:PiflexUml/lib.dart';
import 'package:unittest/unittest.dart';
part 'src/geometry/vector_test.dart';
main () {
testVector();
}
I understand it's failing because library itself in lib.dart
file has a line stating:
library PiflexUml;
// ....
import 'dart:html';
part "blahblah.dart";
part "something_else.dart"
// ....
Even though library itself is dependent on it, I'm not trying to test a class which has anything to do with HTML.
What are my solutions here? Is there a way to just import classes I want to test without importing the whole lib? Or do I have to split my lib into html-dependent part and non-html-dependent part?
You could run browser based unit tests with content_shell
(headless browser).
The folder where you installed DartEditor to (darteditor/chromium/download_contentshell.sh) contains a script file to download the part containing content_shell
.
You need an HTML file that is run by content_shell
and that runs the tests. The HTML file could look like
<!doctype html>
<html>
<body>
<script src="packages/unittest/test_controller.js"></script>
<script type="application/dart" src="browser_tests.dart"></script> <!-- your unit tests -->
<script src="packages/browser/dart.js"></script> </body>
</html>
Dart unit tests
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
main() {
useHtmlConfiguration();
test('test scope', () {
...
});
}
Maybe overkill for your use case, but still a solution.
EDIT
There is also a discussion going on about this problem:
https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pacB66gnVcg
This appears to be fixed in newer versions of the test package. There is a new parameter '--platform' that accepts a browser as a value so:
pub run test --platform chrome
Will compile your tests to javascript and run them on chrome.
You can also use the @TestOn annotation, or the testOn parameter to test() or group() to specify 'vm' or 'browser' if you're writing a library that should work in both locations.
See https://pub.dartlang.org/packages/test#browservm-hybrid-tests
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With