Skip to content

Commit

Permalink
refactor(information): Refactor information step test (#33)
Browse files Browse the repository at this point in the history
- Refactor mocks
- Add missing docs
- Remove commented out code
  • Loading branch information
dominique-mueller committed Sep 23, 2017
1 parent e7ae2e4 commit 528ed66
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 77 deletions.
48 changes: 27 additions & 21 deletions src/steps/information.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,32 @@ import { setupWriteFileMock } from '../../test/write-file.mock';
*/
describe( 'Collect information', () => {

beforeEach( () => {

jest.resetModules();
process.env.GH_TOKEN = 'ABCDE777';
beforeAll( () => {

// Setup mocks & spies
// Hide logging output
jest.spyOn( console, 'log' ).mockImplementation( () => {
return;
} );

} );

beforeEach( () => {

jest.resetModules();
process.env.GH_TOKEN = 'ABCDE777';

} );

afterEach( () => {

delete process.env.GH_TOKEN;

} );

it ( 'should collection information for first version', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
const readFileSpy = jest.fn();
Expand Down Expand Up @@ -59,7 +65,7 @@ describe( 'Collect information', () => {
it ( 'should collect information for updated version', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, false );
setupSimpleGitMock( false, false, false, true );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
const readFileSpy = jest.fn();
Expand Down Expand Up @@ -88,7 +94,7 @@ describe( 'Collect information', () => {
it ( 'should assume "1.0.0" as the initial version (if not defined otherwhise)', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-no-version' ) );
Expand All @@ -107,10 +113,10 @@ describe( 'Collect information', () => {
it ( 'should derive repository information from Git (if necessary)', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-repository' ) );
setupReadFileMock( getPackageJson( 'invalid-no-repository' ) );
const writeFileSpy = jest.fn();
setupWriteFileMock( writeFileSpy );

Expand All @@ -126,7 +132,7 @@ describe( 'Collect information', () => {
it ( 'should throw an error when using an invalid version', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-wrong-version' ) );
Expand All @@ -149,7 +155,7 @@ describe( 'Collect information', () => {
it ( 'should throw an error when using a pre-release version', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-prerelease-version' ) );
Expand All @@ -172,10 +178,10 @@ describe( 'Collect information', () => {
it ( 'should throw an error when deriving repository information fails', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, true, false, true );
setupSimpleGitMock( false, true, false, false );
setupGithubMock( true );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-repository' ) );
setupReadFileMock( getPackageJson( 'invalid-no-repository' ) );
setupWriteFileMock();

const collectInformation = await import( './information' );
Expand All @@ -195,10 +201,10 @@ describe( 'Collect information', () => {
it ( 'should throw an error when deriving repository information fails, due to missing remotes', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, true, true );
setupSimpleGitMock( false, false, true, false );
setupGithubMock( true );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'invalid-repository' ) );
setupReadFileMock( getPackageJson( 'invalid-no-repository' ) );
setupWriteFileMock();

const collectInformation = await import( './information' );
Expand All @@ -218,7 +224,7 @@ describe( 'Collect information', () => {
it ( 'should throw an error when retrieving git tags fails', async() => {

// Setup mocks & spies
setupSimpleGitMock( true, false, false, true );
setupSimpleGitMock( true, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'valid-initial' ) );
Expand All @@ -241,7 +247,7 @@ describe( 'Collect information', () => {
it ( 'should throw an error when evaluating the recommended next version fails', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, false );
setupSimpleGitMock( false, false, false, true );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( true );
setupReadFileMock( getPackageJson( 'valid-update' ) );
Expand All @@ -265,7 +271,7 @@ describe( 'Collect information', () => {

// Setup mocks & spies
delete process.env.GH_TOKEN; // Unset again
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( false );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'valid-initial' ) );
Expand All @@ -288,7 +294,7 @@ describe( 'Collect information', () => {
it ( 'should throw an error when the GitHub API token is invalid', async() => {

// Setup mocks & spies
setupSimpleGitMock( false, false, false, true );
setupSimpleGitMock( false, false, false, false );
setupGithubMock( true );
setupConventionalRecommendedBumpMock( false );
setupReadFileMock( getPackageJson( 'valid-initial' ) );
Expand Down Expand Up @@ -371,7 +377,7 @@ function getPackageJson( type: string ): PackageJson {
}
};

case 'invalid-repository':
case 'invalid-no-repository':
return {
name: 'test-library',
description: 'Lorem ipsum dolor sit amet.',
Expand Down
4 changes: 2 additions & 2 deletions test/github.mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Setup GitHub mock
*/
export function setupGithubMock( shouldFail: boolean = false ): void {
export function setupGithubMock( shouldGetCollaboratorsFail: boolean = false ): void {

jest.doMock( 'github', () => {
return function( options ) {
Expand All @@ -11,7 +11,7 @@ export function setupGithubMock( shouldFail: boolean = false ): void {
},
repos: {
getCollaborators: ( params, callback: ( error: any | null, collaborators: any ) => void ) => {
if ( shouldFail ) {
if ( shouldGetCollaboratorsFail ) {
callback( {
headers: {
status: '500'
Expand Down
72 changes: 18 additions & 54 deletions test/simple-git.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,38 @@ import { GitTags } from '../src/interfaces/git-tags.interface';

/**
* Setup simple git mock
*
* @param [shouldTagsFail=false] - Flag, describing whether getting tags should fail
* @param [shouldGetRemotesFail=false] - Flag, describing whether getting remotes should fail
* @param [shouldGetRemotesBeEmpty=false] - Flag, describing whether the list of remotes should be empty
* @param [initial=true] - Flag, describing whether the mock should return tags or not
*/
export function setupSimpleGitMock(
shouldTagsFail: boolean = false,
shouldGetRemotesFail: boolean = false,
shouldGetRemotesByEmpty: boolean = false,
initial: boolean = true
shouldGetRemotesBeEmpty: boolean = false,
hasTags: boolean = true
): void {

jest.doMock( 'simple-git', () => {
return ( basePath: string ) => {

const api: any = {};

if ( initial ) {
if ( hasTags ) {

api.tags = ( callback: ( gitTagsError: Error | null, gitTags: GitTags ) => void ) => {
if ( shouldTagsFail ) {
callback( new Error( 'An error occured.' ), null );
} else {
callback( null, {
latest: undefined,
all: []
latest: '1.1.0',
all: [
'1.0.0',
'1.0.1',
'1.0.2',
'1.1.0'
]
} );
}
}
Expand All @@ -36,13 +46,8 @@ export function setupSimpleGitMock(
callback( new Error( 'An error occured.' ), null );
} else {
callback( null, {
latest: '1.1.0',
all: [
'1.0.0',
'1.0.1',
'1.0.2',
'1.1.0'
]
latest: undefined,
all: []
} );
}
}
Expand All @@ -52,7 +57,7 @@ export function setupSimpleGitMock(
api.getRemotes = ( verbose: boolean, callback: ( gitGetRemotesError: Error | null, gitRemotes: Array<GitRemote> ) => void ) => {
if ( shouldGetRemotesFail ) {
callback( new Error( 'An error occured.' ), null );
} else if ( shouldGetRemotesByEmpty ) {
} else if ( shouldGetRemotesBeEmpty ) {
callback( null, [ {
name: '',
refs: {}
Expand All @@ -75,44 +80,3 @@ export function setupSimpleGitMock(
} );

}

// export function setupSimpleGitMock( initial: boolean = true ): any {

// if ( initial ) {

// return jest.doMock( 'simple-git', () => {
// return ( basePath: string ) => {
// return {
// tags: ( callback: ( gitTagsError: Error | null, gitTags: GitTags ) => void ) => {
// callback( null, {
// latest: undefined,
// all: []
// } );
// }
// };
// };
// } );

// } else {

// return jest.doMock( 'simple-git', () => {
// return ( basePath: string ) => {
// return {
// tags: ( callback: ( gitTagsError: Error | null, gitTags: GitTags ) => void ) => {
// callback( null, {
// latest: '1.1.0',
// all: [
// '1.0.0',
// '1.0.1',
// '1.0.2',
// '1.1.0'
// ]
// } );
// }
// };
// };
// } );

// }

// }

0 comments on commit 528ed66

Please sign in to comment.