jest-chain icon indicating copy to clipboard operation
jest-chain copied to clipboard

TypeError: result.message is not a function

Open GuyLescalier opened this issue 4 years ago β€’ 5 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 ?

GuyLescalier avatar Jul 25 '21 11:07 GuyLescalier

Looking forward to the fix too. Thank you!

Jedliu avatar Aug 12 '21 01:08 Jedliu

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 πŸ™‚)

NordMagnus avatar Sep 01 '21 07:09 NordMagnus

Hi! πŸ‘‹

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

Today I used patch-package to patch [email protected] 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.

d0whc3r avatar Oct 29 '21 21:10 d0whc3r

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).

Vadorequest avatar Jun 12 '22 12:06 Vadorequest

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)

Vadorequest avatar Jun 12 '22 12:06 Vadorequest

Fixed in: https://www.npmjs.com/package/jest-chain/v/1.1.6

mattphillips avatar Sep 28 '22 16:09 mattphillips