Skip to content

Commit

Permalink
✅ Add tests for project infos module
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck committed Jun 10, 2019
1 parent 34d475c commit aae1bc6
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 9 deletions.
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = {
collectCoverageFrom: ['./src/**/*.js', '!**/node_modules/**', '!**/vendor/**']
collectCoverageFrom: [
'./src/**/*.js',
'!./src/index.js',
'!**/node_modules/**',
'!**/vendor/**'
]
}
14 changes: 6 additions & 8 deletions src/project-infos.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const getProjectName = require('project-name')
const isNil = require('lodash/isNil')
const get = require('lodash/get')
const util = require('util')
const ora = require('ora')
const exec = util.promisify(require('child_process').exec)
const { execSync } = require('child_process')

const { getPackageJson } = require('./utils')

Expand Down Expand Up @@ -33,10 +32,10 @@ const getReposUrlFromPackageJson = async packageJson => {
/**
* Get repository url from git
*/
const getReposUrlFromGit = async () => {
const getReposUrlFromGit = () => {
try {
const result = await exec('git config --get remote.origin.url')
return cleanReposUrl(result.stdout)
const stdout = execSync('git config --get remote.origin.url')
return cleanReposUrl(stdout)
} catch (err) {
return undefined
}
Expand All @@ -48,8 +47,7 @@ const getReposUrlFromGit = async () => {
* @param {Object} packageJson
*/
const getReposUrl = async packageJson =>
(await getReposUrlFromPackageJson(packageJson)) ||
(await getReposUrlFromGit())
(await getReposUrlFromPackageJson(packageJson)) || getReposUrlFromGit()

/**
* Get repository issues url from package.json or git
Expand Down Expand Up @@ -101,7 +99,7 @@ const getProjectInfos = async () => {
const spinner = ora('Gathering project infos').start()

const packageJson = await getPackageJson()
const name = getProjectName() || undefined
const name = getProjectName()
const description = get(packageJson, 'description', undefined)
const engines = get(packageJson, 'engines', undefined)
const author = get(packageJson, 'author', undefined)
Expand Down
257 changes: 257 additions & 0 deletions src/project-infos.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
const ora = require('ora')
const childProcess = require('child_process')

const utils = require('./utils')
const { getProjectInfos } = require('./project-infos')

jest.mock('ora')
jest.mock('child_process', () => ({
execSync: jest.fn()
}))
jest.mock('./utils', () => ({
getPackageJson: jest.fn()
}))

const succeed = jest.fn()
const fail = jest.fn()

ora.mockReturnValue({
start: () => ({
succeed,
fail
})
})

describe('projectInfos', () => {
describe('getProjectInfos', () => {
it('should call ora with correct parameters', async () => {
await getProjectInfos()

expect(ora).toHaveBeenCalledTimes(1)
expect(ora).toHaveBeenCalledWith('Gathering project infos')
expect(succeed).toHaveBeenCalledTimes(1)
expect(succeed).toHaveBeenCalledWith('Project infos gathered')
})

it('should return correct infos', async () => {
const packgeJsonInfos = {
name: 'readme-md-generator',
version: '0.1.3',
description: 'CLI that generates beautiful README.md files.',
author: 'Franck Abgrall',
license: 'MIT',
homepage: 'https://github.com/kefranabg/readme-md-generator#readme',
repository: {
type: 'git',
url: 'git+https://github.com/kefranabg/readme-md-generator.git'
},
bugs: {
url: 'https://github.com/kefranabg/readme-md-generator/issues'
},
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
}
}
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(packgeJsonInfos))
childProcess.execSync.mockReturnValue(
'https://github.com/kefranabg/readme-md-generator.git'
)

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: 'CLI that generates beautiful README.md files.',
version: '0.1.3',
author: 'Franck Abgrall',
repositoryUrl: 'https://github.com/kefranabg/readme-md-generator',
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/issues',
githubUsername: 'kefranabg',
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
},
licenseName: 'MIT',
licenseUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE',
documentationUrl:
'https://github.com/kefranabg/readme-md-generator#readme',
isGithubRepos: true
})
})

it('should return correct infos when repos is not github', async () => {
const packgeJsonInfos = {
name: 'readme-md-generator',
version: '0.1.3',
description: 'CLI that generates beautiful README.md files.',
author: 'Franck Abgrall',
license: 'MIT',
homepage: 'https://gitlab.com/kefranabg/readme-md-generator#readme',
repository: {
type: 'git',
url: 'git+https://gitlab.com/kefranabg/readme-md-generator.git'
},
bugs: {
url: 'https://gitlab.com/kefranabg/readme-md-generator/issues'
},
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
}
}
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(packgeJsonInfos))
childProcess.execSync.mockReturnValue(
'https://github.com/kefranabg/readme-md-generator.git'
)

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: 'CLI that generates beautiful README.md files.',
version: '0.1.3',
author: 'Franck Abgrall',
repositoryUrl: 'https://gitlab.com/kefranabg/readme-md-generator',
contributingUrl:
'https://gitlab.com/kefranabg/readme-md-generator/issues',
githubUsername: undefined,
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
},
licenseName: 'MIT',
licenseUrl: undefined,
documentationUrl:
'https://gitlab.com/kefranabg/readme-md-generator#readme',
isGithubRepos: false
})
})

it('should return correct infos when package.json is not defined', async () => {
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(undefined))
childProcess.execSync.mockReturnValue(
'https://github.com/kefranabg/readme-md-generator.git'
)

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: undefined,
version: undefined,
author: undefined,
repositoryUrl: 'https://github.com/kefranabg/readme-md-generator',
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/issues',
githubUsername: 'kefranabg',
engines: undefined,
licenseName: undefined,
licenseUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE',
documentationUrl: undefined,
isGithubRepos: true
})
})

it('should return correct infos when repos is not github and package.json are not defined', async () => {
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(undefined))
childProcess.execSync.mockReturnValue(
'https://gitlab.com/kefranabg/readme-md-generator.git'
)

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: undefined,
version: undefined,
author: undefined,
repositoryUrl: 'https://gitlab.com/kefranabg/readme-md-generator',
contributingUrl:
'https://gitlab.com/kefranabg/readme-md-generator/issues',
githubUsername: undefined,
engines: undefined,
licenseName: undefined,
licenseUrl: undefined,
documentationUrl: undefined,
isGithubRepos: false
})
})

it('should return correct infos when git config and package.json are not defined', async () => {
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(undefined))
childProcess.execSync.mockImplementation(() => {
throw new Error('error')
})

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: undefined,
version: undefined,
author: undefined,
repositoryUrl: undefined,
contributingUrl: undefined,
githubUsername: undefined,
engines: undefined,
licenseName: undefined,
licenseUrl: undefined,
documentationUrl: undefined,
isGithubRepos: false
})
})

it('should return correct infos when git config is not defined', async () => {
const packgeJsonInfos = {
name: 'readme-md-generator',
version: '0.1.3',
description: 'CLI that generates beautiful README.md files.',
author: 'Franck Abgrall',
license: 'MIT',
homepage: 'https://github.com/kefranabg/readme-md-generator#readme',
repository: {
type: 'git',
url: 'git+https://github.com/kefranabg/readme-md-generator.git'
},
bugs: {
url: 'https://github.com/kefranabg/readme-md-generator/issues'
},
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
}
}
utils.getPackageJson.mockReturnValueOnce(Promise.resolve(packgeJsonInfos))
childProcess.execSync.mockImplementation(() => {
throw new Error('error')
})

const projectInfos = await getProjectInfos()

expect(projectInfos).toEqual({
name: 'readme-md-generator',
description: 'CLI that generates beautiful README.md files.',
version: '0.1.3',
author: 'Franck Abgrall',
repositoryUrl: 'https://github.com/kefranabg/readme-md-generator',
contributingUrl:
'https://github.com/kefranabg/readme-md-generator/issues',
githubUsername: 'kefranabg',
engines: {
npm: '>=5.5.0',
node: '>=9.3.0'
},
licenseName: 'MIT',
licenseUrl:
'https://github.com/kefranabg/readme-md-generator/blob/master/LICENSE',
documentationUrl:
'https://github.com/kefranabg/readme-md-generator#readme',
isGithubRepos: true
})
})
})
})
21 changes: 21 additions & 0 deletions src/questions/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const questions = require('./')

describe('questions', () => {
it('should export questions in the correct order', () => {
const questionsNameOrder = Object.keys(questions)

expect(questionsNameOrder).toEqual([
'askProjectName',
'askProjectVersion',
'askProjectDescription',
'askProjectDocumentationUrl',
'askAuhtorName',
'askAuthorGithub',
'askAuthorTwitter',
'askProjectPrerequisites',
'askLicenseName',
'askLicenseUrl',
'askContributing'
])
})
})

0 comments on commit aae1bc6

Please sign in to comment.