mattphillips / jest-chain

Chain Jest matchers together to create one powerful assertion πŸƒβ›“

Home Page:https://www.npmjs.com/package/jest-chain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: result.message is not a function

GuyLescalier opened this issue Β· comments

Bug

  • package version: 1.1.15
  • node version: 14.17.3
  • npm (or yarn) version: 7.19.1

What you did:
I wrote some invalid expectation: expect("string").toBe(12);

What happened (please provide anything you think will help):
TypeError: result.message is not a function

Hi,
From what I see in chain.js, the JestAssertionError constructor considers result.message as a function when its actually a string.
At some point, Jest must have changed and started calling the message factory to throw the message string instead.
So I replaced this line:
super(result.message());
For that one:
super(typeof result.message === "function" ? result.message() : result.message);
And it fixes the issue without introducing a breaking change.
Should I propose a PR ?

commented

Looking forward to the fix too. Thank you!

Is it reasonable to expect this being added, or the PR to be accepted, any time soon considering the lack of activity in this project the last year or so? (Of course that could also be because no changes has been needed πŸ™‚)

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Today I used patch-package to patch jest-chain@1.1.5 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/jest-chain/dist/chain.js b/node_modules/jest-chain/dist/chain.js
index 4a7b85b..a6edb0f 100644
--- a/node_modules/jest-chain/dist/chain.js
+++ b/node_modules/jest-chain/dist/chain.js
@@ -7,7 +7,7 @@ exports.default = void 0;
 
 class JestAssertionError extends Error {
   constructor(result, callsite) {
-    super(result.message());
+    super(typeof result.message === "function" ? result.message() : result.message);
     this.matcherResult = result;
 
     if (Error.captureStackTrace) {

This issue body was partially generated by patch-package.

Why hasn't this been addressed? The fix look good to me at first glance.

I updated a few package versions and got caught up with this error, I have to rollback to an older version (or use patch-package as shown above).

Had to go from :

    "jest": "28.0.3",
    "jest-expect-message": "1.0.2",
    "jest-extended": "2.0.0",
    "jest-runner-groups": "2.2.0",
    "jest-to-match-shape-of": "1.3.2",
    "ts-jest": "28.0.3",

To:

    "jest": "26.6.3",
    "jest-expect-message": "1.0.2",
    "jest-extended": "0.11.5",
    "jest-runner-groups": "2.0.1",
    "jest-to-match-shape-of": "1.3.1",
    "ts-jest": "26.5.6",

Which fixed the issue. (my tests are failing properly now)