mercredi 15 juillet 2020

Testing without the "Bloatware"

I've been trying to write some tests for an existing AngularDart application and I have to say it is an awful experience so far: documentation is practically non-existing (still as draft with big chunks missing) and I feel kind of left hanging.

I want to simply unit-test the functions I have in a component. No need for the whole test-bed initialization or anything of the sorts. Why? Because is faster and I don't want to test the presentation layer, just my logic...

Anyway here is a trimmed down example:

app_component.dart

    import 'package:angular/angular.dart';

    @Component(
      selector: 'my-app',
      styleUrls: ['app_component.css'],
      templateUrl: 'app_component.html',
    )
    class AppComponent extends OnInit {
      bool isValid = false;
    
      @override
      void ngOnInit() {
        isValid = true;
      }
    }

app_component.html

    <h1>My First AngularDart App</h1>

app_component.css

    :host {
        /* This is equivalent of the 'body' selector of a page. */
    }

app_test

    import 'package:my_app/app_component.dart';
    import 'package:test/test.dart';
    
    void main() {
      var component = AppComponent();
    
      test('initial value should be false', () {
        expect(component.isValid, false);
      });
    
      test('value after init should be true', () {
        component.ngOnInit();
    
        expect(component.isValid, true);
      });
    }

As you can see there is absolutely nothing fancy here. If I run the tests like this:

    pub run build_runner test -- -p chrome

then everything works like a charm. However if I try to run like:

    pub run build_runner test (or pub run test)

I get a bunch of errors due to, what I am guessing, some web dependencies:

    Unable to spawn isolate: test/app_test.dart:3:8: Error: Error when reading 
    
    'lib/app_component.template.dart': No such file or directory
    
    import 'package:chess_dart/app_component.template.dart' as ng;
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular_test-2.3.0/lib/src/frontend/bed.dart:6:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular_test-2.3.0/lib/src/frontend/fixture.dart:6:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/core/application_ref.dart:2:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/core/testability/testability.dart:2:8: Error: Not found: 'dart:html'
    import 'dart:html' show Element;
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular_test-2.3.0/lib/src/bootstrap.dart:6:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/core/linker/app_view_utils.dart:1:8: Error: Not found: 'dart:html'
    import 'dart:html' show DocumentFragment, NodeTreeSanitizer;
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/runtime/dom_events.dart:1:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/bootstrap/modules.dart:6:8: Error: Not found: 'dart:html'
    import 'dart:html';
           ^
    ../../../.pub-cache/hosted/pub.dartlang.org/angular-5.3.1/lib/src/common/directives/ng_class.dart:1:8: Error: Not found: 'dart:html'
    import 'dart:html';
             ^
    00:01 +0 -1: Some tests failed.

Can someone shed some light into this? I want to run my tests on the CI and if possible I want to avoid the hassle of having to install a browser on docker just to run the tests.

Thanks

Aucun commentaire:

Enregistrer un commentaire