rstudio / shinytest

Automated testing for shiny apps
https://rstudio.github.io/shinytest/
Other
225 stars 55 forks source link

AppDriver$new registers the app path with `devtools::test()` but fails with `devtools::check()` #432

Open matt-sd-watson opened 1 year ago

matt-sd-watson commented 1 year ago

I am implementing snapshot/screenshot-based testing for a Shiny app that is wrapped in a R package using shinytest2.

The basic layout of the directories in my package is as follows (some files omitted for simplicity);

.
├── app.R
├── R
│   ├── app.R
├── README.md
├── tests
│   ├── testthat
│   │   ├── setup-shinytest2.R
│   │   ├── _snaps
│   │   │   └── linux-4.2
│   │   │       └── shinytest2
│   │   │           └── myapp-001.png
│   │   ├── test-shinytest2.R
│   └── testthat.R

The app is calling a function in app.R from the base directory that originally comes from the R/app.R directory, which is a shinyApp() function call on a UI and server function, respectively. The contents of app.R is:

library(BiocManager)
options(repos = BiocManager::repositories())
options(shiny.autoload.r=FALSE)

pkgload::load_all()
myapp::myapp()

The contents of the test-shinytest2.R file are as follows:

library(shinytest2)

test_that("{shinytest2} recording: my-app", {
  app <- AppDriver$new(app_dir = test_path("../../"),
                       variant = platform_variant(), name = "my-app", height = 732, 
      width = 1161, load_timeout = 1e+05
      # shinyOptions = list(test.mode = TRUE)
      )
  app$expect_screenshot()
})

When using devtools::test(), this test passes with no problems, but when calling through devtools::check(), the following error occurs:

Error in `app_initialize(self, private, app_dir = app_dir, ..., load_timeout = load_timeout, 
    timeout = timeout, wait = wait, expect_values_screenshot_args = expect_values_screenshot_args, 
    screenshot_args = screenshot_args, check_names = check_names, 
    name = name, variant = variant, view = view, height = height, 
    width = width, seed = seed, clean_logs = clean_logs, shiny_args = shiny_args, 
    render_args = render_args, options = options)`: Error starting shiny application:
Loading required package: shiny
Error in shinyAppDir(x) : App dir must contain either app.R or server.R.

ℹ You can inspect the failed AppDriver object via `rlang::last_error()$app`
ℹ AppDriver logs:
{shinytest2} R info   04:02:23.73 Start AppDriver initialization
{shinytest2} R info   04:02:23.73 Starting Shiny app
{shinytest2} R info   04:02:24.83 Error while initializing AppDriver:
                                  Error starting shiny application:
                                  Loading required package: shiny
                                  Error in shinyAppDir(x) : App dir must contain either app.R or server.R.
{shiny}      R stderr ----------- Loading required package: shiny
{shiny}      R stderr ----------- Error in shinyAppDir(x) : App dir must contain either app.R or server.R.