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

add init option #6442

Merged
merged 8 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## master

### Features

- `[jest-cli]` Add `jest --init` option that generates a basic configuration file with a short description for each option ([#6442](https://github.com/facebook/jest/pull/6442))

### Fixes

- `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428))
Expand Down
4 changes: 4 additions & 0 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ Force Jest to exit after all tests have completed running. This is useful when r

Show the help information, similar to this page.

### `--init`

Generate a basic configuration file. Based on your project, Jest will ask you a few questions that will help to generate a `jest.config.js` file with a short description for each option.

### `--json`

Prints the test results in JSON. This mode will send all other test output and user messages to stderr.
Expand Down
8 changes: 8 additions & 0 deletions docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ If you'd like to learn more about running `jest` through the command line, take

## Additional Configuration

### Generate a basic configuration file

Based on your project, Jest will ask you a few questions and will create a basic configuration file with a short description for each option:

```bash
jest --init
```

### Using Babel

To use [Babel](http://babeljs.io/), install the `babel-jest` and `regenerator-runtime` packages:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"/packages/.*/src/__tests__/expect_util.js",
"/packages/jest-cli/src/__tests__/test_root",
"/packages/jest-cli/src/__tests__/__fixtures__/",
"/packages/jest-cli/src/lib/__tests__/fixtures/",
"/packages/jest-haste-map/src/__tests__/haste_impl.js",
"/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/",
"/packages/jest-runtime/src/__tests__/defaultResolver.js",
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"jest-worker": "^23.0.1",
"micromatch": "^2.3.11",
"node-notifier": "^5.2.1",
"prompts": "^0.1.9",
"realpath-native": "^1.0.0",
"rimraf": "^2.5.4",
"slash": "^1.0.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ export const options = {
'A JSON string with map of variables for the haste module system',
type: 'string',
},
init: {
description: 'Generate a basic configuration file',
type: 'boolean',
},
json: {
default: undefined,
description:
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ import pluralize from '../pluralize';
import yargs from 'yargs';
import rimraf from 'rimraf';
import {sync as realpath} from 'realpath-native';
import init from '../lib/init';

export async function run(maybeArgv?: Argv, project?: Path) {
try {
const argv: Argv = buildArgv(maybeArgv, project);

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

const projects = getProjectListFromCLIArgs(argv, project);

const {results, globalConfig} = await runCLI(argv, projects);
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-cli/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const ICONS = {
pending: '\u25CB',
success: isWindows ? '\u221A' : '\u2713',
};
export const PACKAGE_JSON = 'package.json';
export const JEST_CONFIG = 'jest.config.js';
252 changes: 252 additions & 0 deletions packages/jest-cli/src/lib/__tests__/__snapshots__/init.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`init has jest config in package.json should ask the user whether to override config or not 1`] = `
Object {
"initial": true,
"message": "It seems that you already have a jest configuration, do you want to override it?",
"name": "continue",
"type": "confirm",
}
`;

exports[`init has-jest-config-file ask the user whether to override config or not user answered with "Yes" 1`] = `
Object {
"initial": true,
"message": "It seems that you already have a jest configuration, do you want to override it?",
"name": "continue",
"type": "confirm",
}
`;

exports[`init project with package.json and no jest config all questions answered with answer: "No" should return the default configuration (an empty config) 1`] = `
"// For a detailed explanation regarding each configuration property, visit:
// https://facebook.github.io/jest/docs/en/configuration.html

module.exports = {
// All imported modules in your tests should be mocked automatically
// automock: false,

// Stop running tests after the first failure
// bail: false,

// Respect \\"browser\\" field in package.json when resolving modules
// browser: false,

// The directory where Jest should store its cached dependency information
// cacheDirectory: \\"/tmp/jest\\",

// Automatically clear mock calls and instances between every test
// clearMocks: false,

// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,

// An array of glob patterns indicating a set of files for which coverage information should be collected
// collectCoverageFrom: null,

// The directory where Jest should output its coverage files
// coverageDirectory: null,

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// \\"/node_modules/\\"
// ],

// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// \\"json\\",
// \\"text\\",
// \\"lcov\\",
// \\"clover\\"
// ],

// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: null,

// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,

// Force coverage collection from ignored files usin a array of glob patterns
// forceCoverageMatch: [],

// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: null,

// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: null,

// A set of global variables that need to be available in all test environments
// globals: {},

// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// \\"node_modules\\"
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// \\"js\\",
// \\"json\\",
// \\"jsx\\",
// \\"node\\"
// ],

// A map from regular expressions to module names that allow to stub out resources with a single module
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],

// Activates notifications for test results
// notify: false,

// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: \\"always\\",

// A preset that is used as a base for Jest's configuration
// preset: null,

// Run tests from one or more projects
// projects: null,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,

// Automatically reset mock state between every test
// resetMocks: false,

// Reset the module registry before running each individual test
// resetModules: false,

// A path to a custom resolver
// resolver: null,

// Automatically restore mock state between every test
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: null,

// A list of paths to directories that Jest should use to search for files in
// roots: [
// \\"<rootDir>\\"
// ],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: \\"jest-runner\\",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],

// The path to a module that runs some code to configure or set up the testing framework before each test
// setupTestFrameworkScriptFile: null,

// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],

// The test environment that will be used for testing
// testEnvironment: \\"jest-environment-jsdom\\",

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},

// Adds a location field to test results
// testLocationInResults: false,

// The glob patterns Jest uses to detect test files
// testMatch: [
// \\"**/__tests__/**/*.js?(x)\\",
// \\"**/?(*.)+(spec|test).js?(x)\\"
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// \\"/node_modules/\\"
// ],

// The regexp pattern Jest uses to detect test files
// testRegex: \\"\\",

// This option allows the use of a custom results processor
// testResultsProcessor: null,

// This option allows use of a custom test runner
// testRunner: \\"jasmine2\\",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: \\"about:blank\\",

// Setting this value to \\"fake\\" allows the use of fake timers for functions such as \\"setTimeout\\"
// timers: \\"real\\",

// A map from regular expressions to paths to transformers
// transform: null,

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// \\"/node_modules/\\"
// ],

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,

// Indicates whether each individual test should be reported during the run
// verbose: null,

// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],

// Whether to use watchman for file crawling
// watchman: true,
};
"
`;

exports[`init project with package.json and no jest config some questions answered with answer: "Yes" should create package.json with configured test command when {scripts: true} 1`] = `
"{
\\"name\\": \\"only_package_json\\",
\\"scripts\\": {
\\"test\\": \\"jest\\"
}
}
"
`;

exports[`init typescript project should ask "typescript question" when has typescript in dependencies 1`] = `
Object {
"initial": true,
"message": "Typescript detected, would you like to setup Jest for Typescript?",
"name": "typescript",
"type": "confirm",
}
`;

exports[`init typescript project should ask "typescript question" when has typescript in devDependencies 1`] = `
Object {
"initial": true,
"message": "Typescript detected, would you like to setup Jest for Typescript?",
"name": "typescript",
"type": "confirm",
}
`;

exports[`init typescript project should create configuration for {typescript: true} 1`] = `
Object {
"globals": Object {
"ts-jest": Object {
"tsConfigFile": "tsconfig.json",
},
},
"moduleFileExtensions": Array [
"ts",
"tsx",
"js",
],
"testMatch": Array [
"**/__tests__/*.+(ts|tsx|js)",
],
"transform": Object {
"^.+\\\\.(ts|tsx)$": "ts-jest",
},
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should add test script when there are no scripts 1`] = `
"{
\\"scripts\\": {
\\"test\\": \\"jest\\"
}
}
"
`;

exports[`should add test script when there are scripts 1`] = `
"{
\\"scripts\\": {
\\"lint\\": \\"eslint .\\",
\\"test\\": \\"jest\\"
}
}
"
`;

exports[`should not add test script when { shouldModifyScripts: false } 1`] = `
"{}
"
`;

exports[`should remove jest config if exists 1`] = `
"{
\\"scripts\\": {
\\"test\\": \\"jest\\"
}
}
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "has_jest_config_in_package_json",
"jest": {
"coverage": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "only_package_json",
"scripts": {
"test": "different-test-runner"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test_script_configured",
"scripts": {
"test": "jest"
}
}
Loading