Skip to content

Commit

Permalink
Merge pull request #49 from toebeann/simply-mocks
Browse files Browse the repository at this point in the history
Simplify mocks
  • Loading branch information
toebeann committed Nov 1, 2022
2 parents 79d6925 + 012c61b commit 2d6e2c5
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 167 deletions.
1 change: 1 addition & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"preset": "ts-jest",
"injectGlobals": false,
"restoreMocks": true,
"testEnvironment": "node",
"coverageReporters": ["html", "text", "cobertura", "json", "lcovonly"],
"coverageThreshold": {
Expand Down
108 changes: 39 additions & 69 deletions test/commands/purge.spec/handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
Expand All @@ -19,7 +18,6 @@ import { cli, exclude } from '../../../src';

const out = 'test/.commands.purge.handler';
let versions: version[];
let consoleLogMock: jest.SpiedFunction<typeof console.log>;
const consoleLogMockImplementation = <typeof console.log>(<unknown>undefined);

beforeAll(() => ensureDir(resolve(out)));
Expand All @@ -29,16 +27,15 @@ describe('when `out` points to empty directory', () => {
beforeAll(() => emptyDir(out));
beforeEach(() => {
versions = [];
consoleLogMock = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should log: Nothing to purge!', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
await cli().parse(`purge --out ${out}`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Nothing to purge!');
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith('Nothing to purge!');
});
});

Expand All @@ -64,17 +61,14 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user chooses "no"', () => {
beforeEach(() => {
consoleLogMock = jest
it('should exit without making changes', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should exit without making changes', async () => {
inject(['']);
await cli().parse(`purge --out ${out}`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

await each(versions, async (v) =>
expect(
Expand All @@ -86,17 +80,14 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user chooses "yes"', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions', async () => {
inject(['yes']);
await cli().parse(`purge --out ${out}`);
expect(console.log).toHaveBeenCalledTimes(2);
expect(log).toHaveBeenCalledTimes(2);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];

Expand All @@ -117,16 +108,13 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes -y', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions', async () => {
await cli().parse(`purge --out ${out} -y`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];

Expand All @@ -147,16 +135,13 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes -y --exclude ">=2.0.0"', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions', async () => {
await cli().parse(`purge --out ${out} -y --exclude ">=2.0.0"`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v0.1.0'];

Expand All @@ -177,41 +162,35 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes --no-stale', () => {
beforeEach(() => {
consoleLogMock = jest
it('should log: Nothing to purge!', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should log: Nothing to purge!', async () => {
await cli().parse(`purge --out ${out} --no-stale`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Nothing to purge!');
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith('Nothing to purge!');

consoleLogMock.mockReset();
log.mockReset();
await cli().parse(`purge --out ${out} --stale false`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Nothing to purge!');
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith('Nothing to purge!');

consoleLogMock.mockReset();
log.mockReset();
await cli().parse(`purge --out ${out} --stale=false`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Nothing to purge!');
expect(log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledWith('Nothing to purge!');
});
});

describe('when user passes 2.0.1 -y', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions & 2.0.1', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions & 2.0.1', async () => {
await cli().parse(`purge --out ${out} 2.0.1 -y`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];
const purge = ['v2.0.1'];
Expand All @@ -233,16 +212,13 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes --major 1 -y', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions & all but the last major version', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions & all but the last major version', async () => {
await cli().parse(`purge --out ${out} --major 1 -y`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];
const major = ['v1.2.2', 'v1.2.1', 'v1.2.0', 'v1.1.0', 'v1.0.0'];
Expand All @@ -264,16 +240,13 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes --minor 1 -y', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions & all but the last minor version per major version', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions & all but the last minor version per major version', async () => {
await cli().parse(`purge --out ${out} --minor 1 -y`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];
const minor = ['v2.0.1', 'v2.0.0', 'v1.1.0', 'v1.0.0'];
Expand All @@ -295,16 +268,13 @@ describe('when versions = [ "v2.1.0", "v2.0.1", "v2.0.0", "v2.0.0-alpha.1", "v1.
});

describe('when user passes --patch 1 -y', () => {
beforeEach(() => {
consoleLogMock = jest
it('should purge stale versions & all but the last patch version per minor version', async () => {
const log = jest
.spyOn(console, 'log')
.mockImplementation(consoleLogMockImplementation);
});
afterEach(() => consoleLogMock.mockRestore());

it('should purge stale versions & all but the last patch version per minor version', async () => {
await cli().parse(`purge --out ${out} --patch 1 -y`);
expect(console.log).toHaveBeenCalledTimes(1);
expect(log).toHaveBeenCalledTimes(1);

const stale = ['v2.0.0-alpha.1', 'v0.1.0'];
const patch = ['v2.0.0', 'v1.2.1', 'v1.2.0'];
Expand Down
Loading

0 comments on commit 2d6e2c5

Please sign in to comment.