# Inconsistent VM behavior in sway unit tests

**URL:** <https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742>\
**Category:** Sway\
**Created:** [February 1, 2023, 12:38am UTC](https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742 "2023-02-01T00:38:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsproule](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/rsproule/32/987_2.png) [@rsproule](https://forum.fuel.network/u/rsproule)\
**Post date:** [February 1, 2023, 12:38am UTC](https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742/1 "2023-02-01T00:38:32Z")

</div>

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](https://github.com/FuelLabs/sway-libs/tree/f33fc23bdfd4b57e6be31c43de7181899bf0d28c/libs/signed_integers).

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](https://github.com/FuelLabs/sway-libs/compare/master...rsproule:sway-libs-demo-unit-test-bug:master) demonstrates this in a forked repo.

```auto
#[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:

```auto
  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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![mindtree](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mindtree/32/5_2.png) [@mindtree](https://forum.fuel.network/u/mindtree)\
**Post date:** [February 1, 2023, 1:26am UTC](https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742/2 "2023-02-01T01:26:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mindtree](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/mindtree/32/5_2.png) [@mindtree](https://forum.fuel.network/u/mindtree)\
**Post date:** [February 1, 2023, 1:38am UTC](https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742/3 "2023-02-01T01:38:14Z")

</div>

Oh also - can you try running `forc test` with the new raw log output? E.g.

```plaintext
forc test --log --pretty-print

```

This will dump the raw logs from the receipt that might help to debug what’s going on.

---

<div class="post-metadata">

**Author:** ![rsproule](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/rsproule/32/987_2.png) [@rsproule](https://forum.fuel.network/u/rsproule)\
**Post date:** [February 1, 2023, 5:23am UTC](https://forum.fuel.network/t/inconsistent-vm-behavior-in-sway-unit-tests/1742/4 "2023-02-01T05:23:36Z")

</div>

switched back over to nightly

```auto
❯ 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:

```auto
❯ 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.

```
