How to access component's view model in unit tests

In my experience, the view-model of the component depends on how you are staging your component and view.

StageComponent.withResources("path/to/MyComp").inView("<my-comp></my-comp>")

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.

Hope this helps.

1 Like