Search code examples
unit-testingjasmineaurelia

Aurelia unit test route paths to modules


I've been looking for a good pattern to unit test routes I have configured in my application so that I know the specified modules exist on disk as defined.

Here is an example route configuration:

import { Aurelia, PLATFORM } from 'aurelia-framework';
import { Router, RouterConfiguration } from 'aurelia-router';

export class App {
    params = new bindParameters();
    router: Router;


    configureRouter(config: RouterConfiguration, router: Router) {
        config.title = 'Aurelia';
        config.map([{
            route: ['', 'home'],
            name: 'home',
            settings: { icon: 'home' },
            moduleId: PLATFORM.moduleName('../home/home'),
            nav: true,
            title: 'Home'
        }, {
            route: 'sample',
            name: 'sample',
            settings: { icon: 'education' },
            moduleId: PLATFORM.moduleName('../sample/index'),
            nav: true,
            title: 'Sample Information'
        }]);

        this.router = router;
    }
}

class bindParameters {
    user = "user_name";
}

To test it I took the approach of passing in an instance of a router then checking to see if it exists:

import { App } from './app';
import jasmine from 'jasmine';
import { Container } from "aurelia-framework";
import { RouterConfiguration, Router } from "aurelia-router";

describe('application routes', function () {
    let app: App;
    let router: Router;
    let routerConfiguration: RouterConfiguration;
    let configureRouter: Promise<void>;

    beforeEach(() => {
        var container = new Container().makeGlobal();
        routerConfiguration = container.get(RouterConfiguration);
        router = container.get(Router);
        app = new App();
        app.configureRouter(routerConfiguration, router);
        configureRouter = router.configure(routerConfiguration);
        routerConfiguration.exportToRouter(router);
    });

    it('should exist for sample', function () {
        expect(router).not.toBeNull();
        //configureRouter.then(function () {
        //var route = router.routes.find((route) => route.name == 'sample');
        // add some assert that the sample module can be found
        //    done();
        //});
    });
});

My current problem is that the container is returning a null Router as shown by the current test. The closest pattern I have found to what I am trying to do is in this question.

What am I missing in my example test and also is there a better way to test route configuration?


Solution

  • It seems @thinkOfaNumber was right. Turns out my test was good, but I was missing reflect-metadata. When I applied the fix outlined in this stackoverflow post my tests passed.