avajs / typescript

Test TypeScript projects using AVA.
MIT License
73 stars 16 forks source link

Import problems within test files #48

Closed dustsucker closed 1 year ago

dustsucker commented 1 year ago

First up, it's more a cry for help than a bug report, I think maybe it's a bug though.

What you're trying to do:

I just found an inactive NPM module that I wanted to use and tested it out, it worked but had some stuff that's missing for me. So I tried cloning it and getting it to run locally, so I can add the features I want. I bumped the dependence versions, got it to compile again, moved to ESLint from TSLint and tried running the tests. The tests didn't work, all local dependencies were not recognized, so I began to experiment. The tests are written in JS, the lib itself is written in ts. The test imported the ts files directly from source, so for example: '../src/docker' that gave me a:

 Uncaught exception in test/docker.js

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '~/node-docker-api/src/docker' imported from ~/node-docker-api/test/docker.js

so I changed it to '../src/docker.ts'':


  Uncaught exception in test/docker.js

  TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for ~/node-docker-api/src/docker.ts

then I thought maybe I need to import from the compiled lib folder, so I moved on to '../lib/docker'':

  Uncaught exception in test/docker.js

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '~/node-docker-api/lib/docker' imported from ~/node-docker-api/test/docker.js

than to '../lib/docker.js':

  Uncaught exception in test/docker.js

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '~/node-docker-api/lib/container' imported from ~/node-docker-api/lib/docker.js

and finally '../lib/docker.ts'

  Uncaught exception in test/docker.js

  Error [ERR_MODULE_NOT_FOUND]: Cannot find module '~/node-docker-api/lib/docker.ts' imported from ~/node-docker-api/test/docker.js

So it didn't work then I tried fixing the problem with AVA typescript with the configuration:

"ava": {
    "files": ["test/**/*.js"],
    "typescript": {
      "extensions": [
        "ts",
        "tsx"
      ],
      "rewritePaths": {
        "src/": "lib/"
      },
      "compile": "tsc"
    }
  },

Didn't work I changed the ts config module option from commonjs to ESNext didn't work either.

A now I don't know anything I can try to get it working. I just need it to import the classes from the lib, that's all.

Sample code:

It's not quite a minimal example, but it's quite small: https://github.com/dustsucker/node-docker-api any changes I made are reflected int that fork of the NPM module.

As for AVA version, I use 5.3.1.

I hope you can help me :)

best regards Titus Breede

novemberborn commented 1 year ago

AVA's main discussion forum is more appropriate for this question: https://github.com/avajs/ava/discussions