Jest does not run static methods at the end of a TypeScript module

Viewed 30

I have these TypeScript files:

// Builders.ts
import {Registry} from "./Registry";

export interface IBuilder {
    build: () => void;
}

class NiceBuilder implements IBuilder {
    build(): void {
        console.log('Nice builder')
    }
}

class SuperBuilder implements IBuilder {
    build(): void {
        console.log('Super Builder')
    }
}

Registry.registerBuilder(new NiceBuilder());
Registry.registerBuilder(new SuperBuilder());
// Registry.ts
import {IBuilder} from "./Builders";

export class Registry {
    static builders: Array<IBuilder> = [];
    static registerBuilder(builder: IBuilder) {
        this.builders.push(builder);
    }

    static getBuilder(): IBuilder {
        return this.builders[0];
    }
}
// Builder.test.ts
import {Registry} from "../../../Registry";
import {IBuilder} from "../../../Builders";

describe('test builders', () => {
    it('test', () => {
        const builder: IBuilder = Registry.getBuilder();
        console.log(builder); // logs undefined why???????
    });
});

When I try to write a test the builder instance is undefined and I cannot figure out why.

I expect the import of Builders.ts will automatically run the registering at the end of the file, but it doesn't.

Any ideas?

0 Answers
Related