Stream: beginners

Topic: Rust platform tests


view this post on Zulip Luke Boswell (Feb 25 2023 at 07:06):

Are there any examples of how to run rust tests in a Roc platform? I feel like I'm missing something obvious, but unsure. I've been looking through the example platforms and haven't found any tests, so wondering if this is possible.

For reference, I was wanting to add a test to verify html syntax parsing is working correctly in the static-site example platform. Here is a link to the test I added, but I haven't figure out how to run it.

Running cargo test I get something like;

error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-arch" "arm64" "/var/fold
  ... many lines removed ..
   "-lresolv" "-lc" "-lm" "-liconv" "-L" "/Users/luke/.rustup/toolchains/1.65.0-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "-o" "/Users/luke/Documents/GitHub/roc/examples/static-site-gen/platform/target/debug/deps/host-12b7a8d1a1a85c2f" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: ld: library not found for -lapp
          clang: error: linker command failed with exit code 1 (use -v to see invocation)


warning: `host` (lib test) generated 2 warnings (1 duplicate)
error: could not compile `host` due to previous error; 2 warnings emitted
warning: build failed, waiting for other jobs to finish...
warning: `host` (lib) generated 2 warnings (1 duplicate)

view this post on Zulip Brian Carroll (Feb 25 2023 at 07:59):

Yeah if you are trying to run Rust and Roc together then it has to be driven from Roc. Roc knows how to deal with rust but not the other way around

view this post on Zulip Luke Boswell (Feb 25 2023 at 08:18):

Thank you. What is the preferred strategy for writing tests in platform host code then? Would it be separate into another crate and unit test there?

view this post on Zulip Brian Carroll (Feb 25 2023 at 09:50):

I don't think it's been done enough for a preferred strategy to have developed yet!
If I were doing it, I'd mostly write Rust-only tests for the Rust code. If some function normally accepts an input from a Roc app then the test would just pass it a value of that type in Rust instead. Similarly if your Rust function normally calls a Roc function then have it call some test function in Rust instead.
At some point I guess you really do want a test that crosses the boundary. So I guess I'd make a test app that you compile and run. It has an example Roc app that calls all of your platform functions in certain ways. Exits with a failure exit code if it fails.

view this post on Zulip Richard Feldman (Feb 25 2023 at 14:16):

we have a WIP feature of expect-fx which will in the future let you write stuff like expect-fx myTask and it will run the task during roc test

view this post on Zulip Richard Feldman (Feb 25 2023 at 14:16):

and then you can put various expects inside the task, as normal

view this post on Zulip Brendan Hansknecht (Feb 25 2023 at 20:02):

You can also run rusts testing strategy by first building a shared library of a roc app.

view this post on Zulip Brendan Hansknecht (Feb 25 2023 at 20:02):

Then rust can build test just fine linking against libapp.so


Last updated: Jul 05 2025 at 12:14 UTC