Skip to content

Using native decorators (EcmaScript) in TypeScript 5, the context is always undefined. #3140

@souzaadriano

Description

@souzaadriano

Describe the bug

When trying to use the new decorators, the context always comes as undefined and therefore the test fails when validating if the context.kind is the string 'class'.

Reproduction

Decorator and decorated class mock.ts

function exampleDecorator(ClassExample: any, context:ClassDecoratorContext):any {
    if(context.kind !== 'class') throw new Error('not a class to decorate')
    return function(...args: any[]):any {
        return new ClassExample(...args)
    }
}

@exampleDecorator
class Sut {}

spect.ts

import { describe, expect, it } from "vitest";
import { Sut } from './mock'

describe('module.factory.ts', () => {
        it('should be define a base module', () => {
            
            expect(Sut).toBeDefined()
    })
})

Vitest Output

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  src/core/module/factories/example.spec.ts [ src/core/module/factories/example.spec.ts ]
TypeError: Cannot read properties of undefined (reading 'kind')
 ❯ exampleDecorator src/core/module/factories/mock.ts:2:16
      1| function exampleDecorator(ClassExample: any, context:ClassDecoratorContext):any {
      2|     if(context.kind !== 'class') throw new Error('not a class to decorate')
       |                ^
      3|     return function(...args: any[]):any {
      4|         return new ClassExample(...args)
 ❯ __decorateClass src/core/module/factories/mock.ts:7:57
 ❯ src/core/module/factories/mock.ts:9:8
 ❯ src/core/module/factories/example.spec.ts:3:31

System Info

System:
    OS: Linux 6.2 Pop!_OS 22.04 LTS
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-11300H @ 3.10GHz
    Memory: 5.63 GB / 15.40 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.15.0/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.15.0/bin/npm
  Browsers:
    Chrome: 111.0.5563.146
    Firefox: 111.0.1
  npmPackages:
    vitest: ^0.29.8 => 0.29.8

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions