Skip to content

ggplot2 font family changes are not reflected by expect_doppelganger #158

@mcaselli

Description

@mcaselli

See https://stackoverflow.com/questions/79784967/, largely reproduced below.

In creating unit tests for an r package that provides a custom ggplot2 theme, I've noticed that the snapshots created by vdiffr::expect_doppelganger() aren't reflecting font family changes that work in interactive testing.

a toy package that uses testthat has two files:

R/fonty_theme.R

# a theme provided by the package
fonty_theme <- function(base_family = ""){
  thm <- ggplot2::theme_minimal(base_family = base_family)
  return(thm)
}

# test helper to create base plot
base_plot <- function() {
  ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
    ggplot2::geom_point() +
    ggplot2::labs(
      x = "Miles per Gallon (MPG)",
      y = "Weight (1000 lbs)"
    ) +
    ggplot2::theme_dark()
}

and tests/testthat/test-fonty_theme.R

test_that("fonty_theme works with default family", {
  vdiffr::expect_doppelganger("fonty_theme default", {
    base_plot() + fonty_theme()
  })
})

test_that("fonty_theme works with mono", {
  vdiffr::expect_doppelganger("fonty_theme mono", {
    base_plot() + fonty_theme("mono")
  })
})

test_that("fonty_theme works with serif", {
  vdiffr::expect_doppelganger("fonty_theme serif", {
    base_plot() + fonty_theme("serif")
  })
})

after running devtools::load_all("."), interactive testing with base_plot() + fonty_theme("mono") produces the expected plot with monospaced font:

expected plot

But when you test the package with devtools::test() and inspect the snapshots snapshots produced by expect_doppelganger() saved in testhat\_snaps\fonty_theme\, the snapshot fonty-theme-mono.svg is using a serif font. Indeed the snapshots for all three test cases are identical, all using serif font.

svg snapshot

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions