avajs / typescript

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

A bit more guidance on how to set things up properly #40

Closed osdiab closed 3 years ago

osdiab commented 3 years ago

I'm kinda confused how I'm supposed to properly use this package alongside baseUrl, I think I'm holding it wrong lol.

So I upgraded AVA to ava@next (4.0.0-rc.1) and installed the latest @ava/typescript (3.0.1). My project uses TypeScript Project References, and dumps files into a .build-tsc/ directory, so I set compile: false, and edited my test script to compile before running ava.

Project has a src/ directory at the root containing code, and a tests/ directory containing a mix of tests (file extension *.test.tsx?) and utilities for tests (just ending in .ts). I set baseUrl in my tsconfig to the package root, and before trying @ava/typescript, I was just setting NODE_PATH to .build-tsc so that Node could find all the proper files.

That's fine, but the main downsides are A) all the errors are in the compiled JS instead of the TS, making it harder to understand where the errors come from; and B) pretty sure it just doesn't work with JSX at the moment (but that's less pressing since I'm mostly testing backend business logic).

So trying out @ava/typescript, I set my config as follows:

// package.json
  "scripts": {
    "test": "tsc --build tests && ava",
    // ...
  },
  "ava": {
    "typescript": {
      "rewritePaths": {
        "src/": ".build-tsc/src/",
        "tests/": ".build-tsc/tests/"
      },
      "compile": false,
      "extensions": ["ts", "tsx"]
    },
    "files": [
      "./tests/**/*.test.ts?(x)"
    ]
  }

This evidently is not correct, as I get errors like the following whenever a test tries to import anything in src:

  Uncaught exception in tests/utility/markup-description.test.ts

  Error: Cannot find module 'src/utility/array'
  Require stack:
  - /Users/omar/code/spinach/web-app/.build-tsc/tests/utility/markup-description.test.js
  - /Users/omar/code/spinach/web-app/node_modules/ava/lib/worker/base.js

And then if I set NODE_PATH again to .build-tsc, then it works, but once again it's no different in that error line numbers are in the original JS files, not the TS ones.

So yeah I'm not totally sure how to A) make TypeScript baseUrl (and relatedly paths which I'm not using right now) work, and B) how I should be doing things to get the line numbers to be those of the TypeScript files. I think some more detailed examples/documentation for these kinds of use cases, or at least a disclaimer that this isn't supported, would be helpful for me!

novemberborn commented 3 years ago

before trying @ava/typescript, I was just setting NODE_PATH to .build-tsc so that Node could find all the proper files.

Is your code still assuming this is the case? Unless you configure TypeScript to map src/, the compiled JavaScript code is still trying to import src/ directly and that won't work. @ava/typescript only rewrites the initial import, so you can select the TypeScript files rather than the build output.

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

osdiab commented 2 years ago

Ah, I thought that @ava/typescript would manage that because it had the rewritePaths member - so I guess what exactly does that do, why does it it stop at the initial import? Makes sense though, guess would need to include the tsconfig-paths plugin or keep including the custom NODE_PATH to make it work as it was before.

novemberborn commented 2 years ago

@ava/typescript lets you run npx ava src/test.ts (or select src/ files in the config), plus compile your project for you so you don't forget.