typescript icon indicating copy to clipboard operation
typescript copied to clipboard

Ava watch does not work with pre-compile option

Open StarpTech opened this issue 3 years ago • 14 comments

Please provide details about:

  • What you're trying to do

Ava watch mode with this package.

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

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.

  • What you expected to happen

Ava runner should rerun on file change.

ava: 3.15.0

StarpTech avatar May 09 '21 18:05 StarpTech

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.

novemberborn avatar May 09 '21 18:05 novemberborn

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.

StarpTech avatar May 09 '21 19:05 StarpTech

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.

novemberborn avatar May 12 '21 08:05 novemberborn

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

no, same result.

StarpTech avatar May 14 '21 09:05 StarpTech

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.

novemberborn avatar May 14 '21 15:05 novemberborn

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

StarpTech avatar May 14 '21 19:05 StarpTech

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 avatar May 14 '21 20:05 StarpTech

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

StarpTech avatar May 23 '21 15:05 StarpTech

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().

novemberborn avatar May 24 '21 08:05 novemberborn

I'd be grateful for any help.

StarpTech avatar Sep 22 '21 15:09 StarpTech

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.

novemberborn avatar Sep 22 '21 16:09 novemberborn

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.

alebianco avatar Nov 16 '21 15:11 alebianco

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

novemberborn avatar Nov 16 '21 15:11 novemberborn

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

alebianco avatar Nov 20 '21 11:11 alebianco