Skip to content

Commit

Permalink
Fix tests and references
Browse files Browse the repository at this point in the history
  • Loading branch information
markis committed Jan 29, 2018
1 parent ac97726 commit 607bc1a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
6 changes: 6 additions & 0 deletions __mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const fs = require.requireActual('fs');

const mockCB = function (event, cb) { if (cb) cb(); };
fs.watch = jest.fn().mockReturnValue({
on: mockCB,
once: mockCB,
close: jest.fn(),
});
fs.write = jest.fn();
fs.writeSync = jest.fn();
fs.writeFile = jest.fn();
Expand Down
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
51 changes: 49 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
jest.mock('fs');

import fs from 'fs';
import { resolve } from 'path';
import JestRatchet from './index';

Expand All @@ -16,7 +19,6 @@ const mockConfig = {
rootDir: './example',
};

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

describe('jest-ratchet', () => {
Expand All @@ -43,7 +45,52 @@ describe('jest-ratchet', () => {
expect(jestRatchet.getLastError).toThrowError(/collectCoverage/);
});

it('will run ratchet coverage report', () => {
it('will ratchet percentages', () => {
fs.readFileSync = jest.fn()
.mockReturnValueOnce(JSON.stringify({
total: {
branches: {pct: 100},
functions: {pct: 100},
lines: {pct: 100},
statements: {pct: 100},
},
}))
.mockReturnValueOnce(JSON.stringify({
coverageThreshold: {
global: {
branches: 50,
functions: 50,
lines: 50,
statements: 50,
},
},
}));

const jestRatchet = new JestRatchet(mockConfig);
jestRatchet.onRunComplete();
});

it('will ratchet lines', () => {
fs.readFileSync = jest.fn()
.mockReturnValueOnce(JSON.stringify({
total: {
branches: {covered: 100},
functions: {covered: 100},
lines: {covered: 100},
statements: {covered: 100},
},
}))
.mockReturnValueOnce(JSON.stringify({
coverageThreshold: {
global: {
branches: -50,
functions: -50,
lines: -50,
statements: -50,
},
},
}));

const jestRatchet = new JestRatchet(mockConfig);
jestRatchet.onRunComplete();
});
Expand Down
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
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"noUnusedParameters": true,
"strictNullChecks": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"typeRoots": [
"./node_modules/@types/",
"./types/"
Expand Down

0 comments on commit 607bc1a

Please sign in to comment.