Skip to content

Commit

Permalink
tests: leverage it.each to clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
tdd committed Jun 22, 2018
1 parent cfa476e commit e09f018
Showing 1 changed file with 84 additions and 102 deletions.
186 changes: 84 additions & 102 deletions packages/jest-cli/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,105 +396,83 @@ describe('Watch mode flows', () => {
});
});

it('allows WatchPlugins to modify only white-listed global config keys', async () => {
const pluginPath = `${__dirname}/__fixtures__/plugin_path_config_updater`;
const config = Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
});

let currentOption;

jest.doMock(
pluginPath,
() =>
class WatchPlugin {
getUsageInfo() {
return {key: 'x', prompt: 'test option white-listing'};
}

run(globalConfig, updateConfigAndRun) {
updateConfigAndRun({[currentOption]: '__JUST_TRYING__'});
return Promise.resolve();
}
},
{virtual: true},
);

// updateGlobalConfig.mockReset();
const whiteListedOptions = [
'bail',
'collectCoverage',
'collectCoverageFrom',
'collectCoverageOnlyFrom',
'coverageDirectory',
'coverageReporters',
'notify',
'notifyMode',
'onlyFailures',
'reporters',
'testNamePattern',
'testPathPattern',
'updateSnapshot',
'verbose',
];

for (const whiteListedOption of whiteListedOptions) {
currentOption = whiteListedOption;

watch(config, contexts, pipe, hasteMapInstances, stdin);
await nextTick();

stdin.emit('x');
await nextTick();

const lastCall = updateGlobalConfig.mock.calls.slice(-1)[0];
expect(lastCall[0]).toMatchObject({
[whiteListedOption]: '__JUST_TRYING__',
it.each`
ok | option
✔︎ | bail
✖︎ | changedFilesWithAncestor
✖︎ | changedSince
✔︎ | collectCoverage
✔︎ | collectCoverageFrom
✔︎ | collectCoverageOnlyFrom
✔︎ | coverageDirectory
✔︎ | coverageReporters
✖︎ | coverageThreshold
✖︎ | detectLeaks
✖︎ | detectOpenHandles
✖︎ | enabledTestsMap
✖︎ | errorOnDeprecated
✖︎ | expand
✖︎ | filter
✖︎ | findRelatedTests
✖︎ | forceExit
✖︎ | globalSetup
✖︎ | globalTeardown
✖︎ | json
✖︎ | lastCommit
✖︎ | listTests
✖︎ | logHeapUsage
✖︎ | maxWorkers
✖︎ | nonFlagArgs
✖︎ | noSCM
✖︎ | noStackTrace
✔︎ | notify
✔︎ | notifyMode
✖︎ | onlyChanged
✔︎ | onlyFailures
✖︎ | outputFile
✖︎ | passWithNoTests
✖︎ | projects
✖︎ | replname
✔︎ | reporters
✖︎ | rootDir
✖︎ | runTestsByPath
✖︎ | silent
✖︎ | skipFilter
✖︎ | testFailureExitCode
✔︎ | testNamePattern
✔︎ | testPathPattern
✖︎ | testResultsProcessor
✔︎ | updateSnapshot
✖︎ | useStderr
✔︎ | verbose
✖︎ | watch
✖︎ | watchAll
✖︎ | watchman
✖︎ | watchPlugins
`(
'allows WatchPlugins to modify only white-listed global config keys',
async ({ok, option}) => {
const pluginPath = `${__dirname}/__fixtures__/plugin_path_config_updater`;
const config = Object.assign({}, globalConfig, {
rootDir: __dirname,
watchPlugins: [pluginPath],
});
}

const exampleForbiddenOptions = [
'changedSince',
'changedFilesWithAncestor',
'coverageThreshold',
'detectLeaks',
'detectOpenHandles',
'enabledTestsMap',
'expand',
'filter',
'findRelatedTests',
'forceExit',
'json',
'globalSetup',
'globalTeardown',
'lastCommit',
'logHeapUsage',
'listTests',
'maxWorkers',
'noStackTrace',
'nonFlagArgs',
'noSCM',
'outputFile',
'onlyChanged',
'passWithNoTests',
'projects',
'replname',
'runTestsByPath',
'rootDir',
'silent',
'skipFilter',
'errorOnDeprecated',
'testFailureExitCode',
'testResultsProcessor',
'useStderr',
'watch',
'watchAll',
'watchman',
'watchPlugins',
];
for (const forbiddenOption of exampleForbiddenOptions) {
currentOption = forbiddenOption;

jest.doMock(
pluginPath,
() =>
class WatchPlugin {
getUsageInfo() {
return {key: 'x', prompt: 'test option white-listing'};
}

run(globalConfig, updateConfigAndRun) {
updateConfigAndRun({[option]: '__JUST_TRYING__'});
return Promise.resolve();
}
},
{virtual: true},
);

watch(config, contexts, pipe, hasteMapInstances, stdin);
await nextTick();
Expand All @@ -503,11 +481,15 @@ describe('Watch mode flows', () => {
await nextTick();

const lastCall = updateGlobalConfig.mock.calls.slice(-1)[0];
expect(lastCall[0]).not.toMatchObject({
[forbiddenOption]: '__JUST_TRYING__',
let expector = expect(lastCall[0]);
if (!ok) {
expector = expector.not;
}
expector.toMatchObject({
[option]: '__JUST_TRYING__',
});
}
});
},
);

it('triggers enter on a WatchPlugin when its key is pressed', async () => {
const run = jest.fn(() => Promise.resolve());
Expand Down

0 comments on commit e09f018

Please sign in to comment.