This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): separate test utils from regular utils to prevent npm fro…
…m trying to load dev dependenci
- Loading branch information
Ryan Garant
committed
Mar 25, 2019
1 parent
3cc45e5
commit c50c77e
Showing
4 changed files
with
164 additions
and
165 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters