LimeChain / matchstick

🔥 Unit testing framework for Subgraph development on The Graph protocol. ⚙️
MIT License
207 stars 17 forks source link

[Suggestion] Add verbose to assert fails #395

Closed OscBacon closed 11 months ago

OscBacon commented 1 year ago

Hi, first off, great testing library! It would be nice to have more verbose on assert fails. As of right now, I have to add log statements to find out which assert exactly failed.

For example, on a series assert.entityCount, this message is not helpful enough:

X𝖷 (assert.equals) Expected value was '1' but actual value was '0'

(and the backtrace doesn't say much)

Secondly, having assert functions with a message field could help too, a la nodejs assert: https://nodejs.org/api/assert.html

If I understand correctly, AS doesn't allow for function overloading or optional arguments. One solution could be to have an assert namespace, and an assertMessage namespace with the same functions, but that throw a custom message when the assert fails

dimitrovmaksim commented 1 year ago

Hey @OscBacon, matchstick version 0.6.0-beta3 and matchstick-as version 0.6.0-beta.0 are now available, which add optional 3rd argument message to each assert function. You can try them now. matchstick 0.6.0-beta3 should also be backwards compatible with matchstick-as 0.5.2. Keep in mind also that this is a beta and also includes fixes for derived fields for entities with Bytes Id's and log.critical interrupting the tests execution, so there could be some bugs and issues in general.

import { assert, describe, test } from "matchstick-as";
import { ethereum, Address } from "@graphprotocol/graph-ts";

describe("Asserts", () => {
  test("assert.equals", () => {
    assert.equals(ethereum.Value.fromI32(1), ethereum.Value.fromI32(2), "Value should equal 1");
  });

  test("assert.I32Equals", () => {
    assert.i32Equals(1, 2, "I32 should equal 1");
  });

  test("assert.AddressEquals", () => {
    assert.addressEquals(
      Address.zero(),
      Address.fromString("0x0000000000000000000000000000000000000001"),
      "Address should equal zero address"
    );
  });
});
yarn test -rv 0.6.0-beta3 asserts

___  ___      _       _         _   _      _
|  \/  |     | |     | |       | | (_)    | |
| .  . | __ _| |_ ___| |__  ___| |_ _  ___| | __
| |\/| |/ _` | __/ __| '_ \/ __| __| |/ __| |/ /
| |  | | (_| | || (__| | | \__ \ |_| | (__|   <
\_|  |_/\__,_|\__\___|_| |_|___/\__|_|\___|_|\_\

Compiling...

💬 Compiling asserts...

Igniting tests 🔥

asserts
--------------------------------------------------
  Asserts:
    𝖷 assert.equals - 0.565ms
    𝖷 assert.I32Equals - 0.107ms
    𝖷 assert.AddressEquals - 0.135ms

Failed tests:

asserts
  assert.equals
        𝖷 Value should equal 1
        🛠  Mapping aborted at ~lib/matchstick-as/assert.ts, line 38, column 7, with message: assert.equals Assertion Error
wasm backtrace:
    0: 0x1aee - <unknown>!start:tests/asserts.test~anonymous|0~anonymous|0

  assert.I32Equals
        𝖷 I32 should equal 1
        🛠  Mapping aborted at ~lib/matchstick-as/assert.ts, line 94, column 7, with message: assert.i32Equals Assertion Error
wasm backtrace:
    0: 0x1b37 - <unknown>!start:tests/asserts.test~anonymous|0~anonymous|1

  assert.AddressEquals
        𝖷 Address should equal zero address
        🛠  Mapping aborted at ~lib/matchstick-as/assert.ts, line 66, column 7, with message: assert.addressEquals Assertion Error
wasm backtrace:
    0: 0x1bfd - <unknown>!start:tests/asserts.test~anonymous|0~anonymous|2

3 failed, 0 passed, 3 total
OscBacon commented 1 year ago

Looks promising!

dimitrovmaksim commented 11 months ago

Should be resolved by https://github.com/LimeChain/matchstick/pull/408, can be tested with https://github.com/LimeChain/matchstick/releases/tag/0.6.0-rc.3