create-jest-mine 29.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # create-jest
2
+
3
+ > Getting started with Jest with a single command
4
+
5
+ ```bash
6
+ npm init jest@latest
7
+ # Or for Yarn
8
+ yarn create jest
9
+ # Or for pnpm
10
+ pnpm create jest
11
+ ```
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ require('..').runCLI();
@@ -0,0 +1,31 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ exports.NotFoundPackageJsonError = exports.MalformedPackageJsonError = void 0;
7
+ /**
8
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
9
+ *
10
+ * This source code is licensed under the MIT license found in the
11
+ * LICENSE file in the root directory of this source tree.
12
+ */
13
+
14
+ class NotFoundPackageJsonError extends Error {
15
+ constructor(rootDir) {
16
+ super(`Could not find a "package.json" file in ${rootDir}`);
17
+ this.name = '';
18
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
19
+ Error.captureStackTrace(this, () => {});
20
+ }
21
+ }
22
+ exports.NotFoundPackageJsonError = NotFoundPackageJsonError;
23
+ class MalformedPackageJsonError extends Error {
24
+ constructor(packageJsonPath) {
25
+ super(`There is malformed json in ${packageJsonPath}`);
26
+ this.name = '';
27
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
28
+ Error.captureStackTrace(this, () => {});
29
+ }
30
+ }
31
+ exports.MalformedPackageJsonError = MalformedPackageJsonError;
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _jestConfig() {
8
+ const data = require('jest-config');
9
+ _jestConfig = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ /**
15
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
16
+ *
17
+ * This source code is licensed under the MIT license found in the
18
+ * LICENSE file in the root directory of this source tree.
19
+ */
20
+
21
+ const stringifyOption = (option, map, linePrefix = '') => {
22
+ const description = _jestConfig().descriptions[option];
23
+ const optionDescription =
24
+ description != null && description.length > 0 ? ` // ${description}` : '';
25
+ const stringifiedObject = `${option}: ${JSON.stringify(
26
+ map[option],
27
+ null,
28
+ 2
29
+ )}`;
30
+ return `${optionDescription}\n${stringifiedObject
31
+ .split('\n')
32
+ .map(line => ` ${linePrefix}${line}`)
33
+ .join('\n')},`;
34
+ };
35
+ const generateConfigFile = (results, generateEsm = false) => {
36
+ const {useTypescript, coverage, coverageProvider, clearMocks, environment} =
37
+ results;
38
+ const overrides = {};
39
+ if (coverage) {
40
+ Object.assign(overrides, {
41
+ collectCoverage: true,
42
+ coverageDirectory: 'coverage'
43
+ });
44
+ }
45
+ if (coverageProvider === 'v8') {
46
+ Object.assign(overrides, {
47
+ coverageProvider: 'v8'
48
+ });
49
+ }
50
+ if (environment === 'jsdom') {
51
+ Object.assign(overrides, {
52
+ testEnvironment: 'jsdom'
53
+ });
54
+ }
55
+ if (clearMocks) {
56
+ Object.assign(overrides, {
57
+ clearMocks: true
58
+ });
59
+ }
60
+ const overrideKeys = Object.keys(overrides);
61
+ const properties = [];
62
+ for (const option in _jestConfig().descriptions) {
63
+ const opt = option;
64
+ if (overrideKeys.includes(opt)) {
65
+ properties.push(stringifyOption(opt, overrides));
66
+ } else {
67
+ properties.push(stringifyOption(opt, _jestConfig().defaults, '// '));
68
+ }
69
+ }
70
+ const configHeaderMessage = `/**
71
+ * For a detailed explanation regarding each configuration property, visit:
72
+ * https://jestjs.io/docs/configuration
73
+ */
74
+ `;
75
+ const jsDeclaration = `/** @type {import('jest').Config} */
76
+ const config = {`;
77
+ const tsDeclaration = `import type {Config} from 'jest';
78
+
79
+ const config: Config = {`;
80
+ const cjsExport = 'module.exports = config;';
81
+ const esmExport = 'export default config;';
82
+ return [
83
+ configHeaderMessage,
84
+ useTypescript ? tsDeclaration : jsDeclaration,
85
+ properties.join('\n\n'),
86
+ '};\n',
87
+ useTypescript || generateEsm ? esmExport : cjsExport,
88
+ ''
89
+ ].join('\n');
90
+ };
91
+ var _default = generateConfigFile;
92
+ exports.default = _default;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ export declare function runCLI(): Promise<void>;
9
+
10
+ export declare function runCreate(rootDir?: string): Promise<void>;
11
+
12
+ export {};
package/build/index.js ADDED
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, 'runCLI', {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _runCreate.runCLI;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, 'runCreate', {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _runCreate.runCreate;
16
+ }
17
+ });
18
+ var _runCreate = require('./runCreate');
@@ -0,0 +1,26 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ /**
8
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
9
+ *
10
+ * This source code is licensed under the MIT license found in the
11
+ * LICENSE file in the root directory of this source tree.
12
+ */
13
+
14
+ const modifyPackageJson = ({projectPackageJson, shouldModifyScripts}) => {
15
+ if (shouldModifyScripts) {
16
+ projectPackageJson.scripts
17
+ ? (projectPackageJson.scripts.test = 'jest')
18
+ : (projectPackageJson.scripts = {
19
+ test: 'jest'
20
+ });
21
+ }
22
+ delete projectPackageJson.jest;
23
+ return `${JSON.stringify(projectPackageJson, null, 2)}\n`;
24
+ };
25
+ var _default = modifyPackageJson;
26
+ exports.default = _default;
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ exports.testScriptQuestion = exports.default = void 0;
7
+ /**
8
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
9
+ *
10
+ * This source code is licensed under the MIT license found in the
11
+ * LICENSE file in the root directory of this source tree.
12
+ */
13
+
14
+ const defaultQuestions = [
15
+ {
16
+ initial: false,
17
+ message: 'Would you like to use Typescript for the configuration file?',
18
+ name: 'useTypescript',
19
+ type: 'confirm'
20
+ },
21
+ {
22
+ choices: [
23
+ {
24
+ title: 'node',
25
+ value: 'node'
26
+ },
27
+ {
28
+ title: 'jsdom (browser-like)',
29
+ value: 'jsdom'
30
+ }
31
+ ],
32
+ initial: 0,
33
+ message: 'Choose the test environment that will be used for testing',
34
+ name: 'environment',
35
+ type: 'select'
36
+ },
37
+ {
38
+ initial: false,
39
+ message: 'Do you want Jest to add coverage reports?',
40
+ name: 'coverage',
41
+ type: 'confirm'
42
+ },
43
+ {
44
+ choices: [
45
+ {
46
+ title: 'v8',
47
+ value: 'v8'
48
+ },
49
+ {
50
+ title: 'babel',
51
+ value: 'babel'
52
+ }
53
+ ],
54
+ initial: 0,
55
+ message: 'Which provider should be used to instrument code for coverage?',
56
+ name: 'coverageProvider',
57
+ type: 'select'
58
+ },
59
+ {
60
+ initial: false,
61
+ message:
62
+ 'Automatically clear mock calls, instances, contexts and results before every test?',
63
+ name: 'clearMocks',
64
+ type: 'confirm'
65
+ }
66
+ ];
67
+ var _default = defaultQuestions;
68
+ exports.default = _default;
69
+ const testScriptQuestion = {
70
+ initial: true,
71
+ message:
72
+ 'Would you like to use Jest when running "test" script in "package.json"?',
73
+ name: 'scripts',
74
+ type: 'confirm'
75
+ };
76
+ exports.testScriptQuestion = testScriptQuestion;
@@ -0,0 +1,237 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', {
4
+ value: true
5
+ });
6
+ exports.runCLI = runCLI;
7
+ exports.runCreate = runCreate;
8
+ function path() {
9
+ const data = _interopRequireWildcard(require('path'));
10
+ path = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _chalk() {
16
+ const data = _interopRequireDefault(require('chalk'));
17
+ _chalk = function () {
18
+ return data;
19
+ };
20
+ return data;
21
+ }
22
+ function _exit() {
23
+ const data = _interopRequireDefault(require('exit'));
24
+ _exit = function () {
25
+ return data;
26
+ };
27
+ return data;
28
+ }
29
+ function fs() {
30
+ const data = _interopRequireWildcard(require('graceful-fs'));
31
+ fs = function () {
32
+ return data;
33
+ };
34
+ return data;
35
+ }
36
+ function _prompts() {
37
+ const data = _interopRequireDefault(require('prompts'));
38
+ _prompts = function () {
39
+ return data;
40
+ };
41
+ return data;
42
+ }
43
+ function _jestConfig() {
44
+ const data = require('jest-config');
45
+ _jestConfig = function () {
46
+ return data;
47
+ };
48
+ return data;
49
+ }
50
+ function _jestUtil() {
51
+ const data = require('jest-util');
52
+ _jestUtil = function () {
53
+ return data;
54
+ };
55
+ return data;
56
+ }
57
+ var _errors = require('./errors');
58
+ var _generateConfigFile = _interopRequireDefault(
59
+ require('./generateConfigFile')
60
+ );
61
+ var _modifyPackageJson = _interopRequireDefault(require('./modifyPackageJson'));
62
+ var _questions = _interopRequireWildcard(require('./questions'));
63
+ function _interopRequireDefault(obj) {
64
+ return obj && obj.__esModule ? obj : {default: obj};
65
+ }
66
+ function _getRequireWildcardCache(nodeInterop) {
67
+ if (typeof WeakMap !== 'function') return null;
68
+ var cacheBabelInterop = new WeakMap();
69
+ var cacheNodeInterop = new WeakMap();
70
+ return (_getRequireWildcardCache = function (nodeInterop) {
71
+ return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
72
+ })(nodeInterop);
73
+ }
74
+ function _interopRequireWildcard(obj, nodeInterop) {
75
+ if (!nodeInterop && obj && obj.__esModule) {
76
+ return obj;
77
+ }
78
+ if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
79
+ return {default: obj};
80
+ }
81
+ var cache = _getRequireWildcardCache(nodeInterop);
82
+ if (cache && cache.has(obj)) {
83
+ return cache.get(obj);
84
+ }
85
+ var newObj = {};
86
+ var hasPropertyDescriptor =
87
+ Object.defineProperty && Object.getOwnPropertyDescriptor;
88
+ for (var key in obj) {
89
+ if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
90
+ var desc = hasPropertyDescriptor
91
+ ? Object.getOwnPropertyDescriptor(obj, key)
92
+ : null;
93
+ if (desc && (desc.get || desc.set)) {
94
+ Object.defineProperty(newObj, key, desc);
95
+ } else {
96
+ newObj[key] = obj[key];
97
+ }
98
+ }
99
+ }
100
+ newObj.default = obj;
101
+ if (cache) {
102
+ cache.set(obj, newObj);
103
+ }
104
+ return newObj;
105
+ }
106
+ /**
107
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
108
+ *
109
+ * This source code is licensed under the MIT license found in the
110
+ * LICENSE file in the root directory of this source tree.
111
+ */
112
+
113
+ const {
114
+ JEST_CONFIG_BASE_NAME,
115
+ JEST_CONFIG_EXT_MJS,
116
+ JEST_CONFIG_EXT_JS,
117
+ JEST_CONFIG_EXT_TS,
118
+ JEST_CONFIG_EXT_ORDER,
119
+ PACKAGE_JSON
120
+ } = _jestConfig().constants;
121
+ const getConfigFilename = ext => JEST_CONFIG_BASE_NAME + ext;
122
+ async function runCLI() {
123
+ try {
124
+ const rootDir = process.argv[2];
125
+ await runCreate(rootDir);
126
+ } catch (error) {
127
+ (0, _jestUtil().clearLine)(process.stderr);
128
+ (0, _jestUtil().clearLine)(process.stdout);
129
+ if (error instanceof Error && Boolean(error?.stack)) {
130
+ console.error(_chalk().default.red(error.stack));
131
+ } else {
132
+ console.error(_chalk().default.red(error));
133
+ }
134
+ (0, _exit().default)(1);
135
+ throw error;
136
+ }
137
+ }
138
+ async function runCreate(rootDir = process.cwd()) {
139
+ rootDir = (0, _jestUtil().tryRealpath)(rootDir);
140
+ // prerequisite checks
141
+ const projectPackageJsonPath = path().join(rootDir, PACKAGE_JSON);
142
+ if (!fs().existsSync(projectPackageJsonPath)) {
143
+ throw new _errors.NotFoundPackageJsonError(rootDir);
144
+ }
145
+ const questions = _questions.default.slice(0);
146
+ let hasJestProperty = false;
147
+ let projectPackageJson;
148
+ try {
149
+ projectPackageJson = JSON.parse(
150
+ fs().readFileSync(projectPackageJsonPath, 'utf-8')
151
+ );
152
+ } catch {
153
+ throw new _errors.MalformedPackageJsonError(projectPackageJsonPath);
154
+ }
155
+ if (projectPackageJson.jest) {
156
+ hasJestProperty = true;
157
+ }
158
+ const existingJestConfigExt = JEST_CONFIG_EXT_ORDER.find(ext =>
159
+ fs().existsSync(path().join(rootDir, getConfigFilename(ext)))
160
+ );
161
+ if (hasJestProperty || existingJestConfigExt != null) {
162
+ const result = await (0, _prompts().default)({
163
+ initial: true,
164
+ message:
165
+ 'It seems that you already have a jest configuration, do you want to override it?',
166
+ name: 'continue',
167
+ type: 'confirm'
168
+ });
169
+ if (!result.continue) {
170
+ console.log();
171
+ console.log('Aborting...');
172
+ return;
173
+ }
174
+ }
175
+
176
+ // Add test script installation only if needed
177
+ if (
178
+ !projectPackageJson.scripts ||
179
+ projectPackageJson.scripts.test !== 'jest'
180
+ ) {
181
+ questions.unshift(_questions.testScriptQuestion);
182
+ }
183
+
184
+ // Start the init process
185
+ console.log();
186
+ console.log(
187
+ _chalk().default.underline(
188
+ 'The following questions will help Jest to create a suitable configuration for your project\n'
189
+ )
190
+ );
191
+ let promptAborted = false;
192
+ const results = await (0, _prompts().default)(questions, {
193
+ onCancel: () => {
194
+ promptAborted = true;
195
+ }
196
+ });
197
+ if (promptAborted) {
198
+ console.log();
199
+ console.log('Aborting...');
200
+ return;
201
+ }
202
+
203
+ // Determine if Jest should use JS or TS for the config file
204
+ const jestConfigFileExt = results.useTypescript
205
+ ? JEST_CONFIG_EXT_TS
206
+ : projectPackageJson.type === 'module'
207
+ ? JEST_CONFIG_EXT_MJS
208
+ : JEST_CONFIG_EXT_JS;
209
+
210
+ // Determine Jest config path
211
+ const jestConfigPath =
212
+ existingJestConfigExt != null
213
+ ? getConfigFilename(existingJestConfigExt)
214
+ : path().join(rootDir, getConfigFilename(jestConfigFileExt));
215
+ const shouldModifyScripts = results.scripts;
216
+ if (shouldModifyScripts || hasJestProperty) {
217
+ const modifiedPackageJson = (0, _modifyPackageJson.default)({
218
+ projectPackageJson,
219
+ shouldModifyScripts
220
+ });
221
+ fs().writeFileSync(projectPackageJsonPath, modifiedPackageJson);
222
+ console.log('');
223
+ console.log(
224
+ `✏️ Modified ${_chalk().default.cyan(projectPackageJsonPath)}`
225
+ );
226
+ }
227
+ const generatedConfig = (0, _generateConfigFile.default)(
228
+ results,
229
+ projectPackageJson.type === 'module' ||
230
+ jestConfigPath.endsWith(JEST_CONFIG_EXT_MJS)
231
+ );
232
+ fs().writeFileSync(jestConfigPath, generatedConfig);
233
+ console.log('');
234
+ console.log(
235
+ `📝 Configuration file created at ${_chalk().default.cyan(jestConfigPath)}`
236
+ );
237
+ }
package/build/types.js ADDED
@@ -0,0 +1 @@
1
+ 'use strict';
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "create-jest-mine",
3
+ "description": "Create a new Jest project",
4
+ "version": "29.7.0",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/jestjs/jest.git",
8
+ "directory": "packages/create-jest"
9
+ },
10
+ "license": "MIT",
11
+ "bin": "./bin/create-jest.js",
12
+ "main": "./build/index.js",
13
+ "types": "./build/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./build/index.d.ts",
17
+ "default": "./build/index.js"
18
+ },
19
+ "./package.json": "./package.json",
20
+ "./bin/create-jest": "./bin/create-jest.js"
21
+ },
22
+ "dependencies": {
23
+ "@jest/types": "^29.6.3",
24
+ "chalk": "^4.0.0",
25
+ "exit": "^0.1.2",
26
+ "graceful-fs": "^4.2.9",
27
+ "jest-config": "^29.7.0",
28
+ "jest-util": "^29.7.0",
29
+ "prompts": "^2.0.1"
30
+ },
31
+ "engines": {
32
+ "node": "^14.15.0 || ^16.10.0 || >=18.0.0"
33
+ },
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
37
+ "devDependencies": {
38
+ "@types/exit": "^0.1.30",
39
+ "@types/graceful-fs": "^4.1.3",
40
+ "@types/prompts": "^2.0.1"
41
+ },
42
+ "gitHead": "4e56991693da7cd4c3730dc3579a1dd1403ee630"
43
+ }