Skip to content

expect_output doesn't let you control console width #805

@nealrichardson

Description

@nealrichardson

Unlike expect_known_output(), which lets you pass a width argument, expect_output() has a hard-coded 80-character width. Even though capture_output() does take a width arg, expect_output() calls quasi_capture(), which calls capture_output() with no additional args, so you can't pass it through.

Here's a test that illustrates:

context("debug")
test_that("expect_output respects 'width'", {
    withr::local_options(list(width = 10))
    expect_equal(getOption("width"), 10)
    expect_output(
        expect_equal(getOption("width"), 10),
        NA
    )
})

The width check inside of expect_output fails:

debug: .1.

══ Failed ══════════════════════════════════════════════════════════════════════
── 1. Failure: expect_output respects 'width' (@test-debug.R#5)  ───────────────
getOption("width") not equal to 10.
1/1 mismatches
[1] 80 - 10 == 70

I'm happy to submit a patch if you can provide guidance on where you'd like it solved. My instinct would be to move the withr::local_options setting from where it is inside eval_with_output to test_file() or somewhere higher so that you can control print width options within your test files normally using options(width = whatever). You could also extend expect_output to have a width argument to match expect_known_output, but I suspect that's a bigger refactor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions