Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: toMatchExitCode #11

Open
webdeb opened this issue Apr 18, 2024 · 1 comment
Open

Feature Request: toMatchExitCode #11

webdeb opened this issue Apr 18, 2024 · 1 comment

Comments

@webdeb
Copy link

webdeb commented Apr 18, 2024

To properly test contracts its necessary to check the exact exitCode thrown by the contract.
Jest only allows to match the Error message. https://jestjs.io/docs/expect#tothrowerror

@webdeb
Copy link
Author

webdeb commented Apr 18, 2024

Created a simple implementation, would be nice if this comes into the official release, please feel free to decide about the function name.

import jestGlobal from '@jest/globals';

jestGlobal.expect.extend({
  toMatchExitCode: async (received: any, expected: number) => {
      const result = {
          message: () => `expected exitCode: ${expected}`,
          pass: false,
      }

      try {
          await received();
          result.message = () => `expected to throw Error with exitCode: ${expected}, no Error was thrown instead`
      } catch(e: any) {
          result.pass = e.exitCode == expected;
      }

      return result
  }
});

declare global {
  export namespace jest {
      interface Matchers<R> {
          toMatchExitCode(code: number): R
      }
  }
}

Usage:

expect(async () => await someContract.sendSmt()).toMatchExitCode(123);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant