Skip to content

Commit

Permalink
Improve testability (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
singingknight authored and kasperisager committed Feb 27, 2019
1 parent 64dfa8c commit e7825e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 22 deletions.
41 changes: 30 additions & 11 deletions scripts/helpers/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,48 @@ function isTestable(file, project = workspace) {
exports.isTestable = isTestable;

/**
* Return the path to specification file matching specified file
*
* @param {string} file
* @return {boolean}
* @return {string?}
*/
function hasSpecification(file) {
function getSpecification(file) {
const base = path
.dirname(file)
.split(path.sep)
.map(part => (part === "src" ? "test" : part))
.map((part, index) =>
part === "src"
? "test"
: part === "scripts" && index === 0
? `${part}${path.sep}test`
: part
)
.join(path.sep);

const extensions = [".ts", ".tsx"];

for (const n of extensions) {
for (const m of extensions) {
const spec = path.join(base, `${path.basename(file, n)}.spec${m}`);
for (const extension of extensions) {
const spec = path.join(
base,
`${path.basename(file, path.extname(file))}.spec${extension}`
);

if (isFile(spec)) {
return true;
}
if (isFile(spec)) {
return spec;
}
}

return false;
return null;
}

exports.getSpecification = getSpecification;

/**
* Does the specified file have a corresponding specification
* @param {string} file
* @return {boolean}
*/
function hasSpecification(file) {
return getSpecification(file) !== null;
}

exports.hasSpecification = hasSpecification;
55 changes: 44 additions & 11 deletions scripts/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,71 @@ const { watchFiles } = require("./helpers/file-system");
const { endsWith } = require("./helpers/predicates");
const { format, now } = require("./helpers/time");
const notify = require("./helpers/notify");
const { getSpecification } = require("./helpers/typescript");
const { default: chalk } = require("chalk");

const { build } = require("./tasks/build");
const { diagnose } = require("./tasks/diagnose");
const { test } = require("./tasks/test");

const isSpec = endsWith(".spec.ts", ".spec.tsx");

/**
* These are files which failed at last run
* Saved so we can process them again when next file changes
*
* @type {Array<string>}
*/
let failed = [];
let rerunFailed = process.argv.some(v => v.toLowerCase() === "--rerun-failed");

watchFiles(
[
"packages/**/*.ts",
"packages/**/*.tsx",
"packages/**/scripts/**/*.js",
"scripts/**/*.js",
"scripts/test/**/*.ts",
"!scripts/test/**/*.js",
"docs/**/*.ts",
"docs/**/*.tsx"
],
(event, file) => {
const start = now();
failed = failed.filter(v => v !== file);
const fileList = [file, ...(rerunFailed ? failed : [])];
if (rerunFailed) {
failed = [];
}

let success = diagnose(file) && build(file);
fileList.forEach(file => {
const start = now();

if (isSpec(file)) {
success = success && test(file);
}
let success = diagnose(file) && build(file);

const duration = now(start);
if (isSpec(file)) {
success = success && test(file);
} else {
const spec = getSpecification(file);
if (spec !== null) {
success = success && test(spec);
}
}

if (success) {
notify.success(
`${file} ${format(duration, { color: "yellow", threshold: 400 })}`
);
}
const duration = now(start);

if (success) {
notify.success(
`${file} ${format(duration, { color: "yellow", threshold: 400 })}`
);
if (!rerunFailed && failed.length > 0) {
failed.forEach(file => {
notify.warn(`${chalk.gray(file)} failed previously`);
});
}
} else {
failed.push(file);
}
});
}
);

Expand Down

0 comments on commit e7825e8

Please sign in to comment.