loco-rs / loco

🚂 🦀 The one-person framework for Rust for side-projects and startups
https://loco.rs
Apache License 2.0
5.47k stars 235 forks source link

loco-macros: clean up test pragmas #57

Open jondot opened 11 months ago

jondot commented 11 months ago

currently start of each test in tests/ contains this pragma:

// TODO: see how to dedup / extract this to app-local test utils
// not to framework, because that would require a runtime dep on insta
macro_rules! configure_insta {
    ($($expr:expr),*) => {
        let mut settings = insta::Settings::clone_current();
        settings.set_prepend_module_to_snapshot(false);
        settings.set_snapshot_suffix("auth_request");
        let _guard = settings.bind_to_scope();
    };
}

#[tokio::test]
#[serial]
async fn can_get_current_user() {
    configure_insta!();

    testing::request::<App, Migrator, _, _>(|request, ctx| async move {
...
...

we need to somehow share this macro, or create a new macro that wraps configuring insta as well as booting an app (possibly).

also, making:

#[tokio::test]
#[serial]

into our own attribute loco::test which wraps these might make sense as well.

Bogay commented 6 months ago

In my project, I extract the configure_insta! into tests/mod.rs and use params to configure the snapshot suffix. Here is usage example:

/// in tests/mod.rs
macro_rules! configure_insta {
    ($suffix:expr) => {
        let mut settings = insta::Settings::clone_current();
        settings.set_prepend_module_to_snapshot(false);
        settings.set_snapshot_suffix($suffix);
        let _guard = settings.bind_to_scope();
    };
}
pub(crate) use configure_insta;

/// in tests/models/users.rs
macro_rules! configure_insta {
    () => {
        crate::configure_insta!("users")
    };
}

#[tokio::test]
#[serial]
async fn test_can_validate_model() {
    configure_insta!();
    // ...test code
}

You can see the actual commit in: https://github.com/Bogay/normal-oj/pull/3/commits/e3ed1a5e5915a5889929d7ac8965bf8d6814c893

Do you think this is the right approach to extract configure_insta!?

joshka commented 1 month ago

Can be closed - loco-macros was just removed.