Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow config.transform[regex] to be a function for jest.runCLI(config) #7398

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ca97ebf
add support for argv.config as an object to readConfig
jharris4 Nov 21, 2018
2d27b56
only normalize config.transform if it is a string
jharris4 Nov 21, 2018
100017e
preprocess using the transform if it is a function else require it
jharris4 Nov 21, 2018
a48205a
add normalize unit tests for transform as a function
jharris4 Nov 21, 2018
61e9fdd
fix formatting for lint / flow
jharris4 Nov 21, 2018
40f39f7
add unit test for preprocessor transform as a function vs filepath
jharris4 Nov 21, 2018
848585f
guard against customJSTransformer.includes is not a function error
jharris4 Nov 21, 2018
3bb6d6e
add unit test for runCLI function with standalone config argument
jharris4 Nov 21, 2018
bbf105e
remove typo added to make a passing test fail
jharris4 Nov 21, 2018
6f9d9cf
cosmetic - prettier formatting
jharris4 Nov 22, 2018
829d5da
add required facebook headers for text fixture files
jharris4 Nov 22, 2018
565c83b
fix test assertion by sorting keys before comparing equality
jharris4 Nov 22, 2018
e30e7b7
update snapshot for script_transformer_test
jharris4 Nov 22, 2018
4667163
remove outdated snapshot for script_transformer_test
jharris4 Nov 22, 2018
4ae1e95
cli/run function - add param for optional exit and return results
jharris4 Nov 24, 2018
42e1cd2
add tests calling jest-cli run() with string vs object parameters
jharris4 Nov 24, 2018
bd032a6
remove run exit parameter and add better error message when yargs throws
jharris4 Nov 24, 2018
3aa417a
split run & runCLI tests and cleanup console output for nested tests
jharris4 Nov 24, 2018
458bb67
misc lint fixes
jharris4 Nov 24, 2018
f7fccaa
rename variable
jharris4 Nov 24, 2018
4aa187e
lint fixes for run function returning results
jharris4 Nov 24, 2018
df6adfd
fix flow error on run function
jharris4 Nov 24, 2018
e48fd77
always mock process.exit for cli run function tests
jharris4 Nov 24, 2018
3791c47
Update packages/jest-cli/src/cli/index.js
SimenB Dec 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/jest-cli/src/__tests__/__fixtures__/run/aFile.js
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

module.exports = {
toReplace: 1,
};
18 changes: 18 additions & 0 deletions packages/jest-cli/src/__tests__/__fixtures__/run/aFile_spec.js
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

const expect = require('expect');
const aFile = require('./aFile');

describe('aFile test', () => {
it('should have transformed aFile', () => {
expect(JSON.stringify(aFile)).toEqual(JSON.stringify({runReplaced: 1}));
});
});
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

module.exports = {
process: (src, filename) => src.replace('toReplace', 'runReplaced'),
};
13 changes: 13 additions & 0 deletions packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile.js
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

module.exports = {
toReplace: 1,
};
18 changes: 18 additions & 0 deletions packages/jest-cli/src/__tests__/__fixtures__/runCLI/aFile_spec.js
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

const expect = require('expect');
const aFile = require('./aFile');

describe('aFile test', () => {
it('should have transformed aFile', () => {
expect(JSON.stringify(aFile)).toEqual(JSON.stringify({runCLIReplaced: 1}));
});
});
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

module.exports = {
process: (src, filename) => src.replace('toReplace', 'runCLIReplaced'),
};
99 changes: 99 additions & 0 deletions packages/jest-cli/src/__tests__/cli/run.test.js
@@ -0,0 +1,99 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

import path from 'path';
import {run} from '../../cli';

const runProject = path.join(__dirname, '../__fixtures__/run');

jest.mock('exit');

const runArgvString = [
'--config',
JSON.stringify({
rootDir: runProject,
testMatch: ['<rootDir>/*_spec.js'],
transform: {
'^.+\\.jsx?$': './transform-module',
},
}),
];

const runArgvObject = [
'--config',
{
rootDir: runProject,
testMatch: ['<rootDir>/*_spec.js'],
transform: {
'^.+\\.jsx?$': './transform-module',
},
},
];

const processOnFn = process.on;
const processExitFn = process.exit;
const processErrWriteFn = process.stderr.write;
const consoleErrorFn = console.error;

const noSubTestLogs = true;

describe('run', () => {
beforeEach(() => {
process.on = jest.fn();
process.on.mockReset();
process.exit = jest.fn();
process.exit.mockReset();
if (noSubTestLogs) {
process.stderr.write = jest.fn();
process.stderr.write.mockReset();
console.error = jest.fn();
console.error.mockReset();
}
});

afterEach(() => {
process.on = processOnFn;
process.exit = processExitFn;
if (noSubTestLogs) {
process.stderr.write = processErrWriteFn;
console.error = consoleErrorFn;
}
});

describe('config as string', () => {
it('passes the test when the config has a transform module path', async () => {
let runResult = null;
let error = null;
try {
runResult = await run(runArgvString, runProject);
} catch (ex) {
error = ex;
}
const numPassedTests = runResult ? runResult.numPassedTests : -1;
expect(error).toBe(null);
expect(numPassedTests).toBe(1);
});
});

describe('config as object', () => {
it('throws running the test when the config is an object', async () => {
let runResult = null;
let error = null;
try {
runResult = await run(runArgvObject, runProject);
} catch (ex) {
error = ex;
}
const numPassedTests = runResult ? runResult.numPassedTests : -1;
expect(error).not.toBe(null);
expect(numPassedTests).toBe(-1);
});
});
});
84 changes: 84 additions & 0 deletions packages/jest-cli/src/__tests__/cli/runCLI.test.js
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

'use strict';

import path from 'path';
import {runCLI} from '../../cli';
import transformModule from '../__fixtures__/runCLI/transform-module';

const project = path.join(__dirname, '../__fixtures__/runCLI');
const projects = [project];

const argvObject = {
config: {
testMatch: ['<rootDir>/*_spec.js'],
transform: {
'^.+\\.jsx?$': () => transformModule,
},
},
};

const argvString = {
config: JSON.stringify({
rootDir: project,
testMatch: ['<rootDir>/*_spec.js'],
transform: {
'^.+\\.jsx?$': './transform-module',
},
}),
};

const processErrWriteFn = process.stderr.write;

const noSubTestLogs = true;

describe('runCLI', () => {
beforeEach(() => {
if (noSubTestLogs) {
process.stderr.write = jest.fn();
process.stderr.write.mockReset();
}
});

afterEach(() => {
if (noSubTestLogs) {
process.stderr.write = processErrWriteFn;
}
});

describe('config as object', () => {
it('passes the test when the config has a transform function', async () => {
let runResult = null;
let error = null;
try {
runResult = await runCLI(argvObject, projects);
} catch (ex) {
error = ex;
}
const numPassedTests = runResult ? runResult.results.numPassedTests : -1;
expect(error).toBe(null);
expect(numPassedTests).toBe(1);
});
});

describe('config as string', () => {
it('passes the test when the config is a string', async () => {
let runResult = null;
let error = null;
try {
runResult = await runCLI(argvString, projects);
} catch (ex) {
error = ex;
}
const numPassedTests = runResult ? runResult.results.numPassedTests : -1;
expect(error).toBe(null);
expect(numPassedTests).toBe(1);
});
});
});
44 changes: 28 additions & 16 deletions packages/jest-cli/src/cli/index.js
Expand Up @@ -33,18 +33,22 @@ import {sync as realpath} from 'realpath-native';
import init from '../lib/init';
import logDebugMessages from '../lib/log_debug_messages';

export async function run(maybeArgv?: Argv, project?: Path) {
export const run = async (
maybeArgv?: Argv,
project?: Path,
): Promise<?AggregatedResult> => {
let results, globalConfig;
try {
const argv: Argv = buildArgv(maybeArgv, project);

if (argv.init) {
await init();
return;
return results;
}

const projects = getProjectListFromCLIArgs(argv, project);

const {results, globalConfig} = await runCLI(argv, projects);
({results, globalConfig} = await runCLI(argv, projects));
readResultsAndExit(results, globalConfig);
} catch (error) {
clearLine(process.stderr);
Expand All @@ -53,7 +57,8 @@ export async function run(maybeArgv?: Argv, project?: Path) {
exit(1);
throw error;
}
}
return Promise.resolve(results);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Promise.resolve(results);
return results;

};

export const runCLI = async (
argv: Argv,
Expand Down Expand Up @@ -170,19 +175,26 @@ const readResultsAndExit = (
};

const buildArgv = (maybeArgv: ?Argv, project: ?Path) => {
const argv: Argv = yargs(maybeArgv || process.argv.slice(2))
.usage(args.usage)
.alias('help', 'h')
.options(args.options)
.epilogue(args.docs)
.check(args.check).argv;

validateCLIOptions(
argv,
Object.assign({}, args.options, {deprecationEntries}),
);
try {
const argv: Argv = yargs(maybeArgv || process.argv.slice(2))
.usage(args.usage)
.alias('help', 'h')
.options(args.options)
.epilogue(args.docs)
.check(args.check).argv;

validateCLIOptions(
argv,
Object.assign({}, args.options, {deprecationEntries}),
);

return argv;
return argv;
} catch (err) {
if (maybeArgv) {
throw new Error('Command line arguments should be an array of strings');
}
throw err;
}
};

const getProjectListFromCLIArgs = (argv, project: ?Path) => {
Expand Down