Skip to content

Commit

Permalink
Use jest-resolve to resolve reporters
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronabramov committed May 2, 2017
1 parent 8dec8ba commit 19168b9
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Unexpected value for Path at index 0 of reporter at index 0
Got:
number
Reporters config:
[ 3243242 ]
[
3243242
]
Configuration Documentation:
https://facebook.github.io/jest/docs/configuration.html
Expand Down
10 changes: 7 additions & 3 deletions packages/jest-cli/src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ const throat = require('throat');
const workerFarm = require('worker-farm');
const TestWatcher = require('./TestWatcher');
const ReporterDispatcher = require('./ReporterDispatcher');
const chalk = require('chalk');
const {DEFAULT_REPORTER_LABEL} = require('jest-config');

const DEFAULT_REPORTER_LABEL = 'default';
const SLOW_TEST_TIME = 3000;

class CancelRun extends Error {
Expand Down Expand Up @@ -279,7 +278,12 @@ class TestRunner {
}

_shouldAddDefaultReporters(reporters?: Array<ReporterConfig>): boolean {
return !reporters || reporters.indexOf(DEFAULT_REPORTER_LABEL) !== -1;
return (
!reporters ||
!!reporters.find(
reporterConfig => reporterConfig[0] === DEFAULT_REPORTER_LABEL,
)
);
}

_setupReporters() {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ const path = require('path');

exports.NODE_MODULES = path.sep + 'node_modules' + path.sep;
exports.DEFAULT_JS_PATTERN = '^.+\\.jsx?$';
exports.DEFAULT_REPORTER_LABEL = 'default';
2 changes: 2 additions & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const loadFromPackage = require('./loadFromPackage');
const normalize = require('./normalize');
const setFromArgv = require('./setFromArgv');
const {getTestEnvironment} = require('./utils');
const {DEFAULT_REPORTER_LABEL} = require('./constants');

async function readConfig(
argv: Object,
Expand Down Expand Up @@ -135,6 +136,7 @@ const getConfigs = (
};

module.exports = {
DEFAULT_REPORTER_LABEL,
getTestEnvironment,
normalize,
readConfig,
Expand Down
58 changes: 52 additions & 6 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {InitialOptions} from 'types/Config';
import type {InitialOptions, ReporterConfig} from 'types/Config';

This comment has been minimized.

Copy link
@thymikee

thymikee May 2, 2017

Collaborator

Shouldn't this land in types/Reporters?

This comment has been minimized.

Copy link
@aaronabramov

aaronabramov May 2, 2017

Author Contributor

this is slightly different. There are two parts here:

  1. Configuration of reporters
  2. Implementation of reporters

the first one is a JSON data structure in the shape of ['reporterName, ['otherReporterName', {option: 1}]]

the second one is reporter implementation methods like onRunStart, onRunComplete, etc and their arguments.

I want to clean it up in future diffs though


const {
BULLET,
Expand All @@ -20,7 +20,11 @@ const {
getTestEnvironment,
resolve,
} = require('./utils');
const {NODE_MODULES, DEFAULT_JS_PATTERN} = require('./constants');
const {
NODE_MODULES,
DEFAULT_JS_PATTERN,
DEFAULT_REPORTER_LABEL,
} = require('./constants');
const {validateReporters} = require('./reporterValidationErrors');
const {ValidationError, validate} = require('jest-validate');
const chalk = require('chalk');
Expand Down Expand Up @@ -249,17 +253,59 @@ const normalizeArgv = (options: InitialOptions, argv: Object) => {
}
};

const normalizeReporters = (options: InitialOptions, basedir) => {
if (options.reporters && Array.isArray(options.reporters)) {
validateReporters(options.reporters);

if (!options.reporters) {

This comment has been minimized.

Copy link
@thymikee

thymikee May 2, 2017

Collaborator

Does validateReporters mutate options? if not, this check will never be reached.

return options;
}

options.reporters = options.reporters.map(reporterConfig => {
const normalizedReporterConfig: ReporterConfig = typeof reporterConfig ===
'string'
? // if reporter config is a string, we wrap it in an array
// and pass an empty object for options argument, to make
// them have the same shape.
[reporterConfig, {}]
: reporterConfig;

const reporterPath = _replaceRootDirInPath(
options.rootDir,
normalizedReporterConfig[0],
);

if (reporterPath !== DEFAULT_REPORTER_LABEL) {
const resolvedReporterPath = Resolver.findNodeModule(reporterPath, {
basedir: options.rootDir,
});

if (!resolvedReporterPath) {
throw new Error(
`
Could not resolve a module for a custom reporter.
moduleName: ${reporterPath}
`,
);
}

normalizedReporterConfig[0] = resolvedReporterPath;
}
return normalizedReporterConfig;
});
}

return options;
};

function normalize(options: InitialOptions, argv: Object = {}) {
const {hasDeprecationWarnings} = validate(options, {
comment: DOCUMENTATION_NOTE,
deprecatedConfig: DEPRECATED_CONFIG,
exampleConfig: VALID_CONFIG,
});

if (options.reporters && Array.isArray(options.reporters)) {
validateReporters(options.reporters);
}

normalizeReporters(options);
normalizePreprocessor(options);
normalizeRootDir(options);
normalizeMissingOptions(options);
Expand Down
16 changes: 9 additions & 7 deletions packages/jest-config/src/reporterValidationErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

import type {ReporterConfig, StringReporter, ArrayReporter} from 'types/Config';
import type {ReporterConfig} from 'types/Config';

const {ValidationError} = require('jest-validate');
const {DOCUMENTATION_NOTE, BULLET} = require('./utils');
Expand All @@ -30,7 +30,7 @@ const ERROR = `${BULLET} Reporter Validation Error`;
*/
function createReporterError(
reporterIndex: number,
reporterValue: Array<ReporterConfig> | StringReporter,
reporterValue: Array<ReporterConfig> | string,
): ValidationError {
const errorMessage =
`Reporter at index ${reporterIndex} must be of type:\n` +
Expand All @@ -42,10 +42,10 @@ function createReporterError(
}

function createArrayReporterError(
arrayReporter: ArrayReporter,
arrayReporter: ReporterConfig,
reporterIndex: number,
valueIndex: number,
value: StringReporter | Object,
value: string | Object,
expectedType: string,
valueName: string,
): ValidationError {
Expand All @@ -57,14 +57,16 @@ function createArrayReporterError(
' Got:\n' +
` ${chalk.bold.green(getType(value))}\n` +
` Reporters config:\n` +
` ${chalk.bold.green(JSON.stringify(arrayReporter, null, 2)
` ${chalk.bold.green(JSON.stringify(arrayReporter, null, 2)
.split('\n')
.join('\n '))}`;

return new ValidationError(ERROR, errorMessage, DOCUMENTATION_NOTE);
}

function validateReporters(reporterConfig: Array<ReporterConfig>): boolean {
function validateReporters(
reporterConfig: Array<ReporterConfig | string>,
): boolean {
return reporterConfig.every((reporter, index) => {
if (Array.isArray(reporter)) {
validateArrayReporter(reporter, index);
Expand All @@ -77,7 +79,7 @@ function validateReporters(reporterConfig: Array<ReporterConfig>): boolean {
}

function validateArrayReporter(
arrayReporter: ArrayReporter,
arrayReporter: ReporterConfig,
reporterIndex: number,
) {
const [path, options] = arrayReporter;
Expand Down
6 changes: 2 additions & 4 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export type HasteConfig = {|
providesModuleNodeModules: Array<string>,
|};

export type ReporterConfig = StringReporter | ArrayReporter;
export type StringReporter = string;
export type ArrayReporter = [string, Object];
export type ReporterConfig = [string, Object];

export type ConfigGlobals = Object;

Expand Down Expand Up @@ -79,7 +77,7 @@ export type InitialOptions = {|
forceExit?: boolean,
globals?: ConfigGlobals,
haste?: HasteConfig,
reporters?: Array<ReporterConfig>,
reporters?: Array<ReporterConfig | string>,
logHeapUsage?: boolean,
mapCoverage?: boolean,
moduleDirectories?: Array<string>,
Expand Down

0 comments on commit 19168b9

Please sign in to comment.