vitest-dev / vitest-dev/vitest

Regression in 4.1.0: (method) decorators not accepted in class defined in test files

Open
#9,876 10 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

upstream
Dominant language
TypeScript
Stars
17.1k
Forks
2k
Avg merge
1d 22h
Merged PRs (30d)
94

Description

Describe the bug

(clean version of https://github.com/vitest-dev/vitest/issues/9873, sorry for the mess)

I'm trying to test a method decorator, copying example from TypeScript documentation (inside a test file, the actual case is too test that decorator by defining a class with a method using it).

function loggedMethod(originalMethod: any, _context: any) {
  function replacementMethod(this: any, ...args: any[]) {
    console.log("LOG: Entering method.");
    const result = originalMethod.call(this, ...args);
    console.log("LOG: Exiting method.");
    return result;
  }

  return replacementMethod;
}

class Person {
  name: string;
  constructor(name: string) {
    this.name = name;
  }

  @loggedMethod
  greet() {
    console.log(`Hello, my name is ${this.name}.`);
  }
}

With 4.0.18, this worked fine. With 4.1.0, I get error

FAIL  packages/alfa-cache/test/cache.spec.ts [ packages/alfa-cache/test/cache.spec.ts ]
SyntaxError: Invalid or unexpected token

When commenting out the @loggedMethod line, the tests run fine.

I guess the "unexpected token" is the @ symbol. Couldn't find anything obvious in the changelog that this should now break, so I guess it is a regression (and also, the error message is pretty bad, not giving at least the line where the problem is).

Reproduction

https://github.com/Jym77/vitest-decorator

The Vitest 4.0.18 workflow runs fine.
The Vitest 4.1.0 workflow fails with the "unexpected token" error. The test-no-decorator file passes, but the test-decorator one doesn't. The only difference between them being the inclusion of the decorator (in a class that is not even used, so likely a parsing error?)

System Info
System:
    OS: Linux 6.6 Ubuntu 24.04.4 LTS 24.04.4 LTS (Noble Numbat)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 14.54 GB / 15.48 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 24.14.0 - /home/jym/.nvm/versions/node/v24.14.0/bin/node
    Yarn: 4.13.0 - /home/jym/.nvm/versions/node/v24.14.0/bin/yarn
    npm: 11.9.0 - /home/jym/.nvm/versions/node/v24.14.0/bin/npm
  npmPackages:
    @vitest/coverage-v8: ^4.1.0 => 4.1.0
    vitest: ^4.1.0 => 4.1.0


(node project, not using any browser)
Used Package Manager

yarn

Validations

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction's test-decorator and test-no-decorator files, comparing their behavior under Vitest 4.0.18 and 4.1.0. Trace how the test file containing the method decorator is parsed or transformed, then verify that the decorator test passes under 4.1.0 while the existing no-decorator test remains passing.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.