Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Enable parallel tests on Windows
- Add `assert_not_called`
- Improve `find_total_tests` performance
- Added `assert_match_snapshot_ignore_colors`

## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23

Expand Down
17 changes: 17 additions & 0 deletions docs/snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ Some tests failed
You need to run the tests for this example twice to see them work.
The first time you run them, the snapshots will be generated and the second time they will be asserted.
:::

## assert_match_snapshot_ignore_colors
> `assert_match_snapshot_ignore_colors "actual"`

Like `assert_match_snapshot` but ANSI escape codes in `actual` are ignored. This allows
verifying the output text while disregarding its style.

::: code-group
```bash [Example]
function test_success() {
assert_match_snapshot_ignore_colors "$(printf '\e[31mHello\e[0m World!')"
}
function test_failure() {
assert_match_snapshot_ignore_colors "World"
}
```
:::
37 changes: 37 additions & 0 deletions src/assert_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,40 @@ function assert_match_snapshot() {

state::add_assertions_passed
}

function assert_match_snapshot_ignore_colors() {
local actual
actual=$(echo -n "$1" | sed -r 's/\x1B\[[0-9;]*[mK]//g' | tr -d '\r')

local directory
directory="./$(dirname "${BASH_SOURCE[1]}")/snapshots"
local test_file
test_file="$(helper::normalize_variable_name "$(basename "${BASH_SOURCE[1]}")")"
local snapshot_name
snapshot_name="$(helper::normalize_variable_name "${FUNCNAME[1]}").snapshot"
local snapshot_file
snapshot_file="${directory}/${test_file}.${snapshot_name}"

if [[ ! -f "$snapshot_file" ]]; then
mkdir -p "$directory"
echo "$actual" > "$snapshot_file"

state::add_assertions_snapshot
return
fi

local snapshot
snapshot=$(tr -d '\r' < "$snapshot_file")

if [[ "$actual" != "$snapshot" ]]; then
local label
label=$(helper::normalize_test_function_name "${FUNCNAME[1]}")

state::add_assertions_failed
console_results::print_failed_snapshot_test "$label" "$snapshot_file"

return
fi

state::add_assertions_passed
}
44 changes: 44 additions & 0 deletions tests/unit/assert_snapshot_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,47 @@ function test_unsuccessful_assert_match_snapshot() {

assert_equals "$expected" "$actual"
}

function test_successful_assert_match_snapshot_ignore_colors() {
local colored
colored=$(printf '\e[31mHello\e[0m World!')
assert_empty "$(assert_match_snapshot_ignore_colors "$colored")"
}

function test_creates_a_snapshot_ignore_colors() {
local snapshot_file_path=tests/unit/snapshots/assert_snapshot_test_sh.test_creates_a_snapshot_ignore_colors.snapshot
local expected=$((_ASSERTIONS_SNAPSHOT + 1))

assert_file_not_exists $snapshot_file_path

local colored
colored=$(printf '\e[32mExpected\e[0m snapshot')

assert_match_snapshot_ignore_colors "$colored"

assert_same "$expected" "$_ASSERTIONS_SNAPSHOT"
assert_file_exists $snapshot_file_path
assert_same "Expected snapshot" "$(cat $snapshot_file_path)"

rm $snapshot_file_path
}

function test_unsuccessful_assert_match_snapshot_ignore_colors() {
local expected

if dependencies::has_git; then
expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors
Expected to match the snapshot
[-Actual-]{+Expected+} snapshot[-text-]")"
else
expected="$(printf "✗ Failed: Unsuccessful assert match snapshot ignore colors
Expected to match the snapshot")"
fi

local actual
local colored
colored=$(printf '\e[31mExpected snapshot\e[0m')
actual="$(assert_match_snapshot_ignore_colors "$colored")"

assert_equals "$expected" "$actual"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Actual snapshot text
Loading