Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
fix(utils): separate test utils from regular utils to prevent npm fro…
Browse files Browse the repository at this point in the history
…m trying to load dev dependenci
  • Loading branch information
Ryan Garant committed Mar 25, 2019
1 parent 3cc45e5 commit c50c77e
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 165 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export function setUp() {
invokePayload(options, Command, cooked, remain)

if (process.env.NODE_ENV === 'testing') {
const { prepareTestFixtures } = await import('./utils')
const { prepareTestFixtures } = await import('./test-utils')

await new Command(options).run(prepareTestFixtures(Command.name, cooked))
} else {
Expand Down
162 changes: 162 additions & 0 deletions src/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
* (see file: CONTRIBUTORS)
* SPDX-License-Identifier: BSD-3-Clause
*/

import { isArray, isPlainObject, map, mapValues, upperFirst } from 'lodash'
import * as nock from 'nock'

const nockBack = nock.back

export function prepareTestFixtures(cmdName, argv) {
let id = 0

// These should only include the flags that you need for e2e tests
const cmds = [
{
name: 'Issue',
flags: ['--comment', '--new', '--open', '--close', '--search', '--assign'],
},
{
name: 'PullRequest',
flags: [
'--detailed',
'--info',
'--fetch',
'--comment',
'--open',
'--close',
'--submit',
],
},
{
name: 'Gists',
flags: ['--new', '--fork', '--delete'],
},
{
name: 'Milestone',
flags: ['--list'],
},
{
name: 'Notifications',
},
{
name: 'Repo',
flags: ['--list', '--new', '--fork', '--delete'],
},
{
name: 'User',
flags: ['--logout', '--whoami'],
},
{
name: 'Version',
flags: ['--version'],
},
].filter(cmd => filterByCmdName(cmd, cmdName))

const newCmdName = formatCmdName(cmds[0], argv)

nockBack.fixtures = `${process.cwd()}/__tests__/nockFixtures`
nockBack.setMode('record')

const nockPromise = nockBack(`${newCmdName}.json`, {
before,
afterRecord,
})

return () =>
nockPromise
.then(({ nockDone }) => nockDone())
.catch(err => {
throw new Error(`Nock ==> ${err}`)
})

/* --- Normalization Functions --- */

function normalize(value, key) {
if (!value) return value

if (isPlainObject(value)) {
return mapValues(value, normalize)
}

if (isArray(value) && isPlainObject(value[0])) {
return map(value, normalize)
}

if (key.includes('_at') || key.includes('_on')) {
return '2017-10-10T16:00:00Z'
}

if (key.includes('_count')) {
return 42
}

if (key.includes('id')) {
return 1000 + id++
}

if (key.includes('node_id')) {
return 'MDA6RW50aXR5MQ=='
}

if (key.includes('url')) {
return value.replace(/[1-9][0-9]{2,10}/, '000000001')
}

return value
}

function afterRecord(fixtures) {
const normalizedFixtures = fixtures.map(fixture => {
delete fixture.rawHeaders

fixture.path = stripAccessToken(fixture.path)

if (isArray(fixture.response)) {
fixture.response = fixture.response.slice(0, 3).map(res => {
return mapValues(res, normalize)
})
} else {
fixture.response = mapValues(fixture.response, normalize)
}

return fixture
})

return normalizedFixtures
}

function stripAccessToken(path) {
return path.replace(/access_token(.*?)(&|$)/, '')
}

function before(scope) {
scope.filteringPath = () => stripAccessToken(scope.path)
}
}

function filterByCmdName(cmd, cmdName) {
return cmd.name === cmdName
}

function formatCmdName(cmd, argv) {
if (argv.length === 1) {
return cmd.name
}

return cmd.flags.reduce((flagName, current) => {
if (flagName) {
return flagName
}

if (argv.includes(current)) {
return concatUpper(cmd.name, current.slice(2))
}
}, null)
}

function concatUpper(one, two) {
return `${one}${upperFirst(two)}`
}
163 changes: 0 additions & 163 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,166 +1,3 @@
/**
* © 2013 Liferay, Inc. <https://liferay.com> and Node GH contributors
* (see file: CONTRIBUTORS)
* SPDX-License-Identifier: BSD-3-Clause
*/

import { isArray, isPlainObject, map, mapValues, upperFirst } from 'lodash'
import * as nock from 'nock'

const nockBack = nock.back

export function prepareTestFixtures(cmdName, argv) {
let id = 0

// These should only include the flags that you need for e2e tests
const cmds = [
{
name: 'Issue',
flags: ['--comment', '--new', '--open', '--close', '--search', '--assign'],
},
{
name: 'PullRequest',
flags: [
'--detailed',
'--info',
'--fetch',
'--comment',
'--open',
'--close',
'--submit',
],
},
{
name: 'Gists',
flags: ['--new', '--fork', '--delete'],
},
{
name: 'Milestone',
flags: ['--list'],
},
{
name: 'Notifications',
},
{
name: 'Repo',
flags: ['--list', '--new', '--fork', '--delete'],
},
{
name: 'User',
flags: ['--logout', '--whoami'],
},
{
name: 'Version',
flags: ['--version'],
},
].filter(cmd => filterByCmdName(cmd, cmdName))

const newCmdName = formatCmdName(cmds[0], argv)

nockBack.fixtures = `${process.cwd()}/__tests__/nockFixtures`
nockBack.setMode('record')

const nockPromise = nockBack(`${newCmdName}.json`, {
before,
afterRecord,
})

return () =>
nockPromise
.then(({ nockDone }) => nockDone())
.catch(err => {
throw new Error(`Nock ==> ${err}`)
})

/* --- Normalization Functions --- */

function normalize(value, key) {
if (!value) return value

if (isPlainObject(value)) {
return mapValues(value, normalize)
}

if (isArray(value) && isPlainObject(value[0])) {
return map(value, normalize)
}

if (key.includes('_at') || key.includes('_on')) {
return '2017-10-10T16:00:00Z'
}

if (key.includes('_count')) {
return 42
}

if (key.includes('id')) {
return 1000 + id++
}

if (key.includes('node_id')) {
return 'MDA6RW50aXR5MQ=='
}

if (key.includes('url')) {
return value.replace(/[1-9][0-9]{2,10}/, '000000001')
}

return value
}

function afterRecord(fixtures) {
const normalizedFixtures = fixtures.map(fixture => {
delete fixture.rawHeaders

fixture.path = stripAccessToken(fixture.path)

if (isArray(fixture.response)) {
fixture.response = fixture.response.slice(0, 3).map(res => {
return mapValues(res, normalize)
})
} else {
fixture.response = mapValues(fixture.response, normalize)
}

return fixture
})

return normalizedFixtures
}

function stripAccessToken(path) {
return path.replace(/access_token(.*?)(&|$)/, '')
}

function before(scope) {
scope.filteringPath = () => stripAccessToken(scope.path)
}
}

function filterByCmdName(cmd, cmdName) {
return cmd.name === cmdName
}

function formatCmdName(cmd, argv) {
if (argv.length === 1) {
return cmd.name
}

return cmd.flags.reduce((flagName, current) => {
if (flagName) {
return flagName
}

if (argv.includes(current)) {
return concatUpper(cmd.name, current.slice(2))
}
}, null)
}

function concatUpper(one, two) {
return `${one}${upperFirst(two)}`
}

export function getCurrentFolderName(): string {
const cwdArr = process
.cwd()
Expand Down

0 comments on commit c50c77e

Please sign in to comment.