Flaky Mimic.expect/4 test failure with Function not defined

I recently started using Mimic in my test suite. I have an intermittent test failure that looks similar to this:

  1) test maybe_create_user/1 creates new user with api call to create_new_app_user/2 (MyApp.AccountsTest)
     test/my_app/accounts_test.exs:643
     ** (ArgumentError) Function create_new_app_user/2 not defined for MyApp.API
     code: |> Mimic.expect(:create_new_app_user, fn _, env ->
     stacktrace:
       (mimic 1.7.4) lib/mimic.ex:467: Mimic.raise_if_not_exported_function!/3
       (mimic 1.7.4) lib/mimic.ex:230: Mimic.expect/4
       test/my_app/accounts_test.exs:652: (test)

Looking at the Mimic source code here and then here, it is calling Kernal.function_exported?.

The docs on Kernal.function_exported? mention it can fail if the module is not loaded. That is the issue because the function does exist in the module. However, it only happens maybe 10% of the time.

Is there a better way to ensure the module is loaded instead of calling Code.ensure_loaded/1 before I call Mimic.expect/4 in my tests?

Maybe put Application.ensure_all_started(:mimic) in your test_helper.exs?

1 Like

Thanks for the suggestion! I hadn’t added it before, but unfortunately, the test still failed after adding it to my project.

You can try using the --preload-modules option on mix run, which iirc might be forwarded from mix test.

Generally I’d argue that this is not great code for such a library though given mix test should generally be expected to run in interactive mode.