Skip to content

Commit d9b324e

Browse files
ranyitzcpojer
authored andcommitted
add init option (#6442)
* add init option * for windows snapshot compatibility, mock the sep before the tests * refactor generateConfigFile function * support a case when jest is already configured in test script * update documentation with the jest --init option * update changelog * update prompts version * upgrade prompts in yarn.lock
1 parent ed95b36 commit d9b324e

File tree

31 files changed

+1109
-14
lines changed

31 files changed

+1109
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## master
22

3+
### Features
4+
5+
- `[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))
6+
37
### Fixes
48

59
- `[jest-cli]` Pass `globalConfig` as a parameter to `globalSetup` and `globalTeardown` functions ([#6486](https://github.com/facebook/jest/pull/6486))

docs/CLI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ Force Jest to exit after all tests have completed running. This is useful when r
169169

170170
Show the help information, similar to this page.
171171

172+
### `--init`
173+
174+
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.
175+
172176
### `--json`
173177

174178
Prints the test results in JSON. This mode will send all other test output and user messages to stderr.

docs/GettingStarted.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ If you'd like to learn more about running `jest` through the command line, take
6969

7070
## Additional Configuration
7171

72+
### Generate a basic configuration file
73+
74+
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:
75+
76+
```bash
77+
jest --init
78+
```
79+
7280
### Using Babel
7381

7482
To use [Babel](http://babeljs.io/), install the `babel-jest` and `regenerator-runtime` packages:

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
"/packages/.*/src/__tests__/expect_util.js",
148148
"/packages/jest-cli/src/__tests__/test_root",
149149
"/packages/jest-cli/src/__tests__/__fixtures__/",
150+
"/packages/jest-cli/src/lib/__tests__/fixtures/",
150151
"/packages/jest-haste-map/src/__tests__/haste_impl.js",
151152
"/packages/jest-resolve-dependencies/src/__tests__/__fixtures__/",
152153
"/packages/jest-runtime/src/__tests__/defaultResolver.js",

packages/jest-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"jest-worker": "^23.0.1",
3333
"micromatch": "^2.3.11",
3434
"node-notifier": "^5.2.1",
35+
"prompts": "^0.1.9",
3536
"realpath-native": "^1.0.0",
3637
"rimraf": "^2.5.4",
3738
"slash": "^1.0.0",

packages/jest-cli/src/cli/args.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ export const options = {
294294
'A JSON string with map of variables for the haste module system',
295295
type: 'string',
296296
},
297+
init: {
298+
description: 'Generate a basic configuration file',
299+
type: 'boolean',
300+
},
297301
json: {
298302
default: undefined,
299303
description:

packages/jest-cli/src/cli/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@ import pluralize from '../pluralize';
3232
import yargs from 'yargs';
3333
import rimraf from 'rimraf';
3434
import {sync as realpath} from 'realpath-native';
35+
import init from '../lib/init';
3536

3637
export async function run(maybeArgv?: Argv, project?: Path) {
3738
try {
3839
const argv: Argv = buildArgv(maybeArgv, project);
40+
41+
if (argv.init) {
42+
await init();
43+
return;
44+
}
45+
3946
const projects = getProjectListFromCLIArgs(argv, project);
4047

4148
const {results, globalConfig} = await runCLI(argv, projects);

packages/jest-cli/src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ export const ICONS = {
1616
pending: '\u25CB',
1717
success: isWindows ? '\u221A' : '\u2713',
1818
};
19+
export const PACKAGE_JSON = 'package.json';
20+
export const JEST_CONFIG = 'jest.config.js';
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`init has jest config in package.json should ask the user whether to override config or not 1`] = `
4+
Object {
5+
"initial": true,
6+
"message": "It seems that you already have a jest configuration, do you want to override it?",
7+
"name": "continue",
8+
"type": "confirm",
9+
}
10+
`;
11+
12+
exports[`init has-jest-config-file ask the user whether to override config or not user answered with "Yes" 1`] = `
13+
Object {
14+
"initial": true,
15+
"message": "It seems that you already have a jest configuration, do you want to override it?",
16+
"name": "continue",
17+
"type": "confirm",
18+
}
19+
`;
20+
21+
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`] = `
22+
"// For a detailed explanation regarding each configuration property, visit:
23+
// https://facebook.github.io/jest/docs/en/configuration.html
24+
25+
module.exports = {
26+
// All imported modules in your tests should be mocked automatically
27+
// automock: false,
28+
29+
// Stop running tests after the first failure
30+
// bail: false,
31+
32+
// Respect \\"browser\\" field in package.json when resolving modules
33+
// browser: false,
34+
35+
// The directory where Jest should store its cached dependency information
36+
// cacheDirectory: \\"/tmp/jest\\",
37+
38+
// Automatically clear mock calls and instances between every test
39+
// clearMocks: false,
40+
41+
// Indicates whether the coverage information should be collected while executing the test
42+
// collectCoverage: false,
43+
44+
// An array of glob patterns indicating a set of files for which coverage information should be collected
45+
// collectCoverageFrom: null,
46+
47+
// The directory where Jest should output its coverage files
48+
// coverageDirectory: null,
49+
50+
// An array of regexp pattern strings used to skip coverage collection
51+
// coveragePathIgnorePatterns: [
52+
// \\"/node_modules/\\"
53+
// ],
54+
55+
// A list of reporter names that Jest uses when writing coverage reports
56+
// coverageReporters: [
57+
// \\"json\\",
58+
// \\"text\\",
59+
// \\"lcov\\",
60+
// \\"clover\\"
61+
// ],
62+
63+
// An object that configures minimum threshold enforcement for coverage results
64+
// coverageThreshold: null,
65+
66+
// Make calling deprecated APIs throw helpful error messages
67+
// errorOnDeprecated: false,
68+
69+
// Force coverage collection from ignored files usin a array of glob patterns
70+
// forceCoverageMatch: [],
71+
72+
// A path to a module which exports an async function that is triggered once before all test suites
73+
// globalSetup: null,
74+
75+
// A path to a module which exports an async function that is triggered once after all test suites
76+
// globalTeardown: null,
77+
78+
// A set of global variables that need to be available in all test environments
79+
// globals: {},
80+
81+
// An array of directory names to be searched recursively up from the requiring module's location
82+
// moduleDirectories: [
83+
// \\"node_modules\\"
84+
// ],
85+
86+
// An array of file extensions your modules use
87+
// moduleFileExtensions: [
88+
// \\"js\\",
89+
// \\"json\\",
90+
// \\"jsx\\",
91+
// \\"node\\"
92+
// ],
93+
94+
// A map from regular expressions to module names that allow to stub out resources with a single module
95+
// moduleNameMapper: {},
96+
97+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
98+
// modulePathIgnorePatterns: [],
99+
100+
// Activates notifications for test results
101+
// notify: false,
102+
103+
// An enum that specifies notification mode. Requires { notify: true }
104+
// notifyMode: \\"always\\",
105+
106+
// A preset that is used as a base for Jest's configuration
107+
// preset: null,
108+
109+
// Run tests from one or more projects
110+
// projects: null,
111+
112+
// Use this configuration option to add custom reporters to Jest
113+
// reporters: undefined,
114+
115+
// Automatically reset mock state between every test
116+
// resetMocks: false,
117+
118+
// Reset the module registry before running each individual test
119+
// resetModules: false,
120+
121+
// A path to a custom resolver
122+
// resolver: null,
123+
124+
// Automatically restore mock state between every test
125+
// restoreMocks: false,
126+
127+
// The root directory that Jest should scan for tests and modules within
128+
// rootDir: null,
129+
130+
// A list of paths to directories that Jest should use to search for files in
131+
// roots: [
132+
// \\"<rootDir>\\"
133+
// ],
134+
135+
// Allows you to use a custom runner instead of Jest's default test runner
136+
// runner: \\"jest-runner\\",
137+
138+
// The paths to modules that run some code to configure or set up the testing environment before each test
139+
// setupFiles: [],
140+
141+
// The path to a module that runs some code to configure or set up the testing framework before each test
142+
// setupTestFrameworkScriptFile: null,
143+
144+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
145+
// snapshotSerializers: [],
146+
147+
// The test environment that will be used for testing
148+
// testEnvironment: \\"jest-environment-jsdom\\",
149+
150+
// Options that will be passed to the testEnvironment
151+
// testEnvironmentOptions: {},
152+
153+
// Adds a location field to test results
154+
// testLocationInResults: false,
155+
156+
// The glob patterns Jest uses to detect test files
157+
// testMatch: [
158+
// \\"**/__tests__/**/*.js?(x)\\",
159+
// \\"**/?(*.)+(spec|test).js?(x)\\"
160+
// ],
161+
162+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
163+
// testPathIgnorePatterns: [
164+
// \\"/node_modules/\\"
165+
// ],
166+
167+
// The regexp pattern Jest uses to detect test files
168+
// testRegex: \\"\\",
169+
170+
// This option allows the use of a custom results processor
171+
// testResultsProcessor: null,
172+
173+
// This option allows use of a custom test runner
174+
// testRunner: \\"jasmine2\\",
175+
176+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
177+
// testURL: \\"about:blank\\",
178+
179+
// Setting this value to \\"fake\\" allows the use of fake timers for functions such as \\"setTimeout\\"
180+
// timers: \\"real\\",
181+
182+
// A map from regular expressions to paths to transformers
183+
// transform: null,
184+
185+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
186+
// transformIgnorePatterns: [
187+
// \\"/node_modules/\\"
188+
// ],
189+
190+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
191+
// unmockedModulePathPatterns: undefined,
192+
193+
// Indicates whether each individual test should be reported during the run
194+
// verbose: null,
195+
196+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
197+
// watchPathIgnorePatterns: [],
198+
199+
// Whether to use watchman for file crawling
200+
// watchman: true,
201+
};
202+
"
203+
`;
204+
205+
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`] = `
206+
"{
207+
\\"name\\": \\"only_package_json\\",
208+
\\"scripts\\": {
209+
\\"test\\": \\"jest\\"
210+
}
211+
}
212+
"
213+
`;
214+
215+
exports[`init typescript project should ask "typescript question" when has typescript in dependencies 1`] = `
216+
Object {
217+
"initial": true,
218+
"message": "Typescript detected, would you like to setup Jest for Typescript?",
219+
"name": "typescript",
220+
"type": "confirm",
221+
}
222+
`;
223+
224+
exports[`init typescript project should ask "typescript question" when has typescript in devDependencies 1`] = `
225+
Object {
226+
"initial": true,
227+
"message": "Typescript detected, would you like to setup Jest for Typescript?",
228+
"name": "typescript",
229+
"type": "confirm",
230+
}
231+
`;
232+
233+
exports[`init typescript project should create configuration for {typescript: true} 1`] = `
234+
Object {
235+
"globals": Object {
236+
"ts-jest": Object {
237+
"tsConfigFile": "tsconfig.json",
238+
},
239+
},
240+
"moduleFileExtensions": Array [
241+
"ts",
242+
"tsx",
243+
"js",
244+
],
245+
"testMatch": Array [
246+
"**/__tests__/*.+(ts|tsx|js)",
247+
],
248+
"transform": Object {
249+
"^.+\\\\.(ts|tsx)$": "ts-jest",
250+
},
251+
}
252+
`;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should add test script when there are no scripts 1`] = `
4+
"{
5+
\\"scripts\\": {
6+
\\"test\\": \\"jest\\"
7+
}
8+
}
9+
"
10+
`;
11+
12+
exports[`should add test script when there are scripts 1`] = `
13+
"{
14+
\\"scripts\\": {
15+
\\"lint\\": \\"eslint .\\",
16+
\\"test\\": \\"jest\\"
17+
}
18+
}
19+
"
20+
`;
21+
22+
exports[`should not add test script when { shouldModifyScripts: false } 1`] = `
23+
"{}
24+
"
25+
`;
26+
27+
exports[`should remove jest config if exists 1`] = `
28+
"{
29+
\\"scripts\\": {
30+
\\"test\\": \\"jest\\"
31+
}
32+
}
33+
"
34+
`;

0 commit comments

Comments
 (0)