To import namespace from other files as an external module while maintaining it

Asked 1 years ago, Updated 1 years ago, 83 views

  • Environment

    • Node.js:8.9.4
    • typescript:2.9.2
  • What do you want to do

    • I want to test classes in the namespace with jest or mocha
    • tsconfig.json target is ES5
    • I don't want to change the source side
  • Current State Example

    //src/main.ts
    namespace base {
        export class Foo {
            constructor() { console.log("Called Foo Constructor!");}
            public answerToTheUltimateQuestion(): number {return42;}
            public callHello() {return new Bar().say()}
        }
    }
    
    // src/Hello.ts
    namespace base {
        export class Bar {
            constructor() { console.log("Called Bar Constructor!";};
            say():string {return "hello!";}
            getFoo() {return new base.Foo();}
        }
    }
    
    // \__tests__/main.test.ts
    const foo = new base.Foo(); // ReferenceError: base is not defined
    
    test("someMethod",()=>{
        expect(foo.answerToTheUltimateQuestion()) .toBe(42);
    });
    

Environment

  • Node.js:8.9.4
  • typescript:2.9.2

What you want to do

  • I want to test classes in the namespace with jest or mocha
  • tsconfig.json target is ES5
  • I don't want to change the source side

Current State Example

//src/main.ts
namespace base {
    export class Foo {
        constructor() { console.log("Called Foo Constructor!");}
        public answerToTheUltimateQuestion(): number {return42;}
        public callHello() {return new Bar().say()}
    }
}

// src/Hello.ts
namespace base {
    export class Bar {
        constructor() { console.log("Called Bar Constructor!";};
        say():string {return "hello!";}
        getFoo() {return new base.Foo();}
    }
}

// \__tests__/main.test.ts
const foo = new base.Foo(); // ReferenceError: base is not defined

test("someMethod",()=>{
    expect(foo.answerToTheUltimateQuestion()) .toBe(42);
});

I know it's best to rewrite it in a modular format, but it has a large number of files and
It's hard to change, so I'll do my best to test it. I want to re-write it after securing a situation where I can understand even if it is broken.
Is it possible to change the d.ts file, change the target of tconfig.json, or resolve it by any other means?

node.js typescript

2022-09-30 17:26

1 Answers

There was a similar question in our stackoverflow.However, our questions have not been solved.

How would I test typecode that uses names in Node.js?

However, there was an introduction to using the TypeScript --outFile option.

//<reference/> with the dependent file path in the test code as follows:
outFile option with main.test.js and so on.
A single test code and dependency code can be combined to correct the test code and
You may be able to do this with just the ingenuity of the build tool.

//\_tests__/main.test.ts
/// <reference path="main.ts"/>
/// <reference path="Hello.ts"/>
const foo = new base.Foo(); // ReferenceError: base is not defined

test("someMethod",()=>{
    expect(foo.answerToTheUltimateQuestion()) .toBe(42);
});


2022-09-30 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.