ember.jsember-routerember-testing

how to enumerate all route names in an ember test


we need to get a list of all route names from router.js in a test. preferably a lower level test (unit/integration).

use case for context: we have metadata that is required for all routes. this test is to make sure that the appropriate metadata was added, and not forgotten, when a new route is added.

similar questions here and here have been asked, but attempts to adopt/modify those solutions within a test have not been fruitful.


Solution

  • here's how I've managed to do it in an acceptance test.

    here's what I don't like about this solution:

    the private APIs are used in ember-inspector though, so if it breaks in the future, we should be able to reference whatever they've changed in that repo.

    import { module, test } from 'qunit';
    import { setupApplicationTest } from 'ember-qunit';
    
    module('Acceptance | route metadata test', hooks => {
      setupApplicationTest(hooks);
    
      test('we can get route names', function(assert) {
    
        const router = this.owner.lookup('router:main');
    
        router.setupRouter();
    
        const routeNames = router._routerMicrolib.recognizer.names;
        assert.ok(Object.keys(routeNames).length > 0);
    
      });
    });
    

    the test only asserts that we have route names. for the original use case, it is trivial to change this code to iterate over routeNames.

    ember-cli: 3.5.0
    node: 8.12.0
    

    the need to call setupRouter() was found via this issue