I am unit testing one of my components in an Aurelia project. I’d like to access my component’s viewModel in my unit test but haven’t had any luck so far.
1) my aurelia tests
can manually handle lifecycle:
TypeError: Cannot read property 'name' of undefined
Here is the the line that defines the viewModel on the component object but only if aurelia.root.controllers.length is set. I am not sure how to set controllers in my aurelia code or if I need to do so at all.
I guess my question is: How do I get access to a component’s viewModel in my unit tests?
Make sure you are doing that correctly, or you component is at the specified resource path. If you are using Webpack, you need to setup the testing environment correctly. If you are using Jasmine+Karma+Webpack, you may check this out: Aurelia-Testing with Jasmine/Karma Fails for more info on the setup. Basically, if you are using Webpack, you need to make sure the your component under test included in the bundle (easiest would be to put the component root path to module, in Webpack config).
Once the setup is done correctly, and the specified resource is available under the said path, the component.viewModel should be available after the create(bootstrap), irrespective of whether it is manuallyHandleLifecycle or not.
Also, for those interested, here is a minimalistic setup with karma+mocha+typescript+webpack (with sourcemaps and VS Code debugging) and a basic test verifying the rendered html: https://github.com/fkleuver/aurelia-karma-webpack-testing