Skip to content

Compiling class with decorator to ES6 wont load in karma test #3133

Closed
@derek-pavao

Description

@derek-pavao

I'm not sure if this is a bug, or I'm missing something.

I am compiling my TypeScript to ES6 and then using the babel preprocessor to run my tests in Karma. The class is as follows

import { BaseModel, notEmpty } from '../main';

export class TestModel extends BaseModel {


    @notEmpty('Test property can not be empty')
    public testProperty: string;


}

The above class compiles to

if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {

    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};

import { BaseModel, notEmpty } from '../main';
export class TestModel extends BaseModel {
}
__decorate([
    notEmpty('Test property can not be empty')
], TestModel.prototype, "testProperty");

This compiled code doesn't run. Unfortunately I don't get a helpful error either so I'm not exactly sure the problem. However if I mess with the compiled JavaScript a bit

// changing the first line from ...
if (typeof __decorate !== "function") __decorate = function (decorators, target, key, desc) {
    // code here
}

// to this

if (typeof __decorate !== "function") window.__decorate = function (decorators, target, key, desc) {
    // code here
}

// or to this, which would work in both browser and server environments
if (typeof __decorate !== "function") var __decorate = function (decorators, target, key, desc) {
    // code here
}

with either of those changes everything seems to work fine. Like I said though, I'm not sure if its a bug or I'm missing some small thing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDuplicateAn existing issue was already createdFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions