intercom / ember-href-to

:link: A lightweight alternative to {{link-to}}
Apache License 2.0
190 stars 42 forks source link

TypeError: Cannot read property 'generate' of undefined #94

Open brianshano opened 6 years ago

brianshano commented 6 years ago

Ember 2.9.0

Running ember test with ember-href-to v1.14.0 is fine. Using v1.15.0 we get some failing integration tests:

        actual: >
            null
        stack: >
            TypeError: Cannot read property 'generate' of undefined
                at Class.generate (http://localhost:7357/assets/vendor.js:49196:42)
                at Class.urlFor (http://localhost:7357/assets/vendor.js:76358:48)
                at hrefTo (http://localhost:7357/assets/vendor.js:144238:26)
                at Class.compute (http://localhost:7357/assets/vendor.js:144251:23)
                at Child.compute (http://localhost:7357/assets/vendor.js:34567:26)
                at Child.value (http://localhost:7357/assets/vendor.js:34897:27)
                at read (http://localhost:7357/assets/vendor.js:35290:21)
                at Object.readArray (http://localhost:7357/assets/vendor.js:35312:16)
                at Child.compute (http://localhost:7357/assets/vendor.js:34434:48)
                at Child.value (http://localhost:7357/assets/vendor.js:34897:27)
        message: >
            Died on test #1     at Module.callback (http://localhost:7357/assets/tests.js:27187:24)
                at Module.exports (http://localhost:7357/assets/vendor.js:115:32)
                at requireModule (http://localhost:7357/assets/vendor.js:36:18)
                at TestLoader.<anonymous> (http://localhost:7357/assets/test-support.js:7700:11)
                at TestLoader.require (http://localhost:7357/assets/test-support.js:7690:27)
                at TestLoader.loadModules (http://localhost:7357/assets/test-support.js:7682:16)
                at Function.load (http://localhost:7357/assets/test-support.js:7633:26): Cannot read property 'generate' of undefined
        Log: |

The test renders a component that contains a href-to call:

I haven't manage to recreate it in tests on ember-href-to yet

mazondo commented 6 years ago

This is happening to us as well, dropping to 1.14 resolved it

GavinJoyce commented 6 years ago

@mazondo what version of ember are you using?

nadnoslen commented 6 years ago

I just bumped into this in my unit tests (works fine in the app).

Essentially I have a component rendering a handful of links using href-to.

Ember-2.9.1 ember-href-to-1.15.1

Just noticed that @mazondo downgraded ember-href-to-1.14; I'll try that.

nadnoslen commented 6 years ago

So downgrading to ember-href-to-1.14.0 worked for me too. Thanks @mazondo

cmgurba commented 6 years ago

Errors exist in Ember-2.16.2 as well. Confirmed downgrading to ember-href-to-1.14.0 does get rid of the errors here as well.

GCheung55 commented 6 years ago

I was seeing this on 1.15.x with ember@2.17.1. Looking into this some it looks like the router isn't setup yet, that's why this._routerMicrolib is undefined.

this._routerMicrolib is setup in the _initRouterJs method. My guess is that the router isn't setup in component tests.

Reference: https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/system/router.js#L396

GCheung55 commented 6 years ago

Looking around some more, it looks like the integration test is setting up the router with:

beforeEach() {
  getOwner(this).lookup('router:main').setupRouter();
}

@GavinJoyce Is there a way to automatically setup the router without needing to manually add a beforeEach to every integration test?

panthony commented 6 years ago

@GCheung55 Adding this did fix this particular issue but it broke lot's of other tests.

Looks like this call has side-effects that impact other tests.

GCheung55 commented 6 years ago

@panthony I see. My project has a couple of components that use href-to so setting up the router in a beforeEach for their respective tests addressed the issue but I didn’t see side effects to other tests.

We’re the side-effect errors you saw consistent?

panthony commented 6 years ago

@GCheung55 Typically I have:

In integration tests:

Source: | Error: Must setup rendering context before attempting to interact with elements.

In route tests:

Error: Assertion Failed: Attempting to inject an unknown injection: 'service:i18n'

If I add service:i18n in needs array it will fails eventually with:

Error: Property set failed: object in path "session" could not be found or was destroyed.

Maybe this is due to ember-simple-auth somehow.

I did not try to fix them really. I do not wish to add something in a test that break another one, setupRouter do something that lives beyond the test.

hoIIer commented 6 years ago

+1 just ran into this when using <a href={{href-to foo}} in integration test, going to downgrade for now

chbonser commented 6 years ago

Just in case this data point is helpful... I'm using Ember 3.3.1 and both 1.14.0 and 1.15.1 cause the error.

hoIIer commented 5 years ago

seems like _router is undefined in urlFor which leads to this?