Decorator descriptor is missing
See original GitHub issueI 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:
- Created 4 years ago
- Comments:5
Top GitHub Comments
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
toesnext
.I tested this approach multiple times with success by running the following commands.
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.
My pleasure!