Skip to content

Commit

Permalink
Add snapshot testing with insta
Browse files Browse the repository at this point in the history
  • Loading branch information
mawkler committed Nov 15, 2024
1 parent 67fedc0 commit 231b16e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 47 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ assert-json-diff = "2.0.2"
assert_cmd = "2.0.16"
wiremock = "0.6.2"
predicates = "3.1.2"
insta = { version = "1.41.1", features = ["yaml", "json"] }

# Improved insta runtime
[profile.dev.package]
insta.opt-level = 3
similar.opt-level = 3
66 changes: 19 additions & 47 deletions tests/integration/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,72 +27,45 @@ fn run(
cmd
}

fn assert_snapshot_predicate() -> predicates::function::FnPredicate<impl Fn(&str) -> bool, str> {
predicates::function::function(move |output: &str| {
insta::assert_snapshot!(output);
true
})
}

#[tokio::main]
#[test]
async fn test_get_timesheet() {
async fn get_timesheet() {
// Given
let mock_server = MockServer::start().await;
mock_get_instance(None).mount(&mock_server).await;
mock_get_table_rows(None).mount(&mock_server).await;
create_test_config();

let expected = serde_json::json!({
"lines": [
{
"job": "Job One",
"task": "Some task one",
"week": {
"monday": 8.0,
"tuesday": 0.0,
"wednesday": 0.0,
"thursday": 0.0,
"friday": 0.0,
"saturday": 0.0,
"sunday": 0.0
}
},
{
"job": "Job One",
"task": "Some task two",
"week": {
"monday": 0.0,
"tuesday": 0.0,
"wednesday": 0.0,
"thursday": 0.0,
"friday": 0.0,
"saturday": 0.0,
"sunday": 0.0
}
}
]
});

// When
let output = run_json(["get", "--format", "json"], &mock_server.uri());

// Then
assert_json_diff::assert_json_include!(
expected: expected,
actual: output
);
insta::assert_json_snapshot!(output);
}

#[tokio::main]
#[test]
async fn test_set_hours() {
async fn set_hours() {
// Given
let mock_server = MockServer::start().await;
mock_get_instance(None).mount(&mock_server).await;
mock_get_table_rows(None).mount(&mock_server).await;
// These mocks aren't actually required
// These mocks aren't actually required here
// mock_job_number_search(None).mount(&mock_server).await;
// mock_tasks_search(None).mount(&mock_server).await;
mock_add_row(None).mount(&mock_server).await;
mock_set_hours(None).mount(&mock_server).await;
create_test_config();

// When
let cmd = [
let command = [
"set",
"8",
"--job",
Expand All @@ -102,15 +75,16 @@ async fn test_set_hours() {
"--day",
"monday",
];
let mut output = run(cmd, &mock_server.uri());
let mut output = run(command, &mock_server.uri());

// Then
// TODO: try to assert on the values sent to the mock
output.assert().success();
}

#[tokio::main]
#[test]
async fn test_set_hours_err() {
async fn set_hours_err() {
// Given
let mock_server = MockServer::start().await;
mock_get_instance(None).mount(&mock_server).await;
Expand All @@ -121,21 +95,19 @@ async fn test_set_hours_err() {
create_test_config();

// When
let cmd = [
let command = [
"set",
"--job",
"doesn't exist",
"--task",
"some task one",
"8",
];
let mut output = run(cmd, &mock_server.uri());
let mut output = run(command, &mock_server.uri());

let expected_stdoud_prefix = "Something went wrong when adding a new line to the time sheet: \
did not find job 'doesn't exist' and task 'some task one', even after creating a new line \
for it";
// Then
output
.assert()
.stderr(predicates::str::starts_with(expected_stdoud_prefix))
.stderr(assert_snapshot_predicate())
.failure();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/integration/cli.rs
expression: output
snapshot_kind: text
---
Something went wrong when adding a new line to the time sheet: did not find job 'doesn't exist' and task 'some task one', even after creating a new line for it

Error stack:
- did not find job 'doesn't exist' and task 'some task one', even after creating a new line for it
35 changes: 35 additions & 0 deletions tests/integration/snapshots/integration__cli__get_timesheet.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
source: tests/integration/cli.rs
expression: output
snapshot_kind: text
---
{
"lines": [
{
"job": "Job One",
"task": "Some task one",
"week": {
"friday": 0.0,
"monday": 8.0,
"saturday": 0.0,
"sunday": 0.0,
"thursday": 0.0,
"tuesday": 0.0,
"wednesday": 0.0
}
},
{
"job": "Job One",
"task": "Some task two",
"week": {
"friday": 0.0,
"monday": 0.0,
"saturday": 0.0,
"sunday": 0.0,
"thursday": 0.0,
"tuesday": 0.0,
"wednesday": 0.0
}
}
]
}

0 comments on commit 231b16e

Please sign in to comment.