Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
osdiab opened this issue Nov 20, 2021 · 3 comments
Closed

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

osdiab opened this issue Nov 20, 2021 · 3 comments
Labels
question Further information is requested

Comments

@osdiab
Copy link

osdiab commented Nov 20, 2021

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!

@osdiab osdiab added the question Further information is requested label Nov 20, 2021
@novemberborn
Copy link
Member

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
Copy link
Author

osdiab commented Nov 29, 2021

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
Copy link
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants