question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isObject helper fails in jest

See original GitHub issue

Describe the bug

fromKeyLike does not return an object (JWK). If you use the output of this function in the latest version of node you get the following error:

TypeError: JWK must be an object

To Reproduce

Use Node v15.14.0.

  const key = crypto.generateKeyPairSync("ec", {
    namedCurve: "secp256k1",
  });
  const privateJwk = await fromKeyLike(key.privateKey);
     // ^^^^^^^^^^ this is not an object
  const privateKey = await parseJwk(privateJwk, "ES256K");                                  
  // TypeError: JWK must be an object

Expected behaviour Get proper object so as to be able to use JWK.

Environment:

  • jose version: 3.11.4
  • Typescript v4.2.4
  • affected runtime is: Node.js v15.14.0

Additional context If anyone faces the same issue, there are two tricks that can save you:

  • Do not use privateJwk in the next function as input but {...privateJwk } or
  • After getting privateJwk set jwk.constructor = Object;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
maurolucccommented, Apr 13, 2021

Alright. I’ve updated the gist I shared with you. Now you should be able to clone it and run:

nvm use 15.14.0
yarn
jest

You’ll see that the test fails for this node version but not for older. Notice that to make this test pass in my project I fix it with the trick found in AuxTest.ts, swtiching how I return JWK:

  // returning directly privateJwk provoke getting TypeError: JWK must be an object error
  /* return {
    hexPrivateKey,
    jwk: privateJwk,
    hexPublicKey,
  }; */
  // The code below makes the trick
  return {
    hexPrivateKey,
    jwk: { ...privateJwk }, 
    hexPublicKey,
  }; 
1reaction
panvacommented, Apr 13, 2021

@maurolucc that is of no help unfortunately. I have no clue what the package.json looks like, how you execute this, nor what the typescript configuration is.

I’m looking for a reproduction, e.g. such as that i can clone the gist, run npm i and then execute whatever command that reproduces the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Matchers - Jest
In this code, .toBe(4) is the matcher. When Jest runs, it tracks all the failing matchers so that it can print out nice...
Read more >
How to assert data type with Jest - node.js - Stack Overflow
Use instanceof to prove whether the result variable is a Date Object or Not. Example: expect(result instanceof Date).
Read more >
Helper Functions for Assertions (Jest & Node.js)
isObjectPropertyEmpty.helper"); describe("isObjectPropertyEmpty helper function unit tests", () => { it("should validate incoming module ...
Read more >
Object.defineProperty() - JavaScript - MDN Web Docs
throws an error in strict mode ... trying to write into the non-writable property doesn't change it but doesn't throw an error either....
Read more >
Jest Expect - w3resource
If differences between properties do not help you to understand why a test failed, especially for large report, then you can move the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found