germsvel / phoenix_test

PhoenixTest provides a unified way of writing feature tests -- regardless of whether you're testing LiveView pages or static (non-LiveView) pages.
https://hex.pm/packages/phoenix_test
MIT License
181 stars 23 forks source link

Allow configuration of different Endpoint modules within a single project #61

Closed sesamzoo closed 7 months ago

sesamzoo commented 7 months ago

Hello @germsvel,

First of all: thank you very much for this great library.

Now my issue: How could one configure different Endpoint modules?

The global configuration does not allow for testing different endpoint modules:

In config/test.exs specify the endpoint to be used for routing requests:

config :phoenix_test, :endpoint, MyAppWeb.Endpoint

It would be great to be able to configure the endpoint module at smaller scope than the global scope.

In the project I currently work on (my first professional Elixir/Phoenix project) we have a single project/app that provides endpoints for different clients, f.i. admin vs normal user, both using the same application core.

Since umbrella apps don't seem to be supported by quite a few libraries, we decided to stick with a "normal" app and implement two endpoint modules (and all the things "underneath" each of them) in it.

For testing, we created two different ConnCase modules, f.i. AdminConnCase and UserConnCase, each using the proper endpoint module. Depending on which endpoint we want to test, we use the respective ConnCase module.

Do you think that something similar is possible with PhoenixTest?

Maybe there already is some other solution/workaround, f.i. by having config/admin_test.exs and config/user_test.exs, but I am hesitating because I fear the consequences, f.i. that we would have to duplicate way too many test helpers and other things that just depend on the normal :test environment.

germsvel commented 7 months ago

Hi @sesamzoo, thanks for opening this issue! Let me think about how we could support this. I have to look at the implementation to get a sense of what it would take to do this.

germsvel commented 7 months ago

Hi @sesamzoo, I don't see a straightforward way to do this. Unfortunately, we rely on the @endpoint at compile time (since we need to have it defined to route requests in Phoenix). I'd be happy to look at a PR if you think there might be a way, but I couldn't think of anything after a glance at the code.

For now, I'll close this. But if you think there's a way to do this, let me know, and we can reopen it.

sesamzoo commented 7 months ago

Hi @germsvel, I'll try to find some time to dig deeper in the LiveViewTest sources and learn more about Elixir's macros, because that seems to allow for specifying the @endpoint at test module scope.

megalithic commented 6 months ago

Hi @germsvel, I'll try to find some time to dig deeper in the LiveViewTest sources and learn more about Elixir's macros, because that seems to allow for specifying the @endpoint at test module scope.

Came here for the same reason as you, @sesamzoo. You're not alone in needing the same functionality.

megalithic commented 4 months ago

@sesamzoo; you happen to have any ways around this, in the mean time?

sesamzoo commented 4 months ago

@sesamzoo; you happen to have any ways around this, in the mean time?

@megalithic, unfortunately not. At the time when I had a look it seemed like it's one level of indirection too much for making it work. I hope, I find some spare time, to have another look.

As a possible alternative I will also have another look at the scope macro's host option. That would solve some other issues that come with multiple endpoint modules in a single application (f.i. sharing components instead of duplicating them; having to provide two implementations for the Endpoint module's helper functions) and wouldn't require the feature asked for in this issue.