How to test Exports of a package?

Asked

Viewed 241 times

5

I have a file index.ts which is responsible for exporting all components as follows:

export * from './components/com1'
export * from './components/com2'
export * from './components/com3'
export * from './components/com4'

And an organization of folders

src
|---components
    |---com1
    |---com2
    |---com3
    |---com4
index.ts

My question is whether it is possible for me to verify that index.ts has the export of all components within the Components folder in it... In this case, it is possible to check whether all components have an export within index.ts through testing?

  • 2

    You want to check which members are being exported or the import statements itself? Try to edit your question to make it clearer.

  • @Luizfelipe edited the question, I could see if it gave an improved?

  • 1

    Hello dear Felipe, I understand the motivation of the question as a question of evaluation of the state of the program, I just do not think it feasible to do this in practice, force load all components, I think the components should be isolated and that is why there is import, calling only what is needed, doing an over of everything seems to be overloading the current process without need. Maybe I misunderstood something, could explain the purpose?

  • 1

    @Guilhermenascimento in the case, would not carry all the components. The context is that I am creating a package of components, hence my index serves to export the components, only, in one of the versions, I had forgotten to put the export in index and this component was not visible to those who installed the package, hence I want to create?

  • Check out if this does not help you: https://stackoverflow.com/a/40703602/3021610

1 answer

6


Following its logic, the only possible way (I believe) would be to "scan" all the components required for the application to run.

In architecture:

 src
|---components
    |---com1
      \--> export function com1();
    |---com2
      \--> export function com4();
    |---com3
      \--> export function com4();
    |---com4
      \--> export function com4();
index.ts

Inside index if you do

export * from './components/com1'
export * from './components/com2'
export * from './components/com3'
export * from './components/com4'

console.log(Object.keys(this));
console.log(hasAllExportFunctionExists(this));

function hasAllExportFunctionExists(objectStruct: any) {
    let exportedDeparaRequired = [
        "com1",
        "com2",
        "com3",
        "com4"
    ];
    return Object.keys(objectStruct).every(v => exportedDeparaRequired.includes(v));
}

With your tsconfig.json this way:

{
    "compilerOptions": {
      "target": "es6",
      "module": "commonjs",
      "lib": [
        "dom",
        "es6",
        "es2017",
        "esnext.asynciterable"
      ],
      "sourceMap": true,
      "outDir": "./build",
      "moduleResolution": "node",
      "removeComments": true,
      "noImplicitAny": true,
      "strictNullChecks": true,
      "strictFunctionTypes": true,
      "noImplicitThis": true,
      "noUnusedLocals": false,
      "noUnusedParameters": false,
      "noImplicitReturns": true,
      "noFallthroughCasesInSwitch": true,
      "allowSyntheticDefaultImports": true,
      "esModuleInterop": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "resolveJsonModule": true,
      "skipLibCheck": true,
      "baseUrl": "./"
    },
    "exclude": [
      "node_modules"
    ],
    "include": [
      "./src/**/*.ts"
    ]
  }

the tsc will compile to something like this:

"use strict";
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./components/com1"));
__export(require("./components/com2"));
__export(require("./components/com3"));
__export(require("./components/com4"));
console.log(Object.keys(this));
hasAllExportFunctionExists(this);
function hasAllExportFunctionExists(objectStruct) {
    let exportedDeparaRequired = [
        "com1",
        "com2",
        "com3",
        "com4"
    ];
    return Object.keys(objectStruct).every(v => exportedDeparaRequired.includes(v));
}

What will return in "Node index.js"

['com1', 'com2', 'com3', 'com4' ]
true

Browser other questions tagged

You are not signed in. Login or sign up in order to post.