Skip to content

Commit 586d820

Browse files
committed
Improve integration with AVA
Have the watcher ignore changes to the original TypeScript source files. This way, the watcher will only react to changes to the TypeScript build output. Allow the watcher to resolve the rewritten path of a test file, so it can track it as a dependency of the original TypeScript source file. This way, when the build output changes, the watcher knows to re-run the original test file. Update file patterns to ignore *.d.ts files, as well as any files in the build output. Updated the ignored-by-watcher patterns to ignore source map files from the build output. Fixes #10, #7, #9.
1 parent a5ea5ce commit 586d820

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

index.js

+46-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function isValidRewritePaths(rewritePaths) {
2424
}
2525

2626
module.exports = ({negotiateProtocol}) => {
27-
const protocol = negotiateProtocol(['ava-3'], {version: pkg.version});
27+
const protocol = negotiateProtocol(['ava-3.2', 'ava-3'], {version: pkg.version});
2828
if (protocol === null) {
2929
return;
3030
}
@@ -47,22 +47,62 @@ module.exports = ({negotiateProtocol}) => {
4747

4848
const {
4949
extensions = ['ts'],
50-
rewritePaths
50+
rewritePaths: relativeRewritePaths
5151
} = config;
5252

53+
const rewritePaths = Object.entries(relativeRewritePaths).map(([from, to]) => [
54+
path.join(protocol.projectDir, from),
55+
path.join(protocol.projectDir, to)
56+
]);
57+
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);
58+
5359
return {
5460
async compile() {
5561
return {
5662
extensions: extensions.slice(),
57-
rewritePaths: Object.entries(rewritePaths).map(([from, to]) => [
58-
path.join(protocol.projectDir, from),
59-
path.join(protocol.projectDir, to)
60-
])
63+
rewritePaths: rewritePaths.slice()
6164
};
6265
},
6366

6467
get extensions() {
6568
return extensions.slice();
69+
},
70+
71+
ignoreChange(filePath) {
72+
if (!testFileExtension.test(filePath)) {
73+
return false;
74+
}
75+
76+
return rewritePaths.some(([from]) => filePath.startsWith(from));
77+
},
78+
79+
resolveTestFile(testfile) {
80+
if (!testFileExtension.test(testfile)) {
81+
return testfile;
82+
}
83+
84+
const rewrite = rewritePaths.find(([from]) => testfile.startsWith(from));
85+
if (rewrite === undefined) {
86+
return testfile;
87+
}
88+
89+
const [from, to] = rewrite;
90+
// TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html
91+
return `${to}${testfile.slice(from.length)}`.replace(testFileExtension, '.js');
92+
},
93+
94+
updateGlobs({filePatterns, ignoredByWatcherPatterns}) {
95+
return {
96+
filePatterns: [
97+
...filePatterns,
98+
'!**/*.d.ts',
99+
...Object.values(relativeRewritePaths).map(to => `!${to}**`)
100+
],
101+
ignoredByWatcherPatterns: [
102+
...ignoredByWatcherPatterns,
103+
...Object.values(relativeRewritePaths).map(to => `${to}**/*.js.map`)
104+
]
105+
};
66106
}
67107
};
68108
},

test/protocol-ava-3.2.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const path = require('path');
2+
const test = require('ava');
3+
const pkg = require('../package.json');
4+
const makeProvider = require('..');
5+
6+
const withProvider = (t, run) => run(t, makeProvider({
7+
negotiateProtocol(identifiers, {version}) {
8+
t.true(identifiers.includes('ava-3.2'));
9+
t.is(version, pkg.version);
10+
return {
11+
ava: {version: '3.2.0'},
12+
identifier: 'ava-3.2',
13+
normalizeGlobPatterns: patterns => patterns,
14+
async findFiles({patterns}) {
15+
return patterns.map(file => path.join(__dirname, file));
16+
},
17+
projectDir: __dirname
18+
};
19+
}
20+
}));
21+
22+
test('negotiates ava-3.2 protocol', withProvider, t => t.plan(2));
23+
24+
test('main() ignoreChange()', withProvider, (t, provider) => {
25+
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
26+
t.true(main.ignoreChange(path.join(__dirname, 'src/foo.ts')));
27+
t.false(main.ignoreChange(path.join(__dirname, 'build/foo.js')));
28+
});
29+
30+
test('main() resolveTestfile()', withProvider, (t, provider) => {
31+
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
32+
t.is(main.resolveTestFile(path.join(__dirname, 'src/foo.ts')), path.join(__dirname, 'build/foo.js'));
33+
t.is(main.resolveTestFile(path.join(__dirname, 'build/foo.js')), path.join(__dirname, 'build/foo.js'));
34+
t.is(main.resolveTestFile(path.join(__dirname, 'foo/bar.ts')), path.join(__dirname, 'foo/bar.ts'));
35+
});
36+
37+
test('main() updateGlobs()', withProvider, (t, provider) => {
38+
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
39+
t.snapshot(main.updateGlobs({
40+
filePatterns: ['src/test.ts'],
41+
ignoredByWatcherPatterns: ['assets/**']
42+
}));
43+
});

test/snapshots/protocol-ava-3.2.js.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Snapshot report for `test/protocol-ava-3.2.js`
2+
3+
The actual snapshot is saved in `protocol-ava-3.2.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## main() updateGlobs()
8+
9+
> Snapshot 1
10+
11+
{
12+
filePatterns: [
13+
'src/test.ts',
14+
'!**/*.d.ts',
15+
'!build/**',
16+
],
17+
ignoredByWatcherPatterns: [
18+
'assets/**',
19+
'build/**/*.js.map',
20+
],
21+
}
296 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)