Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Add error message match to shallRevert assertion (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink authored Jul 5, 2022
1 parent ba2de53 commit 78944c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-pillows-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@onflow/flow-js-testing": minor
---

Add optional error message match to shallRevert Jest assertion
12 changes: 7 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,17 @@ describe("interactions - sendTransaction", () => {
});
```

### shallRevert(ix)
## shallRevert(ix, message)

Ensure interaction throws an error. You might want to use this to test incorrect inputs.
Ensure interaction throws an error. Can test for specific error messages or catch any error message if `message` is not provided.
Returns Promise, which contains result, when resolved.

#### Arguments

| Name | Type | Description |
| ---- | --------------------------- | ---------------------------------------------------- |
| `ix` | [Interaction](#interaction) | transaction, either in form of a Promise or function |
| Name | Type | Description |
| ------------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `ix` | [Interaction](api.md#interaction) | transaction, either in form of a Promise or function |
| `message` **(optional)** | `string` or `RegExp` | expected error message provided as either a string equality or regular expression to match, matches any error by default |

#### Returns

Expand Down
11 changes: 6 additions & 5 deletions docs/jest-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,17 @@ describe("interactions - sendTransaction", () => {
});
```

## shallRevert(ix)
## shallRevert(ix, message)

Ensure interaction throws an error. Use this to test incorrect inputs.
Ensure interaction throws an error. Can test for specific error messages or catch any error message if `message` is not provided.
Returns Promise, which contains result, when resolved.

#### Arguments

| Name | Type | Description |
| ---- | --------------------------------- | ---------------------------------------------------- |
| `ix` | [Interaction](api.md#interaction) | transaction, either in form of a Promise or function |
| Name | Type | Description |
| ------------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `ix` | [Interaction](api.md#interaction) | transaction, either in form of a Promise or function |
| `message` **(optional)** | `string` or `RegExp` | expected error message provided as either a string equality or regular expression to match, matches any error by default |

#### Returns

Expand Down
19 changes: 17 additions & 2 deletions src/jest-asserts.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,29 @@ export const shallResolve = async (ix) => {
/**
* Ensure interaction throws an error.
* @param {function | Promise} ix - Promise or function to wrap
* @param {string | RegExp} [message] - Expected error message provided as either a string equality or regular expression
* @returns Promise<*> - result of interaction
* */
export const shallRevert = async (ix) => {
export const shallRevert = async (ix, message) => {
const wrappedInteraction = promise(ix);
const response = await wrappedInteraction;
const [result, error] = response;

await expect(result).toBe(null);
await expect(error).not.toBe(null);

if (message) {
const errorMessage = error
.toString()
.match(/^error: (panic)|(assertion failed): ([^\r\n]*)$/m)
?.at(3);
if (message instanceof RegExp) {
await expect(errorMessage).toMatch(message);
} else {
await expect(errorMessage).toBe(message);
}
} else {
await expect(error).not.toBe(null);
}

return response;
};
Expand Down

0 comments on commit 78944c1

Please sign in to comment.