I have been writing some unit tests in sway via the #[test]
annotation feature. There seems to be some inconsistency in the runtime of these tests compared to running normal scripts via the SDK. Specifically, I noticed this bug while I was building a library that uses the signed integers from sway libs.
In the sway unit tests, the VM panics and fails the test for any standard math operations on the I64
type.
I was able to reproduce this inside of the sway libs repo by adding a simple test to any of the signed integer libraries. This commit demonstrates this in a forked repo.
#[test]
fn test_add() {
// note this is identical code from the tests in `../../tests/src/signed_integers/signed_i64/src/main.sw`
// this should not fail, but does if you run `forc test` in this dir
let one = I64::from(1u64);
let mut res = one + I64::from(1u64);
assert(res == I64::from(2u64));
}
forc test:
Bytecode size is 316 bytes.
Running 2 tests
test test_add ... FAILED (570.375µs)
test test_nothing ... ok (58.166µs)
failures:
- test test_add, "/Users/someguy/code-for-reading/sway-libs-demo-unit-test-bug/libs/signed_integers/src/i64.sw":162
Result: FAILED. 1 passed. 1 failed. Finished in 628.541µs.
fuelup show:
Default host: aarch64-apple-darwin
fuelup home: /Users/someguy/.fuelup
installed toolchains
--------------------
latest-aarch64-apple-darwin (default)
active toolchain
-----------------
latest-aarch64-apple-darwin (default)
forc : 0.33.1
- forc-client
- forc-deploy : 0.33.1
- forc-run : 0.33.1
- forc-doc : 0.33.1
- forc-explore : 0.28.1
- forc-fmt : 0.33.1
- forc-index : 0.2.1
- forc-lsp : 0.33.1
- forc-wallet : 0.1.3
fuel-core : 0.16.1
fuel-indexer : 0.2.0
3 Likes
Thanks for the report and for digging in! Forc’s testing functionality is brand new so feedback is appreciated 
fuelup show:
Could you confirm that you’re getting the same behaviour on the fuelup
nightly toolchain too? And could you also share the version of the Rust SDK that you’re using to run your script that works?
For some added context: forc test
works by running its own unique instance of the Fuel VM interpreter for each #[test]
. In theory, executing with it should be the same as executing the script on a freshly spawned local fuel-core node, though without the indirection of networking calls etc.
We should at least be outputting the revert code and an associated message on failure too - I’ll open an issue at the Sway repo for this as a high priority.
1 Like
Oh also - can you try running forc test
with the new raw log output? E.g.
forc test --log --pretty-print
This will dump the raw logs from the receipt that might help to debug what’s going on.
switched back over to nightly
❯ fuelup show
Default host: aarch64-apple-darwin
fuelup home: /Users/someguy/.fuelup
installed toolchains
--------------------
latest-aarch64-apple-darwin
nightly-aarch64-apple-darwin (default)
active toolchain
-----------------
nightly-aarch64-apple-darwin (default)
forc : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-client
- forc-deploy : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-run : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-doc : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-explore : 0.28.1+nightly.20230201.4bb7392eed
- forc-fmt : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-index - not found
- forc-lsp : 0.33.1+nightly.20230201.b6cd10ba9d
- forc-wallet : 0.1.3+nightly.20230201.543bc1f4d7
fuel-core : 0.16.1+nightly.20230201.d761d2b4bb
fuel-indexer - not found
and the the logs look empty:
❯ forc test --logs --pretty-print
WARNING! unused manifest key: project.target
WARNING! unused manifest key: project.target
Compiled library "core".
Compiled library "std".
Compiled library "signed_integers".
Bytecode size is 316 bytes.
Running 2 tests
test test_add ... FAILED (119.167µs)
[]
test test_nothing ... ok (44.416µs)
[]
failures:
- test test_add, "/Users/someguy/code-for-reading/sway-libs-demo-unit-test-bug/libs/signed_integers/src/i64.sw":162
Result: FAILED. 1 passed. 1 failed. Finished in 163.583µs.