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

Fix for Issue #92 - fs.watch Handler on Mac OS X Invoked Before Tests Complete #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
)
);
})
);