Skip to main content

test

Function test 

Source
pub fn test() -> NuTester
Expand description

Create a NuTester for running Nushell snippets in tests.

Prefer this helper over the nu! macro for most tests. It runs snippets in-process instead of shelling out to a subprocess, which makes tests faster and lets you pass and read values directly without inferring from stdout or stderr. The nu! macro executes the nu binary, and changes in a single crate might not trigger a rebuild of that binary, so tests can run against stale behavior unless you run cargo build first. Using this helper avoids that by executing against the in-process engine components.

The tester starts from a default EngineState with the standard library loaded, and a fresh Stack. Use the returned value to configure environment variables or the working directory before running code.

§Environment behavior

  • This tester does not inherit process environment variables.
  • Any variables you want available to the engine must be added explicitly via NuTester::env (or convenience helpers like NuTester::locale).
  • Experimental options and other external environment settings are respected when constructing the underlying engine state for the current test group.

§Examples

use nu_test_support::prelude::*;

let code = "use std/util ellie; ellie | ansi strip";
let value: String = test().run(code)?;
assert_eq!(value, r#"
     __  ,
 .--()°'.'
'|, . ,'
 !_-(_\
"#.trim_matches('\n'));
use nu_test_support::prelude::*;

let mut tester = test()
    .env("FOO", "bar")
    .cwd("crates/nu-test-support");

let value: String = tester.run("$env.FOO")?;
assert_eq!(value, "bar");