2

I'm writing a UI page object model testing project in TypeScript and I'm trying my best to avoid hideous nested imports.

I found the paths object for the .tsconfig file that fixes the issue in .ts files, but, because I compile into an outDir, my compiled .js files cannot resolve the module path.

I develop in a model directory and compile into a src directory.

I've set up my .tsconfig file as follows

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": true,
    "noImplicitAny": false,
    "outDir": "src/",
    "baseUrl": "./model/",
    "paths": {
      "general.utils": [ "lib/general/general.utils" ],
      "pretest.setup": [ "lib/pretest.setup/pretest.setup" ],
      "tpapi": [ "lib/tpapi/tpapi" ],
      "*": [ "*" ]
    }
  },
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

That paths allows me to

import { chooseRandomValueFrom } from 'general.utils';

from a .ts file that is in model, but won't resolve in a .js file in src.

This is an npm type project that is run with npm test. That command executes a script that compiles, runs tests from, and deletes the src directory.

#!/bin/bash

npm run compile:ts
node_modules/webdriverio/bin/wdio
npm run clean:ts
2
  • I have the same problem, I have been struggling with it for hours. Can not find information about this topic. Commented Dec 12, 2016 at 21:56
  • @CarlosLanderas I added my solution below. I wouldn't call it solved, but I've gotten used to it. Commented Dec 12, 2016 at 22:22

1 Answer 1

1

The best way I found to deal with this is to have a collector/exporter file that exports everything from the files that it collects from.

For examples if I have 5 files that have different types of utilities in them, I would name them appropriately, then collect them all in a collector/exporter file called utilities.

In the utilities file, it's just lines that look like

export * from 'path/to/file1
export * from 'path/to/file2
export * from 'path/to/file3

Then I can import only from the collector/exporter file.

It's not great, but it's a little better than doing nothing at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.