Skip to content

Commit

Permalink
Fix for Issue #92
Browse files Browse the repository at this point in the history
  • Loading branch information
jkworth committed Jun 19, 2021
1 parent 94c17a2 commit 8de6096
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 63 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"coverageReporters": [
"json-summary",
"lcovonly",
"text-summary",
"html"
],
"coverageThreshold": {
Expand Down
118 changes: 55 additions & 63 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { closeSync, existsSync, FSWatcher, mkdirSync, openSync, readFileSync, watch } from 'fs';

import { existsSync, FSWatcher, mkdirSync, readFileSync, watch } from 'fs';
import { getLastError, TimeoutError, tryOrReject } from './errors';
import {
Config,
IstanbulCoverage,
RatchetOptions,
} from './interfaces';
import { Config, IstanbulCoverage, RatchetOptions } from './interfaces';
import { updateFile } from './json';
import {
findCoveragePath,
findCoverageSummaryPath,
findJestConfigPath,
} from './locations';
import { findCoveragePath, findCoverageSummaryPath, findJestConfigPath } from './locations';
import { noop } from './noop';
import { ratchetCoverage } from './ratchet';

Expand All @@ -20,50 +11,48 @@ export default class JestRatchet {
public onRunComplete: () => void = noop;
public runResult = Promise.resolve();

constructor(
globalConfig: Config,
options: RatchetOptions = {},
) {
constructor(globalConfig: Config, options: RatchetOptions = {}) {
if (!process.env.DISABLE_JEST_RATCHET) {
this.getLastError = getLastError.bind(this, globalConfig);
this.runResult = onRunComplete(globalConfig, options);
this.runResult.catch(e => {
this.getLastError = () => { throw e; };
this.runResult.catch((e) => {
this.getLastError = () => {
throw e;
};
});
}
}
}

const onSummaryReportComplete = (
reject: () => void,
resolve: () => void,
watcher: FSWatcher,
timeoutTimer: NodeJS.Timeout | undefined,
coverageSummaryPath: string,
jestConfigPath: string,
globalConfig: Config,
options: RatchetOptions,
) => () =>
tryOrReject(reject, () => {
watcher.close();
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}
const onSummaryReportComplete =
(
reject: () => void,
resolve: () => void,
watcher: FSWatcher,
timeoutTimer: NodeJS.Timeout | undefined,
coverageSummaryPath: string,
jestConfigPath: string,
globalConfig: Config,
options: RatchetOptions
) =>
() =>
tryOrReject(reject, () => {
watcher.close();
if (timeoutTimer) {
clearTimeout(timeoutTimer);
}

const coverageRaw = readFileSync(coverageSummaryPath, 'utf-8');
const summary: IstanbulCoverage = JSON.parse(coverageRaw);
const threshold = globalConfig.coverageThreshold!;
const ratchetResult = ratchetCoverage(threshold, summary, options);
const coverageRaw = readFileSync(coverageSummaryPath, 'utf-8');
const summary: IstanbulCoverage = JSON.parse(coverageRaw);
const threshold = globalConfig.coverageThreshold!;
const ratchetResult = ratchetCoverage(threshold, summary, options);

updateFile(jestConfigPath, ratchetResult);
resolve();
});
updateFile(jestConfigPath, ratchetResult);
resolve();
});

const onRunComplete = (
globalConfig: Config,
options: RatchetOptions,
): Promise<void> => new Promise(
(resolve, reject) =>
const onRunComplete = (globalConfig: Config, options: RatchetOptions): Promise<void> =>
new Promise((resolve, reject) =>
tryOrReject(reject, () => {
const coverageDirectory = findCoveragePath(globalConfig);
const coverageSummaryPath = findCoverageSummaryPath(coverageDirectory);
Expand All @@ -72,26 +61,29 @@ const onRunComplete = (
if (!existsSync(coverageDirectory)) {
mkdirSync(coverageDirectory);
}
if (!existsSync(coverageSummaryPath)) {
closeSync(openSync(coverageSummaryPath, 'w'));
}

const watcher = watch(coverageDirectory);
const timeout = options.timeout;

const timeoutTimer = timeout ? setTimeout(() => {
watcher.close();
reject(new TimeoutError(coverageDirectory, timeout));
}, timeout) : undefined;
const timeoutTimer = timeout
? setTimeout(() => {
watcher.close();
reject(new TimeoutError(coverageDirectory, timeout));
}, timeout)
: undefined;

watcher.once('change', onSummaryReportComplete(
reject,
resolve,
watcher,
timeoutTimer,
coverageSummaryPath,
jestConfigPath,
globalConfig,
options,
));
}));
watcher.once(
'change',
onSummaryReportComplete(
reject,
resolve,
watcher,
timeoutTimer,
coverageSummaryPath,
jestConfigPath,
globalConfig,
options
)
);
})
);

0 comments on commit 8de6096

Please sign in to comment.