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

decode does not allow stubbing for unit test with libraries such as sinon #870

Open
carrolp opened this issue Jan 4, 2023 · 5 comments
Open

Comments

@carrolp
Copy link

carrolp commented Jan 4, 2023

Description

Changes to how decode is defined in 9.0.0 (15a1bc4) prevent it from being altered for unit tests by tools such as sinon, or even direct alteration such as

jwt.decode = function() { return( 'teststring' ); };

As a result errors such as TypeError: Cannot redefine property: decode are thrown in these scenarios.

Other JWT libraries such as jose have added configurable: true to allow this scenario to succeed, which should probably be done here too:

Reproduction

Test code such as

jwt.decode = function() { return( 'teststring' ); };

results in

TypeError: Cannot redefine property

Environment

  • Version of this library used: 9.0.0
  • Version of the platform or framework used, if applicable: n/a
  • Other relevant versions (language, server software, OS, browser): n/a
  • Other modules/plugins/libraries that might be involved: n/a
@frankyhun
Copy link

Until it gets fixed, you can alter it in the test code as:

Object.defineProperty(jwt, 'decode', {
  writeable: true,
  value: () => {return 'some token';},
});

@VRanga000
Copy link

Until it gets fixed, you can alter it in the test code as:

Object.defineProperty(jwt, 'decode', {
  writeable: true,
  value: () => {return 'some token';},
});

This does not work, because you cannot redefine a property:

    TypeError: Cannot redefine property: decode
        at Function.defineProperty (<anonymous>)

    > 69 |     Object.defineProperty(jsonwebtoken, 'decode', {
         |            ^
      70 |       writable: true,
      71 |       value: () => {
      72 |         return 'some token';

@ShilpaJalaja
Copy link

Experiencing the same error

@jsstrellix
Copy link

Same here.

@tiffanyiong
Copy link

The decode() method is not recommended in version 9.0.0
we can use verify() instead!
And if your node version is below v. 12.19, it won't work as well even though you use the verify() method

jakelacey2012 added a commit to jakelacey2012/node-jsonwebtoken that referenced this issue Feb 13, 2023
jakelacey2012 added a commit that referenced this issue Apr 3, 2023
* refactor: revert 15a1bc4 to to solve #875, #870, and possibly also #876
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

6 participants