Skip to content

Improve integration with AVA #11

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

Merged
merged 2 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function isValidRewritePaths(rewritePaths) {
}

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

const {
extensions = ['ts'],
rewritePaths
rewritePaths: relativeRewritePaths
} = config;

const rewritePaths = Object.entries(relativeRewritePaths).map(([from, to]) => [
path.join(protocol.projectDir, from),
path.join(protocol.projectDir, to)
]);
const testFileExtension = new RegExp(`\\.(${extensions.map(ext => escapeStringRegexp(ext)).join('|')})$`);

return {
async compile() {
return {
extensions: extensions.slice(),
rewritePaths: Object.entries(rewritePaths).map(([from, to]) => [
path.join(protocol.projectDir, from),
path.join(protocol.projectDir, to)
])
rewritePaths: rewritePaths.slice()
};
},

get extensions() {
return extensions.slice();
},

ignoreChange(filePath) {
if (!testFileExtension.test(filePath)) {
return false;
}

return rewritePaths.some(([from]) => filePath.startsWith(from));
},

resolveTestFile(testfile) {
if (!testFileExtension.test(testfile)) {
return testfile;
}

const rewrite = rewritePaths.find(([from]) => testfile.startsWith(from));
if (rewrite === undefined) {
return testfile;
}

const [from, to] = rewrite;
// TODO: Support JSX preserve mode — https://www.typescriptlang.org/docs/handbook/jsx.html
return `${to}${testfile.slice(from.length)}`.replace(testFileExtension, '.js');
},

updateGlobs({filePatterns, ignoredByWatcherPatterns}) {
return {
filePatterns: [
...filePatterns,
'!**/*.d.ts',
...Object.values(relativeRewritePaths).map(to => `!${to}**`)
],
ignoredByWatcherPatterns: [
...ignoredByWatcherPatterns,
...Object.values(relativeRewritePaths).map(to => `${to}**/*.js.map`)
]
};
}
};
},
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@
"lcov",
"text"
]
},
"xo": {
"rules": {
"import/order": "off"
}
}
}
43 changes: 43 additions & 0 deletions test/protocol-ava-3.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const path = require('path');
const test = require('ava');
const pkg = require('../package.json');
const makeProvider = require('..');

const withProvider = (t, run) => run(t, makeProvider({
negotiateProtocol(identifiers, {version}) {
t.true(identifiers.includes('ava-3.2'));
t.is(version, pkg.version);
return {
ava: {version: '3.2.0'},
identifier: 'ava-3.2',
normalizeGlobPatterns: patterns => patterns,
async findFiles({patterns}) {
return patterns.map(file => path.join(__dirname, file));
},
projectDir: __dirname
};
}
}));

test('negotiates ava-3.2 protocol', withProvider, t => t.plan(2));

test('main() ignoreChange()', withProvider, (t, provider) => {
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
t.true(main.ignoreChange(path.join(__dirname, 'src/foo.ts')));
t.false(main.ignoreChange(path.join(__dirname, 'build/foo.js')));
});

test('main() resolveTestfile()', withProvider, (t, provider) => {
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
t.is(main.resolveTestFile(path.join(__dirname, 'src/foo.ts')), path.join(__dirname, 'build/foo.js'));
t.is(main.resolveTestFile(path.join(__dirname, 'build/foo.js')), path.join(__dirname, 'build/foo.js'));
t.is(main.resolveTestFile(path.join(__dirname, 'foo/bar.ts')), path.join(__dirname, 'foo/bar.ts'));
});

test('main() updateGlobs()', withProvider, (t, provider) => {
const main = provider.main({config: {rewritePaths: {'src/': 'build/'}}});
t.snapshot(main.updateGlobs({
filePatterns: ['src/test.ts'],
ignoredByWatcherPatterns: ['assets/**']
}));
});
21 changes: 21 additions & 0 deletions test/snapshots/protocol-ava-3.2.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Snapshot report for `test/protocol-ava-3.2.js`

The actual snapshot is saved in `protocol-ava-3.2.js.snap`.

Generated by [AVA](https://avajs.dev).

## main() updateGlobs()

> Snapshot 1

{
filePatterns: [
'src/test.ts',
'!**/*.d.ts',
'!build/**',
],
ignoredByWatcherPatterns: [
'assets/**',
'build/**/*.js.map',
],
}
Binary file added test/snapshots/protocol-ava-3.2.js.snap
Binary file not shown.