Skip to content

Commit

Permalink
Merge 57d30a5 into 4746e10
Browse files Browse the repository at this point in the history
  • Loading branch information
markis committed Jan 30, 2018
2 parents 4746e10 + 57d30a5 commit 134da27
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 182 deletions.
44 changes: 39 additions & 5 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
const fs = require.requireActual('fs');
const path = require('path');

fs.write = jest.fn();
fs.writeSync = jest.fn();
fs.writeFile = jest.fn();
fs.writeFileSync = jest.fn();
const realFs = require.requireActual('fs');
const fs = jest.genMockFromModule('fs');

let regexes = [];
let contents = [];
function __addMockFile(regex, content) {
regexes.push(regex);
contents.push(content);
}
function __resetMockFiles() {
regexes = [];
contents = [];
}
function readFileSync(path, options) {
for (let i = 0; i < regexes.length; i++) {
if (regexes[i].test(path)) {
return contents[i];
}
}
return realFs.readFileSync(path, options);
}

function mockCB(type, cb) {
cb();
}

function watch() {
return {
on: mockCB,
once: mockCB,
close: jest.fn(),
};
}

fs.__addMockFile = __addMockFile;
fs.__resetMockFiles = __resetMockFiles;
fs.readFileSync = readFileSync;
fs.watch = watch;

module.exports = fs;
2 changes: 1 addition & 1 deletion example/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{"total": {"lines":{"total":3,"covered":2,"skipped":0,"pct":66.67},"statements":{"total":3,"covered":2,"skipped":0,"pct":66.67},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Users/markis.taylor/src/jest-ratchet/example/index.js": {"lines":{"total":3,"covered":2,"skipped":0,"pct":66.67},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":3,"covered":2,"skipped":0,"pct":66.67},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/Volumes/Files/src/jest-ratchet/example/index.js": {"lines":{"total":3,"covered":2,"skipped":0,"pct":66.67},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":3,"covered":2,"skipped":0,"pct":66.67},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
}
2 changes: 1 addition & 1 deletion example/jestconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
],
"coverageThreshold": {
"global": {
"branches": 100,
"branches": 50,
"functions": 50,
"lines": 50,
"statements": 50
Expand Down
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
],
"coverageThreshold": {
"global": {
"branches": 100,
"branches": 50,
"functions": 50,
"lines": 66.67,
"statements": 66.67
"lines": 50,
"statements": 50
}
},
"reporters": [
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"collectCoverage": true,
"coverageReporters": [
"json-summary",
"lcovonly"
"lcovonly",
"html"
],
"coverageThreshold": {
"global": {
Expand All @@ -47,7 +48,7 @@
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "^.+\\.test\\.tsx?$",
"testRegex": "test\\.ts$",
"moduleFileExtensions": [
"js",
"ts",
Expand All @@ -58,6 +59,7 @@
"scripts": {
"build": "rollup -c",
"prepublish": "tslint src/*.ts && jest && rollup -c",
"jest": "jest",
"test": "tslint src/*.ts && tsc --noemit && jest --no-cache"
},
"devDependencies": {
Expand Down
57 changes: 0 additions & 57 deletions src/index.test.ts

This file was deleted.

20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { readFileSync, watch } from 'fs';

import { getLastError } from './errors';
import { Config, IstanbulCoverage, JestCoverage } from './interfaces';
Expand All @@ -23,14 +23,20 @@ export default class JestRatchet {
try {
const coverageDirectory = findCoveragePath(this.globalConfig);
const coverageSummaryPath = findCoverageSummaryPath(coverageDirectory);
const coverageRaw = readFileSync(coverageSummaryPath, 'utf-8');
const coverageSummary: IstanbulCoverage = JSON.parse(coverageRaw);
const coverageThreshold: JestCoverage = this.globalConfig.coverageThreshold!;

const ratchetResult = ratchetCoverage(coverageThreshold, coverageSummary);
const watcher = watch(coverageDirectory);
watcher.once('change', () => {
watcher.close();
const coverageRaw = readFileSync(coverageSummaryPath, 'utf-8');
const coverageSummary: IstanbulCoverage = JSON.parse(coverageRaw);
const coverageThreshold: JestCoverage = this.globalConfig.coverageThreshold!;

const jestConfigPath = findJestConfigPath(process.cwd(), process.argv);
updateFile(jestConfigPath, ratchetResult);
const ratchetResult = ratchetCoverage(coverageThreshold, coverageSummary);

const jestConfigPath = findJestConfigPath(process.cwd(), process.argv);

updateFile(jestConfigPath, ratchetResult);
});
} catch (e) {
// tslint:disable-next-line
console.error(e);
Expand Down
38 changes: 0 additions & 38 deletions src/json.test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync, writeFileSync } from 'fs';
import * as jsonInplace from 'json-in-place';
import inplace from 'json-in-place';

import { JestCoverage } from './interfaces';

Expand All @@ -19,7 +19,6 @@ export function setCoverage(
prefix: string,
): string {
prefix += 'coverageThreshold.';
const inplace = jsonInplace as any;
const newSource = inplace(source);
for (const key of Object.keys(result)) {
if (result[key].branches) {
Expand Down
4 changes: 1 addition & 3 deletions src/locations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { isAbsolute, resolve } from 'path';
import * as yargsParser from 'yargs-parser';
import parser from 'yargs-parser';
import { Config } from './interfaces';

type Argv = typeof process.argv;

const parser = yargsParser as any;

export function findJestConfigPath(cwd: string, argv: Argv) {
let configLocation = 'package.json';
const args = parser(argv.slice(2));
Expand Down
54 changes: 0 additions & 54 deletions src/ratchet.test.ts

This file was deleted.

18 changes: 9 additions & 9 deletions src/ratchet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@ export function ratchetCoverage(
const result: any = {};
for (const key of Object.keys(threshold)) {
const summaryKey = key === 'global' ? 'total' : key;
result[key] = _ratchetSingleCoverage(threshold[key], summary[summaryKey]);
result[key] = ratchetSingleCoverage(threshold[key], summary[summaryKey]);
}
return result;
}

function _ratchetSingleCoverage(
function ratchetSingleCoverage(
threshold: JestCoverageCategory,
summary: IstanbulCoverageCategories,
) {
const { branches, functions, lines, statements } = threshold;
return {
branches: _ratchetSingleNumberCoverage(branches, summary.branches),
functions: _ratchetSingleNumberCoverage(functions, summary.functions),
lines: _ratchetSingleNumberCoverage(lines, summary.lines),
statements: _ratchetSingleNumberCoverage(statements, summary.statements),
branches: ratchetSingleNumberCoverage(branches, summary.branches),
functions: ratchetSingleNumberCoverage(functions, summary.functions),
lines: ratchetSingleNumberCoverage(lines, summary.lines),
statements: ratchetSingleNumberCoverage(statements, summary.statements),
};
}

function _ratchetSingleNumberCoverage(
function ratchetSingleNumberCoverage(
num: number | undefined,
category: IstanbulCoverageCategory,
) {
if (num && num > 0 && num <= category.pct) {
if (num && category && num > 0 && num <= category.pct) {
return category.pct;
} else if (num && num < 0 && num >= -category.covered) {
} else if (num && category && num < 0 && num >= -category.covered) {
return -category.covered;
}
}
Loading

0 comments on commit 134da27

Please sign in to comment.