Skip to content

Commit

Permalink
fix: Upgrade jest dependency to latest 27.0.3
Browse files Browse the repository at this point in the history
- avoids using `afterEach` to reset auto-mocks
  • Loading branch information
steveukx committed Dec 26, 2021
1 parent a569868 commit 89a8739
Show file tree
Hide file tree
Showing 12 changed files with 1,046 additions and 2,015 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
"@babel/preset-typescript": "^7.12.7",
"@kwsites/promise-result": "^1.1.0",
"@types/debug": "^4.1.5",
"@types/jest": "^26.0.15",
"@types/jest": "^27.0.3",
"@types/node": "^14.14.10",
"babel-jest": "^26.6.3",
"babel-jest": "^27.4.5",
"babel-plugin-module-resolver": "^4.0.0",
"jest": "^26.6.3",
"jest": "^27.4.5",
"ts-node": "^9.0.0",
"typescript": "^4.1.2"
},
Expand Down
2 changes: 0 additions & 2 deletions test/__fixtures__/create-test-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const io = {

export async function createTestContext (): Promise<SimpleGitTestContext> {

require('@kwsites/file-exists').$real(true);

const root = await io.mkdtemp();

const context: SimpleGitTestContext = {
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/like.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

export function like<T> (what: T) {
export function like<T> (what: Partial<T>) {
return expect.objectContaining(what);
}
55 changes: 55 additions & 0 deletions test/unit/__fixtures__/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
jest.mock('debug', () => {

function logger (name: string, logs: any) {
logs[name] = logs[name] || [];

return Object.assign((_: string, ...messages: Array<string | unknown>) => {
logs[name].push(messages.filter(m => typeof m === 'string' || Buffer.isBuffer(m)).join(' '));
}, {
extend (suffix: string) {
return debug(`${name}:${suffix}`);
},
get logs () {
return logs;
}
})
}

const debug: any = Object.assign(
jest.fn((name) => {

if (debug.mock.results[0].type === 'return') {
return logger(name, debug.mock.results[0].value.logs);
}

return logger(name, {})
}),
{
formatters: {
H: 'hello-world',
},
});

return debug;
});

function logs (): Record<string, string[]> {
return (require('debug') as jest.Mock).mock.results[0].value.logs;
}

export function $logNames(...matching: RegExp[]) {
return Object.keys(logs()).filter(matches);

function matches(namespace: string) {
return !matching.length || matching.some(regex => regex.test(namespace));
}
}

export function $logMessagesFor(name: string) {
const log = logs()[name];

expect(Array.isArray(log)).toBe(true);

return log.join('\n');
}

13 changes: 13 additions & 0 deletions test/unit/__fixtures__/file-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { exists } from '@kwsites/file-exists';

jest.mock('@kwsites/file-exists', () => ({
exists: jest.fn().mockReturnValue(true),
}));

export function isInvalidDirectory() {
(exists as jest.Mock).mockReturnValue(false);
}

export function isValidDirectory() {
(exists as jest.Mock).mockReturnValue(true);
}
3 changes: 3 additions & 0 deletions test/unit/__fixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

export * from './debug';
export * from './file-exists';

export * from './child-processes';
export * from './expectations';

Expand Down
29 changes: 0 additions & 29 deletions test/unit/__mocks__/@kwsites/file-exists.ts

This file was deleted.

93 changes: 0 additions & 93 deletions test/unit/__mocks__/debug.ts

This file was deleted.

4 changes: 1 addition & 3 deletions test/unit/cwd.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { SimpleGit } from 'typings';
import { assertNoExecutedTasks, newSimpleGit, wait } from './__fixtures__';
import { assertNoExecutedTasks, isInvalidDirectory, isValidDirectory, newSimpleGit, wait } from './__fixtures__';

describe('cwd', () => {

let git: SimpleGit;

const {$fails: isInvalidDirectory, $reset: isValidDirectory} = require('@kwsites/file-exists');

beforeEach(() => {
git = newSimpleGit()
});
Expand Down
18 changes: 13 additions & 5 deletions test/unit/git.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { SimpleGit } from 'typings';
import { autoMergeConflict, autoMergeResponse, closeWithSuccess, newSimpleGit, wait } from './__fixtures__';
import {
autoMergeConflict,
autoMergeResponse,
closeWithSuccess,
isInvalidDirectory,
isValidDirectory,
newSimpleGit,
wait
} from './__fixtures__';

import { GitResponseError } from '../..';
import { createInstanceConfig } from '../../src/lib/utils';
Expand All @@ -8,6 +16,8 @@ describe('git', () => {

let git: SimpleGit;

afterEach(() => jest.clearAllMocks());

describe('deprecations', () => {

it('direct access to properties of custom error on GitResponseError', async () => {
Expand Down Expand Up @@ -77,9 +87,6 @@ describe('git', () => {
describe('simpleGit', () => {

const simpleGit = require('../..');
const {$fails, $reset} = require('@kwsites/file-exists');

afterEach(() => $reset());

it('can be created using the default export', () => {
expect(simpleGit.__esModule).toBe(true);
Expand All @@ -89,11 +96,12 @@ describe('git', () => {
});

it('throws when created with a non-existent directory', () => {
$fails();
isInvalidDirectory();
expect(() => simpleGit('/tmp/foo-bar-baz')).toThrow();
});

it('works with valid directories', () => {
isValidDirectory();
expect(() => simpleGit(__dirname)).not.toThrow();
});

Expand Down
47 changes: 4 additions & 43 deletions test/unit/logging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { closeWithError, closeWithSuccess, newSimpleGit, $logNames, $logMessagesFor } from './__fixtures__';
import { TasksPendingQueue } from '../../src/lib/runners/tasks-pending-queue';

import debug from 'debug';
import { closeWithError, closeWithSuccess, newSimpleGit } from './__fixtures__';

describe('logging', () => {

const {$enable, $debugEnvironmentVariable, $enabled, $logNames, $logMessagesFor} = require('debug');

beforeEach(() => {
$debugEnvironmentVariable('*');
$enabled(true);
});
afterEach(() => (TasksPendingQueue as any).counter = 0);

it('creates a new debug logger for each simpleGit instance', async () => {
(debug as any).mockClear();
Expand Down Expand Up @@ -71,40 +68,4 @@ describe('logging', () => {
expect($logMessagesFor('simple-git:output:clean:2')).toMatch('Removing foo/');
});

it('when logging is wild-card enabled, silent disables the namespace', async () => {
newSimpleGit().silent(true);
expect($enable).toHaveBeenCalledWith('*,-simple-git');
});

it('when logging is wild-card enabled, non-silent does nothing', async () => {
newSimpleGit().silent(false);
expect($enable).not.toHaveBeenCalled();
});

it('when logging is explicitly enabled, silent removes the namespace', async () => {
$debugEnvironmentVariable('another,simple-git,other');
newSimpleGit().silent(true);
expect($enable).toHaveBeenCalledWith('another,other');
});

it('when logging is explicitly enabled, non-silent does nothing', async () => {
$debugEnvironmentVariable('another,simple-git,other');
newSimpleGit().silent(false);
expect($enable).not.toHaveBeenCalled();
});

it('when logging is explicitly disabled, silent does nothing', async () => {
$debugEnvironmentVariable('*,-simple-git,-other');
$enabled(false);
newSimpleGit().silent(true);
expect($enable).not.toHaveBeenCalled();
});

it('when logging is explicitly disabled, non-silent does nothing', async () => {
$debugEnvironmentVariable('*,-simple-git,-other');
$enabled(false);
newSimpleGit().silent(false);
expect($enable).toHaveBeenCalledWith('*,-other');
});

});
Loading

0 comments on commit 89a8739

Please sign in to comment.