Skip to content
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

feat: watch dynamic imports #397

Merged
merged 24 commits into from
Jun 15, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules
.env
.nyc_output
/.temp
/test-src
/test-tests
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"run",
"queue",
"queuing",
"watch",
"nodejs",
"node",
"cli-app",
Expand Down
39 changes: 33 additions & 6 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { platformIsValid } from '../helpers/get-runtime.js';
import { format } from '../helpers/format.js';
import { write } from '../helpers/logs.js';
import { hr } from '../helpers/hr.js';
import { mapTests, normalizePath } from '../services/map-tests.js';
import { watch } from '../services/watch.js';
import { poku } from '../modules/poku.js';
import { kill } from '../modules/processes.js';
Expand Down Expand Up @@ -112,20 +113,43 @@ import type { Configs } from '../@types/poku.js';
const executing = new Set<string>();
const interval = Number(getArg('watch-interval')) || 1500;

fileResults.success.clear();
fileResults.fail.clear();
const resultsClear = () => {
fileResults.success.clear();
fileResults.fail.clear();
};

hr();
write(`Watching: ${dirs.join(', ')}`);
resultsClear();

mapTests('.', dirs).then((mappedTests) => [
Array.from(mappedTests.keys()).forEach((mappedTest) => {
watch(mappedTest, (file, event) => {
if (event === 'change') {
const filePath = normalizePath(file);
if (executing.has(filePath)) return;

executing.add(filePath);
resultsClear();

const tests = mappedTests.get(filePath);
if (!tests) return;

poku(tests, options).then(() => {
setTimeout(() => {
executing.delete(filePath);
}, interval);
});
}
});
}),
]);

dirs.forEach((dir) => {
watch(dir, (file, event) => {
if (event === 'change') {
if (executing.has(file)) return;

executing.add(file);
fileResults.success.clear();
fileResults.fail.clear();
resultsClear();

poku(file, options).then(() => {
setTimeout(() => {
Expand All @@ -135,6 +159,9 @@ import type { Configs } from '../@types/poku.js';
}
});
});

hr();
write(`Watching: ${dirs.join(', ')}`);
}
})();

Expand Down
12 changes: 12 additions & 0 deletions src/polyfills/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
stat as nodeStat,
readdir as nodeReaddir,
readFile as nodeReadFile,
type Dirent,
type Stats,
} from 'node:fs';
Expand All @@ -27,4 +28,15 @@ export const stat = (path: string): Promise<Stats> => {
});
};

export const readFile = (
path: string,
encoding: BufferEncoding = 'utf-8'
): Promise<string> =>
new Promise((resolve, reject) => {
nodeReadFile(path, encoding, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});

/* c8 ignore stop */
54 changes: 54 additions & 0 deletions src/services/map-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* c8 ignore next */
import { relative, dirname, sep } from 'node:path';
import { stat, readFile } from '../polyfills/fs.js';
import { listFiles } from '../modules/list-files.js';

const filter = /\.(js|cjs|mjs|ts|cts|mts)$/;

export const normalizePath = (filePath: string) =>
filePath
.replace(/(\.\/)/g, '')
.replace(/^\.+/, '')
.replace(/[/\\]+/g, sep)
.replace(/\\/g, '/');

/* c8 ignore next */
export const mapTests = async (srcDir: string, testPaths: string[]) => {
const allTestFiles: string[] = [];
const allSrcFiles = await listFiles(srcDir, { filter });
const importMap = new Map<string, string[]>();

for (const testPath of testPaths) {
const stats = await stat(testPath);

if (stats.isDirectory()) {
const testFiles = await listFiles(testPath, { filter });

allTestFiles.push(...testFiles);
} else if (stats.isFile() && filter.test(testPath))
allTestFiles.push(testPath);
}

for (const testFile of allTestFiles) {
const content = await readFile(testFile, 'utf-8');

for (const srcFile of allSrcFiles) {
const relativePath = normalizePath(relative(dirname(testFile), srcFile));
const normalizedSrcFile = normalizePath(srcFile);

/* c8 ignore start */
if (
content.includes(relativePath.replace(filter, '')) ||
content.includes(normalizedSrcFile)
) {
if (!importMap.has(normalizedSrcFile))
importMap.set(normalizedSrcFile, []);

importMap.get(normalizedSrcFile)!.push(normalizePath(testFile));
}
/* c8 ignore stop */
}
}

return importMap;
};
71 changes: 71 additions & 0 deletions test/unit/map-tests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import process from 'node:process';
import { nodeVersion } from '../../src/helpers/get-runtime.js';

if (nodeVersion && nodeVersion < 14) process.exit(0);

import { join } from 'node:path';
import { writeFileSync, mkdirSync, rmSync } from 'node:fs';
import { it } from '../../src/modules/it.js';
import { describe } from '../../src/modules/describe.js';
import { beforeEach, afterEach } from '../../src/modules/each.js';
import { assert } from '../../src/modules/assert.js';
import { mapTests, normalizePath } from '../../src/services/map-tests.js';

const createFileSync = (filePath: string, content: string) => {
writeFileSync(filePath, content);
};

const createDirSync = (dirPath: string) => {
mkdirSync(dirPath, { recursive: true });
};

const removeDirSync = (dirPath: string) => {
rmSync(dirPath, { recursive: true, force: true });
};

const testSrcDir = 'test-src';
const testTestDir = 'test-tests';

describe('mapTests', async () => {
beforeEach(() => {
createDirSync(testSrcDir);
createDirSync(testTestDir);
createFileSync(join(testSrcDir, 'example.js'), 'export const foo = 42;');
createFileSync(
join(testTestDir, 'example.test.js'),
'import { foo } from "../test-src/example.js";'
);
});

afterEach(() => {
removeDirSync(testSrcDir);
removeDirSync(testTestDir);
});

await it('should map test files to their corresponding source files', async () => {
const importMap = await mapTests(testSrcDir, [testTestDir]);

const expected = new Map([
[
normalizePath('test-src/example.js'),
[normalizePath('test-tests/example.test.js')],
],
]);

assert.deepStrictEqual(Array.from(importMap), Array.from(expected));
});

await it('should map single test file correctly', async () => {
const singleTestFile = join(testTestDir, 'example.test.js');
const importMap = await mapTests(testSrcDir, [singleTestFile]);

const expected = new Map([
[
normalizePath('test-src/example.js'),
[normalizePath('test-tests/example.test.js')],
],
]);

assert.deepStrictEqual(Array.from(importMap), Array.from(expected));
});
});
22 changes: 11 additions & 11 deletions test/unit/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Watcher Service', async () => {
setTimeout(() => {
fs.writeFileSync(filePath, 'export default { updated: true };'); // update
resolve(undefined);
}, 1000);
}, 250);
});

return await new Promise((resolve) => {
Expand All @@ -73,7 +73,7 @@ describe('Watcher Service', async () => {

watcher.stop();
resolve(undefined);
}, 1000);
}, 250);
});
});

Expand All @@ -92,7 +92,7 @@ describe('Watcher Service', async () => {
setTimeout(() => {
fs.writeFileSync(newFilePath, 'export default {};'); // update
resolve(undefined);
}, 1000);
}, 250);
});

return await new Promise((resolve) => {
Expand All @@ -110,7 +110,7 @@ describe('Watcher Service', async () => {

watcher.stop();
resolve(undefined);
}, 1000);
}, 250);
});
});

Expand All @@ -129,7 +129,7 @@ describe('Watcher Service', async () => {
'Callback should not be called after watcher is stopped'
);
resolve(undefined);
}, 1000);
}, 250);
});
});

Expand All @@ -149,7 +149,7 @@ describe('Watcher Service', async () => {
setTimeout(() => {
fs.writeFileSync(newFilePath, 'export default {};'); // update
resolve(undefined);
}, 1000);
}, 250);
});

return new Promise((resolve) => {
Expand All @@ -167,7 +167,7 @@ describe('Watcher Service', async () => {

watcher.stop();
resolve(undefined);
}, 1000);
}, 250);
});
});

Expand All @@ -187,7 +187,7 @@ describe('Watcher Service', async () => {
setTimeout(() => {
fs.writeFileSync(newNestedFilePath, 'export default {};'); // update
resolve(undefined);
}, 1000);
}, 250);
});

return await new Promise((resolve) => {
Expand All @@ -206,7 +206,7 @@ describe('Watcher Service', async () => {

watcher.stop();
resolve(undefined);
}, 1000);
}, 250);
});
});

Expand All @@ -224,7 +224,7 @@ describe('Watcher Service', async () => {
setTimeout(() => {
fs.writeFileSync(filePath, 'export default {};');
resolve(undefined);
}, 1000);
}, 250);
});

return await new Promise((resolve) => {
Expand All @@ -242,7 +242,7 @@ describe('Watcher Service', async () => {

watcher.stop();
resolve(undefined);
}, 1000);
}, 250);
});
});
});
4 changes: 2 additions & 2 deletions website/docs/documentation/poku/options/watch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ sidebar_position: 9

# `watch`

Watches the events on test paths (after running all the tests), re-running the tests files that are updated for each new event.
Watches the events on tests and files that contains tests (after running all the tests), re-running the tests files that are updated for each new event.

> Currently, `watch` mode only watches for events from test files and directories.
> Note that the import mapping only looks for direct imports.

## CLI

Expand Down
Loading