How can I run unittests in Dart Editor? - unit-testing

I have the web application project in Dart Editor and I want to write some unittests for it. I tried to create .dart file with a simple unit test in the same project (with main function etc) but in context menu I had only 'run as javascript' and 'run in dartium' options (when I click any of them nothing happens). How can I run unittest as console app from Dart Editor? What is the most convenient way of doing such tests?

I had exactly the same problem. I wanted to unit test the behaviour of my widgets (from the dart editor v1.3.3) having mocked out the UI elements, but found that, because I was importing dart:html, I was forced to run the tests as if they were a web app with one of the 'Run in browser' menu options.
It seems that as soon as you import dart:html or a package that does so you're stuck with the 'Run in browser' options for your unit tests (creating another package to test the original one doesn't solve the problem either, for the same reason, it has to import the dart:html one). And yes, attempting to run regular unit tests with the 'Run in browser' options just fails silently and does absolutely nothing.
The simple solution is to use an html file to run your tests. You can adapt your unit tests to output to the webpage if you like but it's not essential.
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unit Test Results</title>
</head>
<body>
<script type="application/dart" src="test.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
test.dart
// Used to illustrate the problem. Importing dart:html causes
// the context menu to show only the 'Run in browser' options.
import 'dart:html';
import 'package:unittest/unittest.dart';
// Required if you want to use useHtmlConfiguration().
import 'package:unittest/html_config.dart';
void main() {
// Required if you want to output the unit test results
// to the HTML page rather than the console.
useHtmlConfiguration();
test ('simple test', () => expect(true, isTrue, reason:"true isn't true"));
}

I actually had the exact same problem as described in the question. After some searching I discovered my directory structure was incorrect.
my directory structure was as follows:
/projectname
/web
index.html
main.dart
/test
test_file.dart
This is wrong. I changed my directory structure to the following:
/projectname
/web
index.html
main.dart
/test
test_file.dart
As you can see, i moved the /test directory out of the /web directory. As soon as I did this, I was now able to right click the test_file.dart file and select 'run' in the context menu (not 'run as javascript' or 'run in dartium') and see the tests being run in the built-in console in the dart editor.
For more information: the full pub package layout conventions can be found here: https://www.dartlang.org/tools/pub/package-layout.html
information about unit tests in dart can be found here: https://www.dartlang.org/articles/dart-unit-tests/#configuring-the-test-environment (including configuring the test environment for stuff like html output etc...)

You say you have a web application project so you want test code that runs in the browser?
I have never experienced that I have run as javascript or run in Dartium for console applications. Did you import 'dart:html' in this dart file?
Anyways maybe this answers your question:
You can run unittests testing web code with content_shell.
The Dart installation directory contains a script to download content_shell (it is not part of the Dart installation package to keep it small).
You actually run an HTML page:
content_shell --dump-render-tree polymer_ajax.html
I haven't tried but when you build your test code to JavaScript using
pub build test
you should be able to run the result using content_shell.
DartEditor doesn't show a run option for a bash script (just tried) so you would need to build a Dart script that invodes content_shell.
The Dart team has mentioned several times recently that they want to improve the test story but the infrastructure for run/build is still work in progress. When this has settled I guess the test support will be worked on next.

Ok, I will describe solution that works best for me.
I created separate project for tests in Dart Editor (polymer web application - the same as target project). Main .html file in test project imports .dart file that looks like:
library foo_test;
import '../../foo_project/web/foo.dart'; //test target
import '../packages/mock/mock.dart';
import '../packages/unittest/unittest.dart';
import '../packages/unittest/html_config.dart';
void main() {
useHtmlConfiguration();
test("sample test", () {
expect(foo_function(), equals(expected_result) );
});
}
useHtmlConfiguration() creates some nice page with all tests listed. I also tried useHtmlEnhancedConfiguration() as described in https://www.dartlang.org/articles/dart-unit-tests/ but it doesn't print stack traces (neither in page nor in console) so it is rather useless for me. There is also useHtmlInteractiveConfiguration() in documentation but it doesn't exist in unittest package.

Related

Unit testing with Webpack, Jasmine (-core), typescript

I have a project that is using webpack to bundle all code into a single file. The project is using Typescript and it is working fine at the moment.
I've gone to add unit testing and jasmine seems to be the way (one of the many ways) forward. Its actually jasmine-core that is included in the package.json - not sure how much of a difference that makes.
So running a very simple test such as
it('true is true', function(){ expect(true).toEqual(true); });
works fine.
But when I add tests that require the use of an import - eg
import MyService = require('./MyServices');
then when I run the tests it complains as it doesn't know what 'require' is.
Uncaught ReferenceError: require is not defined
Now I'm guessing this is because I need to package up the test module in a similar way that I package up the main project.
So what is the best way to do this?
Should I have multiple entry points in the webpack.config.js file - one for each *.spec.ts file?
Or is there a way to have say accept an unknown number of spec files
entry:[ *.spec.ts ] and have it output a js file for each one - *.spec.js
You can use karma/karma-webpack to run all the tests using webpack for resolving the imports. You can take a look at this repository for a simple configuration.
You can also specify an index.spec.ts as en entry point and make this file require all the spec files if you don't want to make one entry point for each spec.ts in your webpack's configuration file.

WebStorm run all dart unit tests

In WebStorm 11 I want to create a run configuration which runs all dart tests in my project.
However there is no option to do this in the "Dart Test" configuration template. The only options are:
Test Kind: All in file, Test group, single test
Test file: must point to a .dart file, otherwise I get "Dart file is not found"
VM Options (text input)
If I point WebStorm to a single test file this command gets executed in the test window:
C:\path\to\dart\bin\dart.exe --ignore-unrecognized-flags --checked --trace_service_pause_events file:\\\C:\path\to\dart\bin\snapshots\pub.dart.snapshot run test:test -r json C:/path/to/project/test/someclass_test.dart
I don't want to create a run configuration for every unit test class I write, there must be a better way.
Currently I prefer to navigate to the project directory and just run
pub run test:test
This runs all tests which live in files ending with _test.dart which is perfectly what I want. More info here: https://github.com/dart-lang/test#running-tests
Is there no such option in WebStorm for dart developers?
Accordingly to WEB-14747 ticket this functionality is already implemented for the next major version.
You can try latest EAP build of WebStorm v12 here.
I guess that's currently not supported.
The feature to run tests this way is quite new anyway.
If you think this feature is important, lease create a feature request in https://youtrack.jetbrains.com/issues/WEB

Import ember addon code directly from out of the box tests

I'm working on creating an ember addon, and I'm a bit stuck trying to write tests for it. This addon implements a command line option, rather than shipping components etc. As a result, none of the moduleFor type test helpers are relevant for me in the out of the box qunit tests. I'm not rendering any components, I just want a test runner to exersize the implementation behind my command line option.
To write my tests, I'll need to just require my various source files that are up in my addon. For example, files sitting in root/lib. I can't get a require/import that can find these files in a qunit integration test under root/tests/integration. Is this possible? I need a relative path like:
import foo from '../../../lib/foo'
But nothing up there seems to work. The folder structure created for an addon is like:
root
app
lib (was planning on putting my addon impl here)
tests
dummy
helpers
integration
example-test.js (trying to reference code out of the lib folder from here)
It seems like my options in this case are just to fall back to some plain old JS unit testing (qunit, jasmine etc), based up in the root of the addon, not using any ember magic or the dummy app. I would like to stay on the 'out of the box' path provided by ember generate addon, but it seems like I need to go my own way here, so I can reference my source files.
Use
import foo from 'myApp/lib/foo'

Dart unit testing classes in a html-dependant library

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

CoffeeScript unit testing?

I'm using CoffeeScript in a Rails application, and I would like to unit test it. Google didn't turn up anything, is there any way to do it short of writing my own testing framework or testing the JavaScript that CoffeeScript outputs?
You can use any javascript testing framework with CoffeeScript. This will be testing the Javascript that CoffeeScript outputs which is necessary since CoffeeScript itself can't be executed.
Writing your own testing framework for CoffeeScript is fun (I did) but entirely uneccessary.
UPDATE: Jasmine tests can be run on node.js in which case both the tests and the code under test can be CoffeeScript, without the need for any compilation step.
You can use QUnit "as-is", but still only write coffee-script - and no glue-code.
I have a very small, pure coffee-script project on github as an example - rubyann.
The HTML test page rubyann_tests.html, references the rubyann_tests.coffee file which tests jquery.rubyann.coffee. I didn't write any javascript or any other code to make this work.
The tests only run on Chrome on your local machine if you use the command-line argument --allow-file-access-from-files. But it works on Firefox and even IE without issues.
addendum - the tests are also setup to run on the command line via Node/gulp/qunitjs - download the repo and type npm run test
I'm testing CoffeeScript in my Rails app with QUnit, and have written up how I'm doing it here: http://effectif.com/coffeescript/qunit-boilerplate
The most interesting thing in my write-up is the use of the callback to Coffee.load to guarantee that files containing tests get loaded after the files that contain the code under test:
<script type="text/coffeescript">
for file in ['models', 'controllers']
lib = "../../app/assets/javascripts/#{file}.js.coffee"
load_test = ->
test = "#{file}_test.coffee"
-> CoffeeScript.load(test)
CoffeeScript.load lib, load_test()
</script>
The need for currying the test variable is explained in the article...
For Coffee-Script Unit testing you can try Beast-Test it was written from the ground up for coffee-script. FYI i am the own but i think you will like it none the less. It is similar to JUnit