Struct insta::Settings

source ·
pub struct Settings { /* private fields */ }
Expand description

Configures how insta operates at test time.

Settings are always bound to a thread and some default settings are always available. These settings can be changed and influence how insta behaves on that thread. They can either temporarily or permanently changed.

This can be used to influence how the snapshot macros operate. For instance it can be useful to force ordering of maps when unordered structures are used through settings.

Some of the settings can be changed but shouldn’t as it will make it harder for tools like cargo-insta or an editor integration to locate the snapshot files.

Settings can also be configured with the with_settings! macro.

Example:

use insta;

let mut settings = insta::Settings::clone_current();
settings.set_sort_maps(true);
settings.bind(|| {
    // runs the assertion with the changed settings enabled
    insta::assert_snapshot!(...);
});

Implementations§

source§

impl Settings

source

pub fn new() -> Settings

Returns the default settings.

It’s recommended to use clone_current instead so that already applied modifications are not discarded.

source

pub fn clone_current() -> Settings

Returns a copy of the current settings.

source

pub fn set_sort_maps(&mut self, value: bool)

Enables forceful sorting of maps before serialization.

Note that this only applies to snapshots that undergo serialization (eg: does not work for assert_debug_snapshot!.)

The default value is false.

source

pub fn sort_maps(&self) -> bool

Returns the current value for map sorting.

source

pub fn set_prepend_module_to_snapshot(&mut self, value: bool)

Disables prepending of modules to the snapshot filename.

By default the filename of a snapshot is <module>__<name>.snap. Setting this flag to false changes the snapshot filename to just <name>.snap.

The default value is true.

source

pub fn prepend_module_to_snapshot(&self) -> bool

Returns the current value for module name prepending.

source

pub fn set_allow_empty_glob(&mut self, value: bool)

Allows the glob! macro to succeed if it matches no files.

By default the glob macro will fail the test if it does not find any files to prevent accidental typos. This can be disabled when fixtures should be conditional.

The default value is false.

source

pub fn allow_empty_glob(&self) -> bool

Returns the current value for the empty glob setting.

source

pub fn set_snapshot_suffix<I: Into<String>>(&mut self, suffix: I)

Sets the snapshot suffix.

The snapshot suffix is added to all snapshot names with an @ sign between. For instance if the snapshot suffix is set to "foo" and the snapshot would be named "snapshot" it turns into "snapshot@foo". This is useful to separate snapshots if you want to use test parameterization.

source

pub fn remove_snapshot_suffix(&mut self)

Removes the snapshot suffix.

source

pub fn snapshot_suffix(&self) -> Option<&str>

Returns the current snapshot suffix.

source

pub fn set_input_file<P: AsRef<Path>>(&mut self, p: P)

Sets the input file reference.

This value is completely unused by the snapshot testing system but it lets you store some meta data with a snapshot that refers you back to the input file. The path stored here is made relative to the workspace root before storing with the snapshot.

source

pub fn remove_input_file(&mut self)

Removes the input file reference.

source

pub fn input_file(&self) -> Option<&Path>

Returns the current input file reference.

source

pub fn set_description<S: Into<String>>(&mut self, value: S)

Sets the description.

The description is stored alongside the snapshot and will be displayed in the diff UI. When a snapshot is captured the Rust expression for that snapshot is always retained. However sometimes that information is not super useful by itself, particularly when working with loops and generated tests. In that case the description can be set as extra information.

See also set_info.

source

pub fn remove_description(&mut self)

Removes the description.

source

pub fn description(&self) -> Option<&str>

Returns the current description

source

pub fn set_info<S: Serialize>(&mut self, s: &S)

Available on crate feature serde only.

Sets the info.

The info is similar to description but for structured data. This is stored with the snapshot and shown in the review UI. This for instance can be used to show extended information that can make a reviewer better understand what the snapshot is supposed to be testing.

As an example the input parameters to the function that creates the snapshot can be persisted here.

Alternatively you can use set_raw_info instead.

source

pub fn set_raw_info(&mut self, content: &Content)

Sets the info from a content object.

This works like set_info but does not require serde.

source

pub fn remove_info(&mut self)

Removes the info.

source

pub fn has_info(&self) -> bool

Returns the current info

source

pub fn set_omit_expression(&mut self, value: bool)

If set to true, does not retain the expression in the snapshot.

source

pub fn omit_expression(&self) -> bool

Returns true if expressions are omitted from snapshots.

source

pub fn add_redaction<R: Into<Redaction>>( &mut self, selector: &str, replacement: R )

Available on crate feature redactions only.

Registers redactions that should be applied.

This can be useful if redactions must be shared across multiple snapshots.

Note that this only applies to snapshots that undergo serialization (eg: does not work for assert_debug_snapshot!.)

source

pub fn add_dynamic_redaction<I, F>(&mut self, selector: &str, func: F)where I: Into<Content>, F: Fn(Content, ContentPath<'_>) -> I + Send + Sync + 'static,

Available on crate feature redactions only.

Registers a replacement callback.

This works similar to a redaction but instead of changing the value it asserts the value at a certain place. This function is internally supposed to call things like assert_eq!.

This is a shortcut to add_redaction(selector, dynamic_redaction(...));

source

pub fn sort_selector(&mut self, selector: &str)

Available on crate feature redactions only.

A special redaction that sorts a sequence or map.

This is a shortcut to add_redaction(selector, sorted_redaction()).

source

pub fn set_redactions<R: Into<Redactions>>(&mut self, redactions: R)

Available on crate feature redactions only.

Replaces the currently set redactions.

The default set is empty.

source

pub fn clear_redactions(&mut self)

Available on crate feature redactions only.

Removes all redactions.

source

pub fn add_filter<S: Into<String>>(&mut self, regex: &str, replacement: S)

Available on crate feature filters only.

Adds a new filter.

Filters are similar to redactions but are applied as regex onto the final snapshot value. This can be used to perform modifications to the snapshot string that would be impossible to do with redactions because for instance the value is just a string.

The first argument is the regex pattern to apply, the second is a replacement string. The replacement string has the same functionality as the second argument to Regex::replace.

This is useful to perform some cleanup procedures on the snapshot for unstable values.

settings.add_filter(r"\b[[:xdigit:]]{32}\b", "[UID]");
source

pub fn set_filters<F: Into<Filters>>(&mut self, filters: F)

Available on crate feature filters only.

Replaces the currently set filters.

The default set is empty.

source

pub fn clear_filters(&mut self)

Available on crate feature filters only.

Removes all filters.

source

pub fn set_snapshot_path<P: AsRef<Path>>(&mut self, path: P)

Sets the snapshot path.

If not absolute it’s relative to where the test is in.

Defaults to snapshots.

source

pub fn snapshot_path(&self) -> &Path

Returns the snapshot path.

source

pub fn bind<F: FnOnce() -> R, R>(&self, f: F) -> R

Runs a function with the current settings bound to the thread.

This is an alternative to bind_to_scope which does not require holding on to a drop guard. The return value of the closure is passed through.

let mut settings = Settings::clone_current();
settings.set_sort_maps(true);
settings.bind(|| {
    // do stuff here
});
source

pub fn bind_async<F: Future<Output = T>, T>( &self, future: F ) -> impl Future<Output = T>

Like bind but for futures.

This lets you bind settings for the duration of a future like this:

let settings = Settings::new();
settings.bind_async(async {
    // do assertions here
}).await;
source

pub fn bind_to_scope(&self) -> SettingsBindDropGuard

Binds the settings to the current thread and resets when the drop guard is released.

This is the recommended way to temporarily bind settings and replaces the earlier bind_to_scope and relies on drop guards. An alterantive is bind which binds for the duration of the block it wraps.

let mut settings = Settings::clone_current();
settings.set_sort_maps(true);
let _guard = settings.bind_to_scope();
// do stuff here

Trait Implementations§

source§

impl Clone for Settings

source§

fn clone(&self) -> Settings

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Settings

source§

fn default() -> Settings

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.