Skip to content

Commit

Permalink
Merge e5d3ac5 into 92785df
Browse files Browse the repository at this point in the history
  • Loading branch information
markis authored Jan 17, 2018
2 parents 92785df + e5d3ac5 commit 1c82e04
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 90 deletions.
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 50,
"lines": 50,
"statements": 50
"functions": 75,
"lines": 75,
"statements": 75
}
},
"reporters": [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"typescript": "2.6.2"
},
"dependencies": {
"json-in-place": "1.0.1",
"yargs-parser": "8.1.0"
}
}
10 changes: 3 additions & 7 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@ const mockConfig = {
rootDir: './example',
};

jest.mock('fs');
process.cwd = jest.fn().mockReturnValue(resolve('./example'));

describe('jest-ratchet', () => {
beforeAll(() => {
jest.mock('fs');
process.cwd = jest.fn().mockReturnValue(resolve('./example'));
});
afterAll(() => {
jest.unmock('fs');
});

it('will initialize without error', () => {
const config = {
Expand Down
14 changes: 4 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { readFileSync, writeFileSync } from 'fs';
import { readFileSync } from 'fs';

import { getLastError } from './errors';
import { Config, IstanbulCoverage, JestCoverage } from './interfaces';
import { updateFile } from './json';
import {
findCoveragePath,
findCoverageSummaryPath,
findJestConfigPath,
} from './locations';
import { ratchetCoverage, setCoverage } from './ratchet';
import { ratchetCoverage } from './ratchet';

export default class JestRatchet {
constructor(
Expand All @@ -29,14 +30,7 @@ export default class JestRatchet {
const ratchetResult = ratchetCoverage(coverageThreshold, coverageSummary);

const jestConfigPath = findJestConfigPath(process.cwd(), process.argv);
const jestConfigRaw = readFileSync(jestConfigPath, 'utf-8');
const jestConfig = JSON.parse(jestConfigRaw);
if (jestConfig.jest) {
setCoverage(jestConfig.jest.coverageThreshold, ratchetResult);
} else {
setCoverage(jestConfig.coverageThreshold, ratchetResult);
}
writeFileSync(jestConfigPath, JSON.stringify(jestConfig, null, 2), 'utf-8');
updateFile(jestConfigPath, ratchetResult);
} catch (e) {
// tslint:disable-next-line
console.error(e);
Expand Down
38 changes: 38 additions & 0 deletions src/json.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { setCoverage } from './json';

const mockThreshold = {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
};

jest.mock('fs');

describe('json', () => {
it('will ratchet percents', () => {
const json = `
{
"coverageThreshold": {
"global": {
"branches": 50, "functions": 50,
"lines": 50, "statements": 50
}
}
}`;
const result = setCoverage(json, mockThreshold, '');

expect(result).toEqual(`
{
"coverageThreshold": {
"global": {
"branches": 80, "functions": 80,
"lines": 80, "statements": 80
}
}
}`);
});

});
30 changes: 30 additions & 0 deletions src/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readFileSync, writeFileSync } from 'fs';
import inplace = require('json-in-place');

import { JestCoverage } from './interfaces';

export function updateFile(fileName: string, result: JestCoverage) {
const jestConfigRaw = readFileSync(fileName, 'utf-8');
const jestConfig = JSON.parse(jestConfigRaw);

const prefix = jestConfig.jest ? 'jest.' : '';
const newFile = setCoverage(jestConfigRaw, result, prefix);

writeFileSync(fileName, newFile, 'utf-8');
}

export function setCoverage(
source: string,
result: JestCoverage,
prefix: string,
): string {
prefix += 'coverageThreshold.';
const newSource = inplace(source);
for (const key of Object.keys(result)) {
newSource.set(prefix + key + '.branches', result[key].branches);
newSource.set(prefix + key + '.functions', result[key].functions);
newSource.set(prefix + key + '.lines', result[key].lines);
newSource.set(prefix + key + '.statements', result[key].statements);
}
return newSource.toString();
}
48 changes: 1 addition & 47 deletions src/ratchet.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ratchetCoverage, setCoverage } from './ratchet';
import { ratchetCoverage } from './ratchet';

const mockCoverage = {
total: {
Expand Down Expand Up @@ -51,50 +51,4 @@ describe('ratchet', () => {
});
});

it('will set coverage', () => {
const mockSource = {
global: {
branches: 50,
functions: 50,
lines: 50,
statements: 50,
},
};
const mockResult = {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
};
setCoverage(mockSource, mockResult);

expect(mockSource.global.branches).toBe(80);
expect(mockSource.global.functions).toBe(80);
expect(mockSource.global.lines).toBe(80);
expect(mockSource.global.statements).toBe(80);
});

it('will set coverage for partial source', () => {
const mockSource: any = {
global: {
branches: 50,
},
};
const mockResult = {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
},
};
setCoverage(mockSource, mockResult);

expect(mockSource.global.branches).toBe(80);
expect(mockSource.global.functions).toBe(80);
expect(mockSource.global.lines).toBe(80);
expect(mockSource.global.statements).toBe(80);
});
});
18 changes: 0 additions & 18 deletions src/ratchet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@ import {
JestCoverageCategory,
} from './interfaces';

export function setCoverage(
sourceCoverage: JestCoverage,
resultCoverage: JestCoverage,
): void {
for (const key of Object.keys(sourceCoverage)) {
const source = sourceCoverage[key];
const result = resultCoverage[key];
source.branches = setSingleCoverage(source.branches, result.branches);
source.functions = setSingleCoverage(source.functions, result.functions);
source.lines = setSingleCoverage(source.lines, result.lines);
source.statements = setSingleCoverage(source.statements, result.statements);
}
}

function setSingleCoverage(sourceValue?: number, resultValue?: number) {
return typeof resultValue === 'number' ? resultValue : sourceValue;
}

export function ratchetCoverage(
threshold: JestCoverage,
summary: IstanbulCoverage,
Expand Down
20 changes: 15 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@types/jest@^22.0.1":
"@types/jest@22.0.1":
version "22.0.1"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.0.1.tgz#6370a6d60cce3845e4cd5d00bf65f654264685bc"

Expand Down Expand Up @@ -1596,7 +1596,7 @@ jest-worker@^22.0.6:
dependencies:
merge-stream "^1.0.1"

jest@^22.0.6:
jest@22.0.6:
version "22.0.6"
resolved "https://registry.yarnpkg.com/jest/-/jest-22.0.6.tgz#0d0d3bdc402c43cf5a3eae58a55cff6da8b6820f"
dependencies:
Expand Down Expand Up @@ -1650,6 +1650,16 @@ jsesc@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b"

json-in-place@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/json-in-place/-/json-in-place-1.0.1.tgz#8a1ecd25a69ce1900552cd7152bd93754de4e1f0"
dependencies:
json-lexer "1.1.1"

json-lexer@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/json-lexer/-/json-lexer-1.1.1.tgz#bd3ed5d7e560b9d99ad2234f28ca706fb377b7d4"

json-schema-traverse@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.3.1.tgz#349a6d44c53a51de89b40805c5d5e59b417d3340"
Expand Down Expand Up @@ -2652,7 +2662,7 @@ trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"

ts-jest@^22.0.1:
ts-jest@22.0.1:
version "22.0.1"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-22.0.1.tgz#48942936a466c2e76e259b02e2f1356f1839afc3"
dependencies:
Expand All @@ -2671,7 +2681,7 @@ tslib@^1.8.0, tslib@^1.8.1:
version "1.8.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.1.tgz#6946af2d1d651a7b1863b531d6e5afa41aa44eac"

tslint@^5.9.1:
tslint@5.9.1:
version "5.9.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.9.1.tgz#1255f87a3ff57eb0b0e1f0e610a8b4748046c9ae"
dependencies:
Expand Down Expand Up @@ -2863,7 +2873,7 @@ yallist@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"

yargs-parser@^8.1.0:
yargs-parser@8.1.0, yargs-parser@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-8.1.0.tgz#f1376a33b6629a5d063782944da732631e966950"
dependencies:
Expand Down

0 comments on commit 1c82e04

Please sign in to comment.