Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Add watcher that will run tests and lint #616

Merged
merged 2 commits into from
Oct 30, 2018
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
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,18 @@ More information about writing rule tests can be found in [TSLint documentation]

Please also see our [Code of Conduct](CODE_OF_CONDUCT.md).

### Watchers

Two npm scripts can be used to watch files and simplify testing and linting with constant changes.

Use `npm start` to start watcher that will rebuild rules and Mocha tests.

> NOTE: Run `npm test` before `npm start` to copy all required files to `dist` folder.

Use `npm run watch:run-tests` that will re-run tests (both Mocha and TSLint) and lint code when: files changed in `tests`, `test-data` or `npm start` compiled rules or tests code.

> NOTE: It is recommended to wait when `npm start` will finish initial compilation before starting `npm run watch:run-tests` (in separate terminal).

### Debug code

**VS Code**
Expand Down Expand Up @@ -1268,8 +1280,6 @@ Set a breakpoint somewhere in your code and resume execution. Your breakpoint sh

You can use `npm start` watcher that will rebuild TS files from `src` before launching debug commands.

> NOTE: Run `npm test` before `npm start` to copy all required files to `dist` folder.

> NOTE: If breakpoints are not hit you can try to use `inlineSourceMaps` instead of `sourceMaps` in `tsconfig.json`

### Creating a new Release
Expand Down
24 changes: 12 additions & 12 deletions build-tasks/validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ function ruleIsEnabled(value) {
return false;
}

if (typeof value === "boolean") {
if (typeof value === 'boolean') {
return value;
}

return value[0];
}

const disabledRules = new Set([
"missing-jsdoc",
"no-duplicate-case",
"no-empty-interfaces",
"no-empty-line-after-opening-brace",
"no-multiline-string",
"no-relative-imports",
"no-stateless-class",
"no-unexternalized-strings",
"no-var-self",
"react-tsx-curly-spacing",
"valid-typeof",
'missing-jsdoc',
'no-duplicate-case',
'no-empty-interfaces',
'no-empty-line-after-opening-brace',
'no-multiline-string',
'no-relative-imports',
'no-stateless-class',
'no-unexternalized-strings',
'no-var-self',
'react-tsx-curly-spacing',
'valid-typeof'
]);

const errors = [];
Expand Down
72 changes: 72 additions & 0 deletions build-tasks/watch-run-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { yellowBright, red, green } = require('chalk');
const chokidar = require('chokidar');
const runAll = require('npm-run-all');

const changes = new Set();
let testsRunning = false;

function simpleDebounce(delay, fn) {
let timer;

return () => {
clearTimeout(timer);
timer = setTimeout(fn, delay);
};
}

function runTests() {
console.log(green('\nStarting tests'));

testsRunning = true;

return runAll(['test:*', 'lint:*'], {
stdin: process.stdin,
stdout: process.stdout,
stderr: process.stderr
})
.then(() => console.log(green('\nSuccess.')))
.catch(() => console.error(red('\nFailure!')))
.then(() => { testsRunning = false; })
}

const scheduleTests = simpleDebounce(1000, () => {
console.log(yellowBright('Scheduling new tests because of changes in: \n') + [...changes].join('\n'));

changes.clear();

runTests()
.then(() => {
if (changes.size > 0) {
scheduleTests();
}
});
});

function registerChange(path) {
console.log(yellowBright('Registered change: ') + path);
changes.add(path);

if (!testsRunning) {
scheduleTests();
}
}

const fileWatcher = chokidar
.watch(['dist/src/**/*.js', 'test-data/**', 'tests/**']);

function subscribeFileEvents() {
fileWatcher
.on('add', registerChange)
.on('change', registerChange)
.on('unlink', registerChange);
}

fileWatcher.on('ready', () => {
const watchedCount = Object.keys(fileWatcher.getWatched())
.reduce((memo, items) => memo + items.length, 0);
console.log(`Watching ${watchedCount} files and folders.`);

subscribeFileEvents();

runTests();
});
Loading