avajs / typescript

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

Surprising rewritePaths needed #27

Closed dsernst closed 4 years ago

dsernst commented 4 years ago

My source files are organized like this:

./src/
    example.ts
    second-example.ts
    tests/
      test-example.ts
      test-second-example.ts

My tsconfig.json looks like this:

{
  "compilerOptions": {
    "outDir": "./build",
    "allowJs": true,
    "target": "es5"
  },
  "include": ["./src/**/*"]
}

Which results in output like this

./build/
    src/
      example.js
      second-example.js
      tests/
        test-example.js
        test-second-example.js

I installed @ava/typescript and added rewritePaths to package.json as instructed in this library's readme, like this:

"ava": {
  "typescript": {
    "rewritePaths": {
      "src/": "build/"
    }
  }
}

But then running ava or npx ava src/tests/test-example.ts or other variations kept resulting in fatal errors like:

Uncaught exception in src/tests/test-example.ts

Error: Cannot find module '/Users/dsernst/Documents/myproject/build/tests/test-example.js'

It took me a while to figure out what was going on.

Note that it's missing the /src/ directory between /build/ and /tests/.

So after much tweaking, I was finally able to get it working with:

  "rewritePaths": {
    "src/": "build/src/"
  }

Maybe this is obvious, and I was just being stupid, but I'm putting this up here in part to help anyone else that comes across a similar issue.

version info:

@ava/typescript: 1.1.1 ava: 3.9.0 typescript: 3.9.5

node v12.16.3

novemberborn commented 4 years ago

Sorry you got bit by this. The third paragraph of the readme explains build/, but maybe that's too far away from the sample configuration?

In other words, say you have a test file at src/test.ts. You've configured TypeScript to output to build/. Using @ava/typescript you can run the build/test.js file using npx ava src/test.ts. AVA won't pick up any of the JavaScript files present in the build/ directory, unless they have a TypeScript counterpart in src/.

(Emphasis mine)

https://github.com/avajs/typescript/issues/2 is open so that hopefully maybe we can determine this automatically.