You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current Shiny documentation emphasizes that moduleServer is the recommended approach and can be tested using testServer, whereas callModule does not have direct testing support. However, many legacy Shiny applications still rely on callModule, making it difficult to refactor or maintain without the ability to write tests.
Proposed Solution
We can implement a function, testCallModule(), that enables testing for callModule by evaluating the module's server-side expression as a server function with arguments bound in its environment. This allows us to inject the testing expression into testServer.
Thanks for opening an issue and for providing a work around for anyone wanting to test callModule(). Before opening a PR, the next step would be to do some background research into the testing situation with callModule(). Here are some things we'd want to review:
A small reprex demonstrating how callModule() can't be tested in the same way as moduleServer(). Ideally, that'd be the same module used with both functions, but only moduleServer() is able to be tested with testServer().
What differentiates callModule() and moduleServer() such that testing with the second approach is possible?
Could we make an internal change to testServer() or related infrastructure that could fix this issue, or are we stuck with the existing limitations?
I think it's certainly reasonable to support testing legacy code in Shiny, but we have balance our priorities and focus. If you're interested in moving this forward, having the above context would be a big help and would make it easier for us to review proposed solutions.
Context
The current Shiny documentation emphasizes that
moduleServer
is the recommended approach and can be tested usingtestServer
, whereascallModule
does not have direct testing support. However, many legacy Shiny applications still rely oncallModule
, making it difficult to refactor or maintain without the ability to write tests.Proposed Solution
We can implement a function,
testCallModule()
, that enables testing forcallModule
by evaluating the module's server-side expression as a server function with arguments bound in its environment. This allows us to inject the testing expression intotestServer
.Here's the code:
Usage Example
Here’s how this function could be used in practice:
Request
Could this functionality be officially supported in Shiny? It offers a way to refactor and stabilize legacy code.
If the Shiny team sees value in this, I’d happily open a PR to contribute to this functionality.
The text was updated successfully, but these errors were encountered: