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.

Decorator descriptor is missing

See original GitHub issue

I have issue with using vue-jest along with typescript decorators. Decorator method is not receiving 3rd parameter called descriptor. TS docs: https://www.typescriptlang.org/docs/handbook/decorators.html

Runnable reproduction: https://github.com/budziam/vue-decorator-issue I have created two almost identical components.

  • Example1 - component created in .vue file
  • Example2 - component created in .ts file

Example 1 and 2 both have one decorated method. When you mount the component there is difference in 3rd parametr received by the decorator function. In Example1 case we get undefined while in Example2 we get appropriate object.

You can reproduce it by running

$ yarn test
yarn run v1.17.3
$ jest
 PASS  __tests__/mount.test.ts
  ✓ mount component from vue file (9ms)
  ✓ mount component from ts file (2ms)

  console.log src/decorator.ts:2
    { target:
       Example1 { constructor: [Function: Example1], action1: [Function] },
      key: 'action1',
      descriptor: undefined }

  console.log src/decorator.ts:2
    { target:
       Example2 { constructor: [Function: Example2], action2: [Function] },
      key: 'action2',
      descriptor:
       { value: [Function],
         writable: true,
         enumerable: true,
         configurable: true } }

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.094s
Ran all test suites.
Done in 1.58s.

My real-case scenario is using @Emit decorator from vue-property-decorator library. When I bundle my code using webpack, everything is OK. However, an error occurs when running tests

TypeError: Cannot read property 'value' of undefined

It happens because descriptor is undefined.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
vanhoofmaartencommented, Aug 8, 2019

Ok, because I wasn’t clearing the Jest cache, it generated false positives of which I provided you as a solution. My apologies.

vue-jest does pick up the correct tsConfig, so no need for the extra globals configuration.

However, I believe to get the correct decorator transformation, you need to set the compilerOptions.target to esnext.

I tested this approach multiple times with success by running the following commands.

jest --clearCache
jest

I would love to provide you with a more clear answer why this works, but I haven’t really figured out the how and why yet.

I believe it has something to do with the update on the TC39 Decorator proposal

Hopefully, this will solve your problem.

0reactions
vanhoofmaartencommented, Aug 9, 2019

My pleasure!

Read more comments on GitHub >

github_iconTop Results From Across the Web

target in javascript decorator is missing its properties
When I come to make the callback from the subscription, the target in the decorator has no properties, only methods. I'm probably doing ......
Read more >
Documentation - Decorators - TypeScript
The decorator is applied to the Property Descriptor for the method, and can be used to observe, modify, or replace a method definition....
Read more >
TypeScript method decorators in depth. Problems and solutions
The decorator is applied to the Property Descriptor for the method, and can be used to observe, modify, or replace a method definition.”....
Read more >
When a decorator meets a descriptor - Rebased Blog
We used the descriptor concept along with a partial helper to create a bound method. This way we have our missing positional argument...
Read more >
A practical guide to TypeScript decorators - LogRocket Blog
const myDecorator = (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => { // do something with your method } class Rocket { ...
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