avajs / typescript

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

Ava watch does not work with pre-compile option #33

Open StarpTech opened 3 years ago

StarpTech commented 3 years ago

Please provide details about:

Ava watch mode with this package.

{
  "ava": {
    "files": [
      "src/**/*.test.ts"
    ],
    "typescript": {
      "rewritePaths": {
        "src/": "build/"
      },
      "compile": "tsc"
    }
  }
}

ava --watch

If I change a source file or test file the runner is not restarted. If I do the same with ts-node/register it works.

Ava runner should rerun on file change.

ava: 3.15.0

novemberborn commented 3 years ago

Could you run AVA with DEBUG=ava:watcher npx ava --watch? That should print out any files that AVA detects as having changed, and what it decided to do in response.

StarpTech commented 3 years ago
ava:watcher Detected change of src/registry/federation/deactivate-schema.test.ts +0ms
ava:watcher Ignoring changed file /home/starptech/repositories/starptech/graphql-registry/src/registry/federation/deactivate-schema.test.ts +101ms

The test isn't rerun.

novemberborn commented 3 years ago

I don't think I've noticed this before myself. Does it work when you rewrite from src/ to build/?

This line looks suspect:

https://github.com/avajs/typescript/blob/b6bc75786716cc54040582b1f42e716046347ad8/index.js#L119

The log output gives an absolute path, but that logic assumes a relative path.

If we can figure out what configuration does work then we can make sure alternative configurations also work, or fail to run the tests with a useful error, instead of having the watcher not working.

StarpTech commented 3 years ago

Does it work when you rewrite from src/ to build/?

no, same result.

novemberborn commented 3 years ago

Bummer!

This sounds like a bug, though I haven't encountered it myself. If you could share a reproduction that would make it easier to debug.

StarpTech commented 3 years ago

The repository is public https://github.com/StarpTech/graphql-registry. Just run npm run test:watch.

StarpTech commented 3 years ago

I think I found the bug. As you already mentioned the conditions in https://github.com/avajs/typescript/blob/b6bc75786716cc54040582b1f42e716046347ad8/index.js#L119 looks odd. The results must be reversed.

    return !rewritePaths.find(([from]) => filePath.startsWith(from));

After that the test is rerun but then I run into

  ava:watcher /home/starptech/repositories/starptech/graphql-registry/build/registry/maintanance/garbage-collect.test.js is a dependency of /home/starptech/repositories/starptech/graphql-registry/src/registry/maintanance/garbage-collect.test.ts +2s
  ava:watcher Files remain that cannot be traced to specific tests: [
  ava:watcher   '/home/starptech/repositories/starptech/graphql-registry/build/registry/maintanance/garbage-collect.test.js',
  ava:watcher   '/home/starptech/repositories/starptech/graphql-registry/build/tsconfig.tsbuildinfo'
  ava:watcher ] +0ms
  ava:watcher Rerunning all tests +0ms
StarpTech commented 3 years ago

Friendly ping @novemberborn. Any idea? I'd work on it.

novemberborn commented 3 years ago

Hi @StarpTech sorry for the delay. I think that function is supposed to detect and ignore changes to files in the src/ directory. The problem may be in the filePath.startsWith(from) call. We shouldn't have to negate the outcome of the .some().

StarpTech commented 3 years ago

I'd be grateful for any help.

novemberborn commented 3 years ago

Would be good to log the values passed to ignoreChange(), as well as the rewritePaths, to see what stands out. I don't think the answer is the snipped you shared above.

alebianco commented 3 years ago

I bumped into the same issue and I mostly agree with @StarpTech on the cause

when the compilation step is enabled, you want to monitor changes on the .ts files and to do so, the current check on the rewritePaths must be reversed. You want to trigger on changes in the "from" paths and ignore changes in the "to" paths.

on the other hand, if the compilation is done externally, you don't want to trigger the test then the .ts file change, because the compiled output probably isn't ready yet. In this case the current check on the rewritePaths is correct: it ignores the "from" paths and trigger for changes in the "to" path.

Also, the current behaviour does not match with what's described in the main README file.

novemberborn commented 3 years ago

Thanks for the clarification @alebianco. Would appreciate some PRs to fix.

alebianco commented 3 years ago

@novemberborn let me know if I have to do something about those pending checks