vscode-jest-runner icon indicating copy to clipboard operation
vscode-jest-runner copied to clipboard

(typescript) Decorated parameters result in jest-runner erroring

Open jbjhjm opened this issue 2 years ago • 3 comments

As soon as a test file contains a param decorator like e.g.

class SimpleTestController {
	handlerMethod(@Body() xxx) {
		return;
	}
}

jest-runner will fail. There already was a issue report some time ago: https://github.com/firsttris/vscode-jest-runner/issues/225

Unfortunately the issue remains. Console reported the following error:

jest-editor-support parser returned error SyntaxError: Decorators cannot be used to decorate parameters. (18:15)
    at mt._raise (c:\Users\Jannik\.vscode\extensions\firsttris.vscode-jest-runner-0.4.48\node_modules\@babel\parser\lib\index.js:479:22)

In the log paths one can see babel parser is generating this error. It seems some of the used dependencies try to transpile typescript based on babel.

I think this is whats happening:

  • https://github.com/firsttris/vscode-jest-runner/blob/master/src/JestRunnerCodeLensProvider.ts#L58
  • JestRunnerCodeLensProvider parses source files using jest-editor-support.
  • jest-editor-support transpiles files using babel 7 so it does not fully support typescript syntax

So if jest-editor-support is not going to implement full typescript / tsc support, typescript files should be compiled to ECMAscript before being passed to the babel-based transpiler.

jbjhjm avatar Jun 28 '22 11:06 jbjhjm

thx for your report.

my understanding is babel 7 parser fully supports typescript syntax. We even migrated from typescript to babel parser in jest-editor-support. for whatever reasons. but decorators are experimental (even in typescript compiler). jest-editor-support would need to add the corresponding plugin to make decorators work. probably the plugin is missing.

since you already figured out the issue originates in jest-editor-support, we need to create the issue there to get it fixed: https://github.com/jest-community/jest-editor-support/issues

best regards Tristan

firsttris avatar Jun 28 '22 11:06 firsttris

Thx for blazingly fast reply @firsttris !

Who knows if typescript decorators will ever be marked stable 🤷‍♂️ The babel documentation on decorators mentions only class and method level decorators: https://babeljs.io/docs/en/babel-plugin-proposal-decorators And there is a 3rd party babel 7 plugin stating to add parameter-level decorators: https://github.com/WarnerHooh/babel-plugin-parameter-decorator Combining those two infos I'm kinda sure there's no official support for param decorators in Babel 7.

jest-editor-support documentation seems pretty sparse so I cannot see if there is any way to load the plugin without changing the library source code. I'll open a issue over on their repo.

jbjhjm avatar Jun 28 '22 14:06 jbjhjm

i think its just a matter of adding the plugin here. https://github.com/jest-community/jest-editor-support/blob/master/src/parsers/helper.ts#L11

i refactored this from typerscript to bable_parser at some point

and of course, they need to release a new version, we need to increase the dependency version...

firsttris avatar Oct 14 '22 08:10 firsttris