Get to dependency injection container from cypress test

Is there a way to get to the Aurelia dependency injection container from a cypress test? I would like to be able to stub out methods on objects in the container. I looked and I could not find anything currently in v1 or 2 though I feel like I am not the first one to think of this, I can’t find anything on it. I don’t have any child containers currently. I just need to the root container.

Thanks in advance.

Cypress is usually used to perform e2e tests. Which means that you typically don’t want to replace or mock modules in your app.

Sometimes, the need to mock some of the modules generated because you want to mock some network requests, so that the test data does not mess up your production environment. For example, you don’t want your test data to end up in your production database.

If you also have similar requirement, then I would suggest to use a proxy to capture and modify the network requests that you want to mock. As I am not using cypress myself, I can only provide this resource which shows the example of proxying network request: https://blog.bitsrc.io/mocking-http-calls-in-cypress-end-to-end-tests-fa2e6b7caaf7

Additional resource: https://playwright.dev/docs/network (with playwright, but elaborates the concept bit more directly IMO).

In case your mocking need is different than what I have described here, you might be better served with unit tests instead of e2e tests.

1 Like

Thanks Sayan.

Sorry for taking so long to get back to you, I was on vacation and didn’t get to my emails till today. I will take a look at these links.

1 Like