testModule: Integration testing for Shiny modules or server functions

Description Usage Arguments Examples

View source: R/test-module.R

Description

Offer a way to test the reactive interactions in Shiny — either in Shiny modules or in the server portion of a Shiny application. For more information, visit the Shiny Dev Center article on integration testing.

Usage

1
2
3
testModule(module, expr, ...)

testServer(expr, appDir = NULL)

Arguments

module

The module to test

expr

Test code containing expectations. The test expression will run in the module's environment, meaning that the module's parameters (e.g. input, output, and session) will be available along with any other values created inside of the module.

...

Additional arguments to pass to the module function. These arguments are processed with rlang::list2() and so are dynamic.

appDir

The directory root of the Shiny application. If NULL, this function will work up the directory hierarchy — starting with the current directory — looking for a directory that contains an app.R or server.R file.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module <- function(input, output, session, multiplier = 2, prefix = "I am ") {
  myreactive <- reactive({
    input$x * multiplier
  })
  output$txt <- renderText({
    paste0(prefix, myreactive())
  })
}

# Basic Usage
# -----------
testModule(module, {
  session$setInputs(x = 1)
  # You're also free to use third-party
  # testing packages like testthat:
  #   expect_equal(myreactive(), 2)
  stopifnot(myreactive() == 2)
  stopifnot(output$txt == "I am 2")

  session$setInputs(x = 2)
  stopifnot(myreactive() == 4)
  stopifnot(output$txt == "I am 4")
  # Any additional arguments, below, are passed along to the module.
}, multiplier = 2)

# Advanced Usage
# --------------
multiplier_arg_name = "multiplier"
more_args <- list(prefix = "I am ")
testModule(module, {
  session$setInputs(x = 1)
  stopifnot(myreactive() == 2)
  stopifnot(output$txt == "I am 2")
  # !!/:= and !!! from rlang are used below to splice computed arguments
  # into the testModule() argument list.
}, !!multiplier_arg_name := 2, !!!more_args)

tomkuipers1402/shiny documentation built on Feb. 13, 2020, 7:22 p.m.