Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metadata seemingly erased when using multiple files with karma,mocha,webpack #80

Closed
grofit opened this issue Sep 1, 2017 · 2 comments
Closed

Comments

@grofit
Copy link

grofit commented Sep 1, 2017

I have a helper class which basically wraps the dealing with metadata:

import "reflect-metadata";
import {Ruleset} from "treacherous";

export const rulesetMetadataKey = "ruleset";

export function getRulesetFromMetadata(target: Object | any) : Ruleset  { 
    const targetUsed = target.prototype || target;
    if(Reflect.hasMetadata(rulesetMetadataKey, targetUsed))
    { return Reflect.getMetadata(rulesetMetadataKey, targetUsed); }
    else
    { return new Ruleset(); }
}

export function updateRulesetMetadata(target: Object | any, ruleset: Ruleset): void {
    Reflect.defineMetadata(rulesetMetadataKey, ruleset, targetUsed);
}

A few files in the lib use this, however when I go to test the decorators I have made everything works fine when I made the first file, so I have a test file which tests one of the decorators. As I say this all works fine, everything is as expected, but then when I went to add another test file for a different decorator just the mere fact that I use the decorator in another file, it causes the first file with tests to blow up... so for example:

test1.ts

import {SomeDecorator} from "../some/relative/lib"; // this uses the above helper

class SomeModel
{
   @SomeDecorator
    public someProp;
}

// some tests that use that model, and all pass

test2.ts

import {SomeOtherDecorator} from "../some/relative/lib"; // this uses the above helper

class SomeModel
{
   @SomeOtherDecorator
    public someProp;
}

Just having that decorator in there causes the test1.ts file tests to stop passing, as it seems to remove all metadata, however if I were to comment out the SomeModel it works fine and all tests in the first file pass. I am not sure if webpack will be removing the import when there is nothing that uses it (using webpack 1.* atm).

So is this because every time it is included it deletes the existing metadata? I tried putting a console.log("Hello") in the helper file after the import just to see how many times its run, and it seems to get run once per file.

If it helps here is an example decorator which would be used, and is the one which is used in both files:

import {Ruleset, RuleLink, IModelResolver} from "treacherous";
import {getRulesetFromMetadata, updateRulesetMetadata} from "../helpers/metadata-helper";

export function withRule(ruleName: string, ruleOptions?: any, 
    appliesIf?: (modelResolver: IModelResolver, value: any, ruleOptions?: any) => boolean | boolean, 
    messageOverride?: (value: any, ruleOptions?: any) => string | string) {
    
    return function(target: Object, propertyKey: string) : void
    {
        const existingRuleset = getRulesetFromMetadata(target);
        const ruleLink = new RuleLink(ruleName, ruleOptions);

        if(typeof appliesIf !== "undefined")
        { ruleLink.appliesIf = appliesIf; }

        if(typeof messageOverride !== "undefined")
        { ruleLink.messageOverride = messageOverride; }

        existingRuleset.addRule(propertyKey, ruleLink);        
        updateRulesetMetadata(target, existingRuleset);
    }
}
@westy92
Copy link

westy92 commented Jan 16, 2018

Thank you!
Should we add a test as well to help prevent future regressions?

@rbuckton
Copy link
Owner

Sure, added in c2dbe1d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants