From 30e957f0fc663f6265980745547f543966f42403 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sun, 27 Aug 2023 12:37:16 +0400 Subject: [PATCH 01/25] feat(create-jest): Add `npm init` / `yarn create` initialiser --- packages/create-jest/.npmignore | 8 ++++++++ packages/create-jest/README.md | 11 +++++++++++ packages/create-jest/bin/create-jest.js | 13 +++++++++++++ packages/create-jest/package.json | 24 ++++++++++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 packages/create-jest/.npmignore create mode 100644 packages/create-jest/README.md create mode 100755 packages/create-jest/bin/create-jest.js create mode 100644 packages/create-jest/package.json diff --git a/packages/create-jest/.npmignore b/packages/create-jest/.npmignore new file mode 100644 index 000000000000..bb9fdb98bae7 --- /dev/null +++ b/packages/create-jest/.npmignore @@ -0,0 +1,8 @@ +**/__mocks__/** +**/__tests__/** +__typetests__ +src +tsconfig.json +tsconfig.tsbuildinfo +api-extractor.json +.eslintcache diff --git a/packages/create-jest/README.md b/packages/create-jest/README.md new file mode 100644 index 000000000000..002699d38cd2 --- /dev/null +++ b/packages/create-jest/README.md @@ -0,0 +1,11 @@ +# create-jest + +> Getting started with Jest with a single command + +```bash +npm init jest@latest +# Or for Yarn +yarn create jest +# Or for pnpm +pnpm dlx create-jest +``` diff --git a/packages/create-jest/bin/create-jest.js b/packages/create-jest/bin/create-jest.js new file mode 100755 index 000000000000..c7f70e032cd3 --- /dev/null +++ b/packages/create-jest/bin/create-jest.js @@ -0,0 +1,13 @@ +#!/usr/bin/env node +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +const importLocal = require('import-local'); + +if (!importLocal(__filename)) { + require('jest-cli').run(['--init']); +} diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json new file mode 100644 index 000000000000..87bba6a7f0bc --- /dev/null +++ b/packages/create-jest/package.json @@ -0,0 +1,24 @@ +{ + "name": "create-jest", + "description": "Create a new Jest project", + "version": "29.6.4", + "repository": { + "type": "git", + "url": "https://github.com/jestjs/jest.git", + "directory": "packages/create-jest" + }, + "license": "MIT", + "bin": { + "create-jest": "./bin/create-jest.js" + }, + "main": "./bin/create-jest.js", + "dependencies": { + "jest-cli": "workspace:^" + }, + "engines": { + "node": "^14.15.0 || ^16.10.0 || >=18.0.0" + }, + "publishConfig": { + "access": "public" + } +} From 5b580b1f984d337b8d03b4476cc4c70f3e2451d8 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sun, 27 Aug 2023 12:47:51 +0400 Subject: [PATCH 02/25] fix: Updated `yarn.lock` --- packages/create-jest/package.json | 4 +--- yarn.lock | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index 87bba6a7f0bc..39757a10e29d 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -8,9 +8,7 @@ "directory": "packages/create-jest" }, "license": "MIT", - "bin": { - "create-jest": "./bin/create-jest.js" - }, + "bin": "./bin/create-jest.js", "main": "./bin/create-jest.js", "dependencies": { "jest-cli": "workspace:^" diff --git a/yarn.lock b/yarn.lock index a83a4736f19b..f8fbdf0e2ea4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8021,6 +8021,16 @@ __metadata: languageName: node linkType: hard +"create-jest@workspace:packages/create-jest": + version: 0.0.0-use.local + resolution: "create-jest@workspace:packages/create-jest" + dependencies: + jest-cli: "workspace:^" + bin: + create-jest: ./bin/create-jest.js + languageName: unknown + linkType: soft + "create-require@npm:^1.1.0": version: 1.1.1 resolution: "create-require@npm:1.1.1" From e804edb89b5aa3cb161551b2643f8b0454d92aa1 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sun, 27 Aug 2023 13:10:56 +0400 Subject: [PATCH 03/25] feat: Add typescript API --- packages/create-jest/package.json | 11 ++++++++++- packages/create-jest/src/index.ts | 10 ++++++++++ packages/create-jest/tsconfig.json | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 packages/create-jest/src/index.ts create mode 100644 packages/create-jest/tsconfig.json diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index 39757a10e29d..0cafb26ae301 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -9,7 +9,16 @@ }, "license": "MIT", "bin": "./bin/create-jest.js", - "main": "./bin/create-jest.js", + "main": "./build/index.js", + "types": "./build/index.d.ts", + "exports": { + ".": { + "types": "./build/index.d.ts", + "default": "./build/index.js" + }, + "./package.json": "./package.json", + "./bin/create-jest": "./bin/create-jest.js" + }, "dependencies": { "jest-cli": "workspace:^" }, diff --git a/packages/create-jest/src/index.ts b/packages/create-jest/src/index.ts new file mode 100644 index 000000000000..da80f80ba1fc --- /dev/null +++ b/packages/create-jest/src/index.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {run as runCLI} from 'jest-cli'; + +export const run = (): Promise => runCLI(['--init']); diff --git a/packages/create-jest/tsconfig.json b/packages/create-jest/tsconfig.json new file mode 100644 index 000000000000..aefe83d5c410 --- /dev/null +++ b/packages/create-jest/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "include": ["./src/**/*"], + "references": [ + {"path": "../jest-cli"} + ] +} From 546db2565465a9020b85ef1c4cc4e99355fc9074 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sun, 27 Aug 2023 13:22:29 +0400 Subject: [PATCH 04/25] fix: Prettier error --- packages/create-jest/tsconfig.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/create-jest/tsconfig.json b/packages/create-jest/tsconfig.json index aefe83d5c410..07aaaab547d0 100644 --- a/packages/create-jest/tsconfig.json +++ b/packages/create-jest/tsconfig.json @@ -5,7 +5,5 @@ "outDir": "build" }, "include": ["./src/**/*"], - "references": [ - {"path": "../jest-cli"} - ] + "references": [{"path": "../jest-cli"}] } From 0ffab46d13ee1a05a603eb9c846d798a10bb1ff4 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 00:02:46 +0400 Subject: [PATCH 05/25] feat: Move config initialisation logic to `create-jest` package --- .eslintrc.cjs | 48 +++++++++---------- packages/create-jest/bin/create-jest.js | 2 +- packages/create-jest/package.json | 11 ++++- .../has-jest-config-file-cjs/jest.config.cjs | 0 .../has-jest-config-file-cjs/package.json | 0 .../has-jest-config-file-js/jest.config.js | 0 .../has-jest-config-file-js/package.json | 0 .../jest.config.json | 0 .../has-jest-config-file-json/package.json | 0 .../has-jest-config-file-mjs/jest.config.mjs | 0 .../has-jest-config-file-mjs/package.json | 0 .../has-jest-config-file-ts/jest.config.ts | 0 .../has-jest-config-file-ts/package.json | 0 .../package.json | 0 .../__fixtures__/no-package-json/index.js | 0 .../only-package-json/package.json | 0 .../package.json | 0 .../test-script-configured/package.json | 0 .../__fixtures__/type-module/package.json | 0 .../typescript-in-dependencies/package.json | 0 .../package.json | 0 .../__tests__/__snapshots__/init.test.ts.snap | 0 .../modifyPackageJson.test.ts.snap | 0 .../src}/__tests__/init.test.ts | 38 ++++++++------- .../src}/__tests__/modifyPackageJson.test.ts | 0 .../create-jest/src/__tests__/tsconfig.json | 5 ++ .../src/init => create-jest/src}/errors.ts | 0 .../src}/generateConfigFile.ts | 0 packages/create-jest/src/index.ts | 4 +- .../src}/modifyPackageJson.ts | 0 .../src/init => create-jest/src}/questions.ts | 0 .../index.ts => create-jest/src/runCreate.ts} | 2 +- .../src/init => create-jest/src}/types.ts | 0 packages/create-jest/tsconfig.json | 7 ++- packages/jest-cli/package.json | 5 +- .../jest-cli/src/init/__tests__/tsconfig.json | 5 -- packages/jest-cli/src/run.ts | 4 +- packages/jest-cli/tsconfig.json | 1 + yarn.lock | 16 ++++--- 39 files changed, 83 insertions(+), 65 deletions(-) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-cjs/jest.config.cjs (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-cjs/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-js/jest.config.js (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-js/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-json/jest.config.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-json/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-mjs/jest.config.mjs (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-mjs/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-ts/jest.config.ts (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-file-ts/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/has-jest-config-in-package-json/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/no-package-json/index.js (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/only-package-json/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/test-generated-jest-config-ts/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/test-script-configured/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/type-module/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/typescript-in-dependencies/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__fixtures__/typescript-in-dev-dependencies/package.json (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__snapshots__/init.test.ts.snap (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/__snapshots__/modifyPackageJson.test.ts.snap (100%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/init.test.ts (88%) rename packages/{jest-cli/src/init => create-jest/src}/__tests__/modifyPackageJson.test.ts (100%) create mode 100644 packages/create-jest/src/__tests__/tsconfig.json rename packages/{jest-cli/src/init => create-jest/src}/errors.ts (100%) rename packages/{jest-cli/src/init => create-jest/src}/generateConfigFile.ts (100%) rename packages/{jest-cli/src/init => create-jest/src}/modifyPackageJson.ts (100%) rename packages/{jest-cli/src/init => create-jest/src}/questions.ts (100%) rename packages/{jest-cli/src/init/index.ts => create-jest/src/runCreate.ts} (99%) rename packages/{jest-cli/src/init => create-jest/src}/types.ts (100%) delete mode 100644 packages/jest-cli/src/init/__tests__/tsconfig.json diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 089863ba5f5e..cbae481af219 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -9,7 +9,7 @@ const fs = require('fs'); const path = require('path'); -const {sync: readPkg} = require('read-pkg'); +const { sync: readPkg } = require('read-pkg'); function getPackages() { const PACKAGES_DIR = path.resolve(__dirname, 'packages'); @@ -19,7 +19,7 @@ function getPackages() { .filter(f => fs.lstatSync(path.resolve(f)).isDirectory()) .filter(f => fs.existsSync(path.join(path.resolve(f), 'package.json'))); return packages.map(packageDir => { - const pkg = readPkg({cwd: packageDir}); + const pkg = readPkg({ cwd: packageDir }); return pkg.name; }); } @@ -48,12 +48,12 @@ module.exports = { files: ['*.ts', '*.tsx'], plugins: ['@typescript-eslint/eslint-plugin', 'local'], rules: { - '@typescript-eslint/array-type': ['error', {default: 'generic'}], + '@typescript-eslint/array-type': ['error', { default: 'generic' }], '@typescript-eslint/ban-types': 'error', '@typescript-eslint/no-inferrable-types': 'error', '@typescript-eslint/no-unused-vars': [ 'error', - {argsIgnorePattern: '^_'}, + { argsIgnorePattern: '^_' }, ], '@typescript-eslint/prefer-ts-expect-error': 'error', '@typescript-eslint/no-var-requires': 'off', @@ -129,7 +129,7 @@ module.exports = { '@typescript-eslint/ban-types': [ 'error', // TODO: remove these overrides: https://github.com/jestjs/jest/issues/10177 - {types: {Function: false, object: false, '{}': false}}, + { types: { Function: false, object: false, '{}': false } }, ], 'local/ban-types-eventually': [ 'warn', @@ -160,7 +160,7 @@ module.exports = { 'e2e/test-match/test-suites/*', 'packages/test-utils/src/ConditionalTest.ts', ], - env: {'jest/globals': true}, + env: { 'jest/globals': true }, excludedFiles: ['**/__typetests__/**'], extends: ['plugin:jest/style'], plugins: ['jest'], @@ -252,7 +252,7 @@ module.exports = { quotes: [ 'error', 'single', - {allowTemplateLiterals: true, avoidEscape: true}, + { allowTemplateLiterals: true, avoidEscape: true }, ], }, }, @@ -350,14 +350,14 @@ module.exports = { }, }, { - env: {node: true}, + env: { node: true }, files: ['*.js', '*.jsx', '*.mjs', '*.cjs'], }, { files: [ 'scripts/*', 'packages/*/__benchmarks__/test.js', - 'packages/jest-cli/src/init/index.ts', + 'packages/create-jest/src/runCreate.ts', 'packages/jest-repl/src/cli/runtime-cli.ts', ], rules: { @@ -387,10 +387,10 @@ module.exports = { }, plugins: ['import', 'jsdoc'], rules: { - 'accessor-pairs': ['warn', {setWithoutGet: true}], + 'accessor-pairs': ['warn', { setWithoutGet: true }], 'block-scoped-var': 'off', 'callback-return': 'off', - camelcase: ['off', {properties: 'always'}], + camelcase: ['off', { properties: 'always' }], complexity: 'off', 'consistent-return': 'warn', 'consistent-this': ['off', 'self'], @@ -398,7 +398,7 @@ module.exports = { 'default-case': 'off', 'dot-notation': 'off', eqeqeq: ['off', 'allow-null'], - 'eslint-comments/disable-enable-pair': ['error', {allowWholeFile: true}], + 'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }], 'eslint-comments/no-unused-disable': 'error', 'func-names': 'off', 'func-style': ['off', 'declaration'], @@ -422,7 +422,7 @@ module.exports = { ], }, ], - 'import/no-unresolved': ['error', {ignore: ['fsevents']}], + 'import/no-unresolved': ['error', { ignore: ['fsevents'] }], 'import/order': [ 'error', { @@ -461,7 +461,7 @@ module.exports = { 'no-confusing-arrow': 'off', 'no-console': [ 'warn', - {allow: ['warn', 'error', 'time', 'timeEnd', 'timeStamp']}, + { allow: ['warn', 'error', 'time', 'timeEnd', 'timeStamp'] }, ], 'no-const-assign': 'error', 'no-constant-condition': 'off', @@ -497,7 +497,7 @@ module.exports = { 'no-irregular-whitespace': 'error', 'no-iterator': 'off', 'no-label-var': 'warn', - 'no-labels': ['error', {allowLoop: true, allowSwitch: true}], + 'no-labels': ['error', { allowLoop: true, allowSwitch: true }], 'no-lonely-if': 'off', 'no-loop-func': 'off', 'no-magic-numbers': 'off', @@ -505,7 +505,7 @@ module.exports = { 'no-mixed-spaces-and-tabs': 'error', 'no-multi-str': 'error', 'no-multiple-empty-lines': 'off', - 'no-native-reassign': ['error', {exceptions: ['Map', 'Set']}], + 'no-native-reassign': ['error', { exceptions: ['Map', 'Set'] }], 'no-negated-condition': 'off', 'no-negated-in-lhs': 'error', 'no-nested-ternary': 'off', @@ -527,11 +527,11 @@ module.exports = { 'no-regex-spaces': 'warn', 'no-restricted-globals': [ 'error', - {message: 'Use `globalThis` instead.', name: 'global'}, + { message: 'Use `globalThis` instead.', name: 'global' }, ], 'no-restricted-imports': [ 'error', - {message: 'Please use graceful-fs instead.', name: 'fs'}, + { message: 'Please use graceful-fs instead.', name: 'fs' }, ], 'no-restricted-modules': 'off', 'no-restricted-syntax': 'off', @@ -553,7 +553,7 @@ module.exports = { 'no-unneeded-ternary': 'warn', 'no-unreachable': 'error', 'no-unused-expressions': 'off', - 'no-unused-vars': ['error', {argsIgnorePattern: '^_'}], + 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], 'no-use-before-define': 'off', 'no-useless-call': 'error', 'no-useless-computed-key': 'error', @@ -563,25 +563,25 @@ module.exports = { 'no-warn-comments': 'off', 'no-with': 'off', 'object-shorthand': 'error', - 'one-var': ['warn', {initialized: 'never'}], + 'one-var': ['warn', { initialized: 'never' }], 'operator-assignment': ['warn', 'always'], 'operator-linebreak': 'off', 'padded-blocks': 'off', - 'prefer-arrow-callback': ['error', {allowNamedFunctions: true}], + 'prefer-arrow-callback': ['error', { allowNamedFunctions: true }], 'prefer-const': 'error', 'prefer-template': 'error', quotes: [ 'error', 'single', - {allowTemplateLiterals: false, avoidEscape: true}, + { allowTemplateLiterals: false, avoidEscape: true }, ], radix: 'warn', 'require-jsdoc': 'off', 'require-yield': 'off', - 'sort-imports': ['error', {ignoreDeclarationSort: true}], + 'sort-imports': ['error', { ignoreDeclarationSort: true }], 'sort-keys': 'error', 'sort-vars': 'off', - 'spaced-comment': ['off', 'always', {exceptions: ['eslint', 'global']}], + 'spaced-comment': ['off', 'always', { exceptions: ['eslint', 'global'] }], strict: 'off', 'use-isnan': 'error', 'valid-jsdoc': 'off', diff --git a/packages/create-jest/bin/create-jest.js b/packages/create-jest/bin/create-jest.js index c7f70e032cd3..d611d2c99c76 100755 --- a/packages/create-jest/bin/create-jest.js +++ b/packages/create-jest/bin/create-jest.js @@ -9,5 +9,5 @@ const importLocal = require('import-local'); if (!importLocal(__filename)) { - require('jest-cli').run(['--init']); + require('..').runCreate(); } diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index 0cafb26ae301..f572830b2dcc 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -20,12 +20,21 @@ "./bin/create-jest": "./bin/create-jest.js" }, "dependencies": { - "jest-cli": "workspace:^" + "@jest/types": "workspace:^", + "chalk": "^4.0.0", + "graceful-fs": "^4.2.9", + "jest-config": "workspace:^", + "jest-util": "workspace:^", + "prompts": "^2.0.1" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" }, "publishConfig": { "access": "public" + }, + "devDependencies": { + "@types/graceful-fs": "^4.1.3", + "@types/prompts": "^2.0.1" } } diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cjs/jest.config.cjs b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-cjs/jest.config.cjs similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cjs/jest.config.cjs rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-cjs/jest.config.cjs diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cjs/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-cjs/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-cjs/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-cjs/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-js/jest.config.js b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-js/jest.config.js similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-js/jest.config.js rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-js/jest.config.js diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-js/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-js/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-js/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-js/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-json/jest.config.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-json/jest.config.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-json/jest.config.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-json/jest.config.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-json/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-json/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-json/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-json/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-mjs/jest.config.mjs b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-mjs/jest.config.mjs similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-mjs/jest.config.mjs rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-mjs/jest.config.mjs diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-mjs/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-mjs/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-mjs/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-mjs/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-ts/jest.config.ts b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-ts/jest.config.ts similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-ts/jest.config.ts rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-ts/jest.config.ts diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-ts/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-ts/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-file-ts/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-file-ts/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-in-package-json/package.json b/packages/create-jest/src/__tests__/__fixtures__/has-jest-config-in-package-json/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/has-jest-config-in-package-json/package.json rename to packages/create-jest/src/__tests__/__fixtures__/has-jest-config-in-package-json/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/no-package-json/index.js b/packages/create-jest/src/__tests__/__fixtures__/no-package-json/index.js similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/no-package-json/index.js rename to packages/create-jest/src/__tests__/__fixtures__/no-package-json/index.js diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/only-package-json/package.json b/packages/create-jest/src/__tests__/__fixtures__/only-package-json/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/only-package-json/package.json rename to packages/create-jest/src/__tests__/__fixtures__/only-package-json/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/test-generated-jest-config-ts/package.json b/packages/create-jest/src/__tests__/__fixtures__/test-generated-jest-config-ts/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/test-generated-jest-config-ts/package.json rename to packages/create-jest/src/__tests__/__fixtures__/test-generated-jest-config-ts/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/test-script-configured/package.json b/packages/create-jest/src/__tests__/__fixtures__/test-script-configured/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/test-script-configured/package.json rename to packages/create-jest/src/__tests__/__fixtures__/test-script-configured/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/type-module/package.json b/packages/create-jest/src/__tests__/__fixtures__/type-module/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/type-module/package.json rename to packages/create-jest/src/__tests__/__fixtures__/type-module/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/typescript-in-dependencies/package.json b/packages/create-jest/src/__tests__/__fixtures__/typescript-in-dependencies/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/typescript-in-dependencies/package.json rename to packages/create-jest/src/__tests__/__fixtures__/typescript-in-dependencies/package.json diff --git a/packages/jest-cli/src/init/__tests__/__fixtures__/typescript-in-dev-dependencies/package.json b/packages/create-jest/src/__tests__/__fixtures__/typescript-in-dev-dependencies/package.json similarity index 100% rename from packages/jest-cli/src/init/__tests__/__fixtures__/typescript-in-dev-dependencies/package.json rename to packages/create-jest/src/__tests__/__fixtures__/typescript-in-dev-dependencies/package.json diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap b/packages/create-jest/src/__tests__/__snapshots__/init.test.ts.snap similarity index 100% rename from packages/jest-cli/src/init/__tests__/__snapshots__/init.test.ts.snap rename to packages/create-jest/src/__tests__/__snapshots__/init.test.ts.snap diff --git a/packages/jest-cli/src/init/__tests__/__snapshots__/modifyPackageJson.test.ts.snap b/packages/create-jest/src/__tests__/__snapshots__/modifyPackageJson.test.ts.snap similarity index 100% rename from packages/jest-cli/src/init/__tests__/__snapshots__/modifyPackageJson.test.ts.snap rename to packages/create-jest/src/__tests__/__snapshots__/modifyPackageJson.test.ts.snap diff --git a/packages/jest-cli/src/init/__tests__/init.test.ts b/packages/create-jest/src/__tests__/init.test.ts similarity index 88% rename from packages/jest-cli/src/init/__tests__/init.test.ts rename to packages/create-jest/src/__tests__/init.test.ts index 30a5769d1be1..38bb1b747226 100644 --- a/packages/jest-cli/src/init/__tests__/init.test.ts +++ b/packages/create-jest/src/__tests__/init.test.ts @@ -10,7 +10,7 @@ import * as path from 'path'; import {writeFileSync} from 'graceful-fs'; import * as prompts from 'prompts'; import {constants} from 'jest-config'; -import init from '../'; +import {runCreate} from '../runCreate'; const {JEST_CONFIG_EXT_ORDER} = constants; @@ -44,7 +44,7 @@ describe('init', () => { it('should return the default configuration (an empty config)', async () => { jest.mocked(prompts).mockResolvedValueOnce({}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfigFilename = jest.mocked(writeFileSync).mock.calls[0][0]; @@ -71,7 +71,7 @@ describe('init', () => { it('should generate empty config with mjs extension', async () => { jest.mocked(prompts).mockResolvedValueOnce({}); - await init(resolveFromFixture('type-module')); + await runCreate(resolveFromFixture('type-module')); const writtenJestConfigFilename = jest.mocked(writeFileSync).mock.calls[0][0]; @@ -93,7 +93,7 @@ describe('init', () => { it('should create configuration for {clearMocks: true}', async () => { jest.mocked(prompts).mockResolvedValueOnce({clearMocks: true}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -107,7 +107,7 @@ describe('init', () => { it('should create configuration for {coverage: true}', async () => { jest.mocked(prompts).mockResolvedValueOnce({coverage: true}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -124,7 +124,7 @@ describe('init', () => { it('should create configuration for {coverageProvider: "babel"}', async () => { jest.mocked(prompts).mockResolvedValueOnce({coverageProvider: 'babel'}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -138,7 +138,7 @@ describe('init', () => { it('should create configuration for {coverageProvider: "v8"}', async () => { jest.mocked(prompts).mockResolvedValueOnce({coverageProvider: 'v8'}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -152,7 +152,7 @@ describe('init', () => { it('should create configuration for {environment: "jsdom"}', async () => { jest.mocked(prompts).mockResolvedValueOnce({environment: 'jsdom'}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -165,7 +165,7 @@ describe('init', () => { it('should create configuration for {environment: "node"}', async () => { jest.mocked(prompts).mockResolvedValueOnce({environment: 'node'}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenJestConfig = jest.mocked(writeFileSync).mock.calls[0][1]; const evaluatedConfig = eval(writtenJestConfig as string) as Record< @@ -178,7 +178,7 @@ describe('init', () => { it('should create package.json with configured test command when {scripts: true}', async () => { jest.mocked(prompts).mockResolvedValueOnce({scripts: true}); - await init(resolveFromFixture('only-package-json')); + await runCreate(resolveFromFixture('only-package-json')); const writtenPackageJson = jest.mocked(writeFileSync).mock.calls[0][1]; const parsedPackageJson = JSON.parse(writtenPackageJson as string) as { @@ -196,7 +196,7 @@ describe('init', () => { expect.assertions(1); try { - await init(resolveFromFixture('no-package-json')); + await runCreate(resolveFromFixture('no-package-json')); } catch (error) { expect((error as Error).message).toMatch( 'Could not find a "package.json" file in', @@ -215,7 +215,9 @@ describe('init', () => { .mockResolvedValueOnce({continue: true}) .mockResolvedValueOnce({}); - await init(resolveFromFixture(`has-jest-config-file-${extension}`)); + await runCreate( + resolveFromFixture(`has-jest-config-file-${extension}`), + ); expect(jest.mocked(prompts).mock.calls[0][0]).toMatchSnapshot(); @@ -230,7 +232,9 @@ describe('init', () => { it('user answered with "No"', async () => { jest.mocked(prompts).mockResolvedValueOnce({continue: false}); - await init(resolveFromFixture(`has-jest-config-file-${extension}`)); + await runCreate( + resolveFromFixture(`has-jest-config-file-${extension}`), + ); // return after first prompt expect(prompts).toHaveBeenCalledTimes(1); }); @@ -243,7 +247,7 @@ describe('init', () => { it('user answered with "Yes"', async () => { jest.mocked(prompts).mockResolvedValueOnce({useTypescript: true}); - await init(resolveFromFixture('test-generated-jest-config-ts')); + await runCreate(resolveFromFixture('test-generated-jest-config-ts')); expect(jest.mocked(prompts).mock.calls[0][0]).toMatchSnapshot(); @@ -264,7 +268,7 @@ describe('init', () => { it('user answered with "No"', async () => { jest.mocked(prompts).mockResolvedValueOnce({useTypescript: false}); - await init(resolveFromFixture('test-generated-jest-config-ts')); + await runCreate(resolveFromFixture('test-generated-jest-config-ts')); const jestConfigFileName = jest.mocked(writeFileSync).mock.calls[0][0]; @@ -282,7 +286,7 @@ describe('init', () => { .mockResolvedValueOnce({continue: true}) .mockResolvedValueOnce({}); - await init(resolveFromFixture('has-jest-config-in-package-json')); + await runCreate(resolveFromFixture('has-jest-config-in-package-json')); expect(jest.mocked(prompts).mock.calls[0][0]).toMatchSnapshot(); @@ -296,7 +300,7 @@ describe('init', () => { it('should not ask "test script question"', async () => { jest.mocked(prompts).mockResolvedValueOnce({}); - await init(resolveFromFixture('test-script-configured')); + await runCreate(resolveFromFixture('test-script-configured')); const questions = jest.mocked(prompts).mock.calls[0][0] as Array< prompts.PromptObject diff --git a/packages/jest-cli/src/init/__tests__/modifyPackageJson.test.ts b/packages/create-jest/src/__tests__/modifyPackageJson.test.ts similarity index 100% rename from packages/jest-cli/src/init/__tests__/modifyPackageJson.test.ts rename to packages/create-jest/src/__tests__/modifyPackageJson.test.ts diff --git a/packages/create-jest/src/__tests__/tsconfig.json b/packages/create-jest/src/__tests__/tsconfig.json new file mode 100644 index 000000000000..dd1bca103251 --- /dev/null +++ b/packages/create-jest/src/__tests__/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../../../../tsconfig.test.json", + "include": ["./**/*"], + "references": [{"path": "../../"}] +} diff --git a/packages/jest-cli/src/init/errors.ts b/packages/create-jest/src/errors.ts similarity index 100% rename from packages/jest-cli/src/init/errors.ts rename to packages/create-jest/src/errors.ts diff --git a/packages/jest-cli/src/init/generateConfigFile.ts b/packages/create-jest/src/generateConfigFile.ts similarity index 100% rename from packages/jest-cli/src/init/generateConfigFile.ts rename to packages/create-jest/src/generateConfigFile.ts diff --git a/packages/create-jest/src/index.ts b/packages/create-jest/src/index.ts index da80f80ba1fc..fb5b27b18a34 100644 --- a/packages/create-jest/src/index.ts +++ b/packages/create-jest/src/index.ts @@ -5,6 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -import {run as runCLI} from 'jest-cli'; - -export const run = (): Promise => runCLI(['--init']); +export {runCreate} from './runCreate'; diff --git a/packages/jest-cli/src/init/modifyPackageJson.ts b/packages/create-jest/src/modifyPackageJson.ts similarity index 100% rename from packages/jest-cli/src/init/modifyPackageJson.ts rename to packages/create-jest/src/modifyPackageJson.ts diff --git a/packages/jest-cli/src/init/questions.ts b/packages/create-jest/src/questions.ts similarity index 100% rename from packages/jest-cli/src/init/questions.ts rename to packages/create-jest/src/questions.ts diff --git a/packages/jest-cli/src/init/index.ts b/packages/create-jest/src/runCreate.ts similarity index 99% rename from packages/jest-cli/src/init/index.ts rename to packages/create-jest/src/runCreate.ts index 5a9afe346d97..898fad5e654f 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/create-jest/src/runCreate.ts @@ -28,7 +28,7 @@ const { const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; -export default async function init( +export async function runCreate( rootDir: string = tryRealpath(process.cwd()), ): Promise { // prerequisite checks diff --git a/packages/jest-cli/src/init/types.ts b/packages/create-jest/src/types.ts similarity index 100% rename from packages/jest-cli/src/init/types.ts rename to packages/create-jest/src/types.ts diff --git a/packages/create-jest/tsconfig.json b/packages/create-jest/tsconfig.json index 07aaaab547d0..0f1cd728f463 100644 --- a/packages/create-jest/tsconfig.json +++ b/packages/create-jest/tsconfig.json @@ -5,5 +5,10 @@ "outDir": "build" }, "include": ["./src/**/*"], - "references": [{"path": "../jest-cli"}] + "exclude": ["./**/__tests__/**/*"], + "references": [ + {"path": "../jest-config"}, + {"path": "../jest-types"}, + {"path": "../jest-util"} + ] } diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 067c0744072a..e2e641829e9b 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -17,20 +17,17 @@ "@jest/test-result": "workspace:^", "@jest/types": "workspace:^", "chalk": "^4.0.0", + "create-jest": "workspace:^", "exit": "^0.1.2", - "graceful-fs": "^4.2.9", "import-local": "^3.0.2", "jest-config": "workspace:^", "jest-util": "workspace:^", "jest-validate": "workspace:^", - "prompts": "^2.0.1", "yargs": "^17.3.1" }, "devDependencies": { "@tsd/typescript": "^5.0.4", "@types/exit": "^0.1.30", - "@types/graceful-fs": "^4.1.3", - "@types/prompts": "^2.0.1", "@types/yargs": "^17.0.8", "tsd-lite": "^0.7.0" }, diff --git a/packages/jest-cli/src/init/__tests__/tsconfig.json b/packages/jest-cli/src/init/__tests__/tsconfig.json deleted file mode 100644 index facea41430cf..000000000000 --- a/packages/jest-cli/src/init/__tests__/tsconfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "extends": "../../../../../tsconfig.test.json", - "include": ["./**/*"], - "references": [{"path": "../../../"}] -} diff --git a/packages/jest-cli/src/run.ts b/packages/jest-cli/src/run.ts index 7322203214ed..6b68efb15df5 100644 --- a/packages/jest-cli/src/run.ts +++ b/packages/jest-cli/src/run.ts @@ -12,11 +12,11 @@ import yargs = require('yargs'); import {getVersion, runCLI} from '@jest/core'; import type {AggregatedResult} from '@jest/test-result'; import type {Config} from '@jest/types'; +import {runCreate} from 'create-jest'; import {deprecationEntries} from 'jest-config'; import {clearLine, tryRealpath} from 'jest-util'; import {validateCLIOptions} from 'jest-validate'; import * as args from './args'; -import init from './init'; export async function run( maybeArgv?: Array, @@ -26,7 +26,7 @@ export async function run( const argv = await buildArgv(maybeArgv); if (argv.init) { - await init(); + await runCreate(); return; } diff --git a/packages/jest-cli/tsconfig.json b/packages/jest-cli/tsconfig.json index 32f96c0f8a77..dabfdb21d4c4 100644 --- a/packages/jest-cli/tsconfig.json +++ b/packages/jest-cli/tsconfig.json @@ -7,6 +7,7 @@ "include": ["./src/**/*"], "exclude": ["./**/__tests__/**/*"], "references": [ + {"path": "../create-jest"}, {"path": "../jest-config"}, {"path": "../jest-core"}, {"path": "../jest-test-result"}, diff --git a/yarn.lock b/yarn.lock index f8fbdf0e2ea4..f446e5f02cbb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8021,11 +8021,18 @@ __metadata: languageName: node linkType: hard -"create-jest@workspace:packages/create-jest": +"create-jest@workspace:^, create-jest@workspace:packages/create-jest": version: 0.0.0-use.local resolution: "create-jest@workspace:packages/create-jest" dependencies: - jest-cli: "workspace:^" + "@jest/types": "workspace:^" + "@types/graceful-fs": ^4.1.3 + "@types/prompts": ^2.0.1 + chalk: ^4.0.0 + graceful-fs: ^4.2.9 + jest-config: "workspace:^" + jest-util: "workspace:^" + prompts: ^2.0.1 bin: create-jest: ./bin/create-jest.js languageName: unknown @@ -12406,17 +12413,14 @@ __metadata: "@jest/types": "workspace:^" "@tsd/typescript": ^5.0.4 "@types/exit": ^0.1.30 - "@types/graceful-fs": ^4.1.3 - "@types/prompts": ^2.0.1 "@types/yargs": ^17.0.8 chalk: ^4.0.0 + create-jest: "workspace:^" exit: ^0.1.2 - graceful-fs: ^4.2.9 import-local: ^3.0.2 jest-config: "workspace:^" jest-util: "workspace:^" jest-validate: "workspace:^" - prompts: ^2.0.1 tsd-lite: ^0.7.0 yargs: ^17.3.1 peerDependencies: From df988bfb6327a0d6c1696cdc230f4045a9ee45a9 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 00:22:41 +0400 Subject: [PATCH 06/25] fix: Eslint errors --- .eslintrc.cjs | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index cbae481af219..7151330e720d 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -9,7 +9,7 @@ const fs = require('fs'); const path = require('path'); -const { sync: readPkg } = require('read-pkg'); +const {sync: readPkg} = require('read-pkg'); function getPackages() { const PACKAGES_DIR = path.resolve(__dirname, 'packages'); @@ -19,7 +19,7 @@ function getPackages() { .filter(f => fs.lstatSync(path.resolve(f)).isDirectory()) .filter(f => fs.existsSync(path.join(path.resolve(f), 'package.json'))); return packages.map(packageDir => { - const pkg = readPkg({ cwd: packageDir }); + const pkg = readPkg({cwd: packageDir}); return pkg.name; }); } @@ -48,12 +48,12 @@ module.exports = { files: ['*.ts', '*.tsx'], plugins: ['@typescript-eslint/eslint-plugin', 'local'], rules: { - '@typescript-eslint/array-type': ['error', { default: 'generic' }], + '@typescript-eslint/array-type': ['error', {default: 'generic'}], '@typescript-eslint/ban-types': 'error', '@typescript-eslint/no-inferrable-types': 'error', '@typescript-eslint/no-unused-vars': [ 'error', - { argsIgnorePattern: '^_' }, + {argsIgnorePattern: '^_'}, ], '@typescript-eslint/prefer-ts-expect-error': 'error', '@typescript-eslint/no-var-requires': 'off', @@ -129,7 +129,7 @@ module.exports = { '@typescript-eslint/ban-types': [ 'error', // TODO: remove these overrides: https://github.com/jestjs/jest/issues/10177 - { types: { Function: false, object: false, '{}': false } }, + {types: {Function: false, object: false, '{}': false}}, ], 'local/ban-types-eventually': [ 'warn', @@ -160,7 +160,7 @@ module.exports = { 'e2e/test-match/test-suites/*', 'packages/test-utils/src/ConditionalTest.ts', ], - env: { 'jest/globals': true }, + env: {'jest/globals': true}, excludedFiles: ['**/__typetests__/**'], extends: ['plugin:jest/style'], plugins: ['jest'], @@ -252,7 +252,7 @@ module.exports = { quotes: [ 'error', 'single', - { allowTemplateLiterals: true, avoidEscape: true }, + {allowTemplateLiterals: true, avoidEscape: true}, ], }, }, @@ -350,7 +350,7 @@ module.exports = { }, }, { - env: { node: true }, + env: {node: true}, files: ['*.js', '*.jsx', '*.mjs', '*.cjs'], }, { @@ -387,10 +387,10 @@ module.exports = { }, plugins: ['import', 'jsdoc'], rules: { - 'accessor-pairs': ['warn', { setWithoutGet: true }], + 'accessor-pairs': ['warn', {setWithoutGet: true}], 'block-scoped-var': 'off', 'callback-return': 'off', - camelcase: ['off', { properties: 'always' }], + camelcase: ['off', {properties: 'always'}], complexity: 'off', 'consistent-return': 'warn', 'consistent-this': ['off', 'self'], @@ -398,7 +398,7 @@ module.exports = { 'default-case': 'off', 'dot-notation': 'off', eqeqeq: ['off', 'allow-null'], - 'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }], + 'eslint-comments/disable-enable-pair': ['error', {allowWholeFile: true}], 'eslint-comments/no-unused-disable': 'error', 'func-names': 'off', 'func-style': ['off', 'declaration'], @@ -422,7 +422,7 @@ module.exports = { ], }, ], - 'import/no-unresolved': ['error', { ignore: ['fsevents'] }], + 'import/no-unresolved': ['error', {ignore: ['fsevents']}], 'import/order': [ 'error', { @@ -461,7 +461,7 @@ module.exports = { 'no-confusing-arrow': 'off', 'no-console': [ 'warn', - { allow: ['warn', 'error', 'time', 'timeEnd', 'timeStamp'] }, + {allow: ['warn', 'error', 'time', 'timeEnd', 'timeStamp']}, ], 'no-const-assign': 'error', 'no-constant-condition': 'off', @@ -497,7 +497,7 @@ module.exports = { 'no-irregular-whitespace': 'error', 'no-iterator': 'off', 'no-label-var': 'warn', - 'no-labels': ['error', { allowLoop: true, allowSwitch: true }], + 'no-labels': ['error', {allowLoop: true, allowSwitch: true}], 'no-lonely-if': 'off', 'no-loop-func': 'off', 'no-magic-numbers': 'off', @@ -505,7 +505,7 @@ module.exports = { 'no-mixed-spaces-and-tabs': 'error', 'no-multi-str': 'error', 'no-multiple-empty-lines': 'off', - 'no-native-reassign': ['error', { exceptions: ['Map', 'Set'] }], + 'no-native-reassign': ['error', {exceptions: ['Map', 'Set']}], 'no-negated-condition': 'off', 'no-negated-in-lhs': 'error', 'no-nested-ternary': 'off', @@ -527,11 +527,11 @@ module.exports = { 'no-regex-spaces': 'warn', 'no-restricted-globals': [ 'error', - { message: 'Use `globalThis` instead.', name: 'global' }, + {message: 'Use `globalThis` instead.', name: 'global'}, ], 'no-restricted-imports': [ 'error', - { message: 'Please use graceful-fs instead.', name: 'fs' }, + {message: 'Please use graceful-fs instead.', name: 'fs'}, ], 'no-restricted-modules': 'off', 'no-restricted-syntax': 'off', @@ -553,7 +553,7 @@ module.exports = { 'no-unneeded-ternary': 'warn', 'no-unreachable': 'error', 'no-unused-expressions': 'off', - 'no-unused-vars': ['error', { argsIgnorePattern: '^_' }], + 'no-unused-vars': ['error', {argsIgnorePattern: '^_'}], 'no-use-before-define': 'off', 'no-useless-call': 'error', 'no-useless-computed-key': 'error', @@ -563,25 +563,25 @@ module.exports = { 'no-warn-comments': 'off', 'no-with': 'off', 'object-shorthand': 'error', - 'one-var': ['warn', { initialized: 'never' }], + 'one-var': ['warn', {initialized: 'never'}], 'operator-assignment': ['warn', 'always'], 'operator-linebreak': 'off', 'padded-blocks': 'off', - 'prefer-arrow-callback': ['error', { allowNamedFunctions: true }], + 'prefer-arrow-callback': ['error', {allowNamedFunctions: true}], 'prefer-const': 'error', 'prefer-template': 'error', quotes: [ 'error', 'single', - { allowTemplateLiterals: false, avoidEscape: true }, + {allowTemplateLiterals: false, avoidEscape: true}, ], radix: 'warn', 'require-jsdoc': 'off', 'require-yield': 'off', - 'sort-imports': ['error', { ignoreDeclarationSort: true }], + 'sort-imports': ['error', {ignoreDeclarationSort: true}], 'sort-keys': 'error', 'sort-vars': 'off', - 'spaced-comment': ['off', 'always', { exceptions: ['eslint', 'global'] }], + 'spaced-comment': ['off', 'always', {exceptions: ['eslint', 'global']}], strict: 'off', 'use-isnan': 'error', 'valid-jsdoc': 'off', From 92db787ead028011c93bf939837b498b9e486d9b Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 01:35:40 +0400 Subject: [PATCH 07/25] feat: Add `rootDir` to `create-jest` CLI API --- packages/create-jest/bin/create-jest.js | 2 +- packages/create-jest/package.json | 9 +++++-- packages/create-jest/src/index.ts | 2 +- packages/create-jest/src/runCreate.ts | 34 +++++++++++++++++++++++-- packages/create-jest/tsconfig.json | 1 + yarn.lock | 5 ++++ 6 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/create-jest/bin/create-jest.js b/packages/create-jest/bin/create-jest.js index d611d2c99c76..09887467fcb9 100755 --- a/packages/create-jest/bin/create-jest.js +++ b/packages/create-jest/bin/create-jest.js @@ -9,5 +9,5 @@ const importLocal = require('import-local'); if (!importLocal(__filename)) { - require('..').runCreate(); + require('..').runCLI(); } diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index f572830b2dcc..4e8810cbf561 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -20,12 +20,15 @@ "./bin/create-jest": "./bin/create-jest.js" }, "dependencies": { + "@jest/core": "workspace:^", "@jest/types": "workspace:^", "chalk": "^4.0.0", + "exit": "^0.1.2", "graceful-fs": "^4.2.9", "jest-config": "workspace:^", "jest-util": "workspace:^", - "prompts": "^2.0.1" + "prompts": "^2.0.1", + "yargs": "^17.3.1" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -34,7 +37,9 @@ "access": "public" }, "devDependencies": { + "@types/exit": "^0.1.30", "@types/graceful-fs": "^4.1.3", - "@types/prompts": "^2.0.1" + "@types/prompts": "^2.0.1", + "@types/yargs": "^17.0.8" } } diff --git a/packages/create-jest/src/index.ts b/packages/create-jest/src/index.ts index fb5b27b18a34..9fd75c54b7c5 100644 --- a/packages/create-jest/src/index.ts +++ b/packages/create-jest/src/index.ts @@ -5,4 +5,4 @@ * LICENSE file in the root directory of this source tree. */ -export {runCreate} from './runCreate'; +export {runCreate, runCLI} from './runCreate'; diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index 898fad5e654f..c99735bd72f8 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -7,10 +7,13 @@ import * as path from 'path'; import chalk = require('chalk'); +import exit = require('exit'); import * as fs from 'graceful-fs'; import prompts = require('prompts'); +import yargs = require('yargs'); +import {getVersion} from '@jest/core'; import {constants} from 'jest-config'; -import {tryRealpath} from 'jest-util'; +import {clearLine, tryRealpath} from 'jest-util'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; import generateConfigFile from './generateConfigFile'; import modifyPackageJson from './modifyPackageJson'; @@ -28,9 +31,36 @@ const { const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; +export async function runCLI(): Promise { + try { + const argv = await yargs(process.argv.slice(2)) + .usage('Usage: $0 [rootDir]') + .version(getVersion()) + .alias('help', 'h') + .epilogue('Documentation: https://jestjs.io/').argv; + + const rootDir = + typeof argv._[0] !== 'undefined' ? String(argv._[0]) : undefined; + + await runCreate(rootDir); + } catch (error: any) { + clearLine(process.stderr); + clearLine(process.stdout); + if (error?.stack) { + console.error(chalk.red(error.stack)); + } else { + console.error(chalk.red(error)); + } + + exit(1); + throw error; + } +} + export async function runCreate( - rootDir: string = tryRealpath(process.cwd()), + rootDir: string = process.cwd(), ): Promise { + rootDir = tryRealpath(rootDir); // prerequisite checks const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); diff --git a/packages/create-jest/tsconfig.json b/packages/create-jest/tsconfig.json index 0f1cd728f463..7875ef207503 100644 --- a/packages/create-jest/tsconfig.json +++ b/packages/create-jest/tsconfig.json @@ -8,6 +8,7 @@ "exclude": ["./**/__tests__/**/*"], "references": [ {"path": "../jest-config"}, + {"path": "../jest-core"}, {"path": "../jest-types"}, {"path": "../jest-util"} ] diff --git a/yarn.lock b/yarn.lock index f446e5f02cbb..0ee46316c175 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8025,14 +8025,19 @@ __metadata: version: 0.0.0-use.local resolution: "create-jest@workspace:packages/create-jest" dependencies: + "@jest/core": "workspace:^" "@jest/types": "workspace:^" + "@types/exit": ^0.1.30 "@types/graceful-fs": ^4.1.3 "@types/prompts": ^2.0.1 + "@types/yargs": ^17.0.8 chalk: ^4.0.0 + exit: ^0.1.2 graceful-fs: ^4.2.9 jest-config: "workspace:^" jest-util: "workspace:^" prompts: ^2.0.1 + yargs: ^17.3.1 bin: create-jest: ./bin/create-jest.js languageName: unknown From 66384ac411050927beccbdc2d53db8e3896a8d8f Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 02:06:58 +0400 Subject: [PATCH 08/25] fix: Typesafe values operators for `create-jest` package --- .../__tests__/basic-support.test.js | 1 + packages/create-jest/src/generateConfigFile.ts | 5 +++-- packages/create-jest/src/runCreate.ts | 15 ++++++++------- packages/create-jest/src/types.ts | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 e2e/to-match-inline-snapshot/__tests__/basic-support.test.js diff --git a/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js b/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js new file mode 100644 index 000000000000..436424ae8b46 --- /dev/null +++ b/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js @@ -0,0 +1 @@ +test('inline snapshots', () => expect({apple: "original value"}).toMatchInlineSnapshot()); \ No newline at end of file diff --git a/packages/create-jest/src/generateConfigFile.ts b/packages/create-jest/src/generateConfigFile.ts index 8ac4f93ba3e9..afd6fce7ce18 100644 --- a/packages/create-jest/src/generateConfigFile.ts +++ b/packages/create-jest/src/generateConfigFile.ts @@ -7,13 +7,14 @@ import type {Config} from '@jest/types'; import {defaults, descriptions} from 'jest-config'; +import type {PromptsResults} from './types'; const stringifyOption = ( option: keyof Config.InitialOptions, map: Partial, linePrefix = '', ): string => { - const optionDescription = ` // ${descriptions[option]}`; + const optionDescription = ` // ${descriptions[option] ?? ''}`; const stringifiedObject = `${option}: ${JSON.stringify( map[option], null, @@ -27,7 +28,7 @@ const stringifyOption = ( }; const generateConfigFile = ( - results: Record, + results: PromptsResults, generateEsm = false, ): string => { const {useTypescript, coverage, coverageProvider, clearMocks, environment} = diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index c99735bd72f8..4714d9976ddf 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -43,10 +43,10 @@ export async function runCLI(): Promise { typeof argv._[0] !== 'undefined' ? String(argv._[0]) : undefined; await runCreate(rootDir); - } catch (error: any) { + } catch (error: unknown) { clearLine(process.stderr); clearLine(process.stdout); - if (error?.stack) { + if (error instanceof Error && Boolean(error?.stack)) { console.error(chalk.red(error.stack)); } else { console.error(chalk.red(error)); @@ -75,7 +75,7 @@ export async function runCreate( try { projectPackageJson = JSON.parse( fs.readFileSync(projectPackageJsonPath, 'utf-8'), - ); + ) as ProjectPackageJson; } catch { throw new MalformedPackageJsonError(projectPackageJsonPath); } @@ -88,7 +88,7 @@ export async function runCreate( fs.existsSync(path.join(rootDir, getConfigFilename(ext))), ); - if (hasJestProperty || existingJestConfigExt) { + if (hasJestProperty || Boolean(existingJestConfigExt)) { const result: {continue: boolean} = await prompts({ initial: true, message: @@ -142,9 +142,10 @@ export async function runCreate( : JEST_CONFIG_EXT_JS; // Determine Jest config path - const jestConfigPath = existingJestConfigExt - ? getConfigFilename(existingJestConfigExt) - : path.join(rootDir, getConfigFilename(jestConfigFileExt)); + const jestConfigPath = + typeof existingJestConfigExt !== 'undefined' + ? getConfigFilename(existingJestConfigExt) + : path.join(rootDir, getConfigFilename(jestConfigFileExt)); const shouldModifyScripts = results.scripts; diff --git a/packages/create-jest/src/types.ts b/packages/create-jest/src/types.ts index 639c22e10da8..07432fab121d 100644 --- a/packages/create-jest/src/types.ts +++ b/packages/create-jest/src/types.ts @@ -17,7 +17,7 @@ export type PromptsResults = { useTypescript: boolean; clearMocks: boolean; coverage: boolean; - coverageProvider: boolean; - environment: boolean; + coverageProvider: string; + environment: string; scripts: boolean; }; From e8d20439d0d8d8db2214c714b120bca7dade7153 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 02:15:24 +0400 Subject: [PATCH 09/25] fix: Extra diff --- e2e/to-match-inline-snapshot/__tests__/basic-support.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js b/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js index 436424ae8b46..91983c3f64ec 100644 --- a/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js +++ b/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js @@ -1 +1,2 @@ -test('inline snapshots', () => expect({apple: "original value"}).toMatchInlineSnapshot()); \ No newline at end of file +test('inline snapshots', () => + expect({apple: 'original value'}).toMatchInlineSnapshot()); From d1607880969b8576a64e4c5916c0dc49b7cf97a1 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 02:17:56 +0400 Subject: [PATCH 10/25] fix: Remove redundant file --- e2e/to-match-inline-snapshot/__tests__/basic-support.test.js | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 e2e/to-match-inline-snapshot/__tests__/basic-support.test.js diff --git a/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js b/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js deleted file mode 100644 index 91983c3f64ec..000000000000 --- a/e2e/to-match-inline-snapshot/__tests__/basic-support.test.js +++ /dev/null @@ -1,2 +0,0 @@ -test('inline snapshots', () => - expect({apple: 'original value'}).toMatchInlineSnapshot()); From c9b4ecb0ecf27d032e302754917368ecd5c8ff9b Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 10:51:37 +0400 Subject: [PATCH 11/25] Update packages/create-jest/src/runCreate.ts Co-authored-by: Tom Mrazauskas --- packages/create-jest/src/runCreate.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index 4714d9976ddf..fb9fb96db83d 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -57,9 +57,7 @@ export async function runCLI(): Promise { } } -export async function runCreate( - rootDir: string = process.cwd(), -): Promise { +export async function runCreate(rootDir = process.cwd()): Promise { rootDir = tryRealpath(rootDir); // prerequisite checks const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); From b126c72f4b6dd8e79632e6f941e8d0dcf7fd8108 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:03:43 +0400 Subject: [PATCH 12/25] fix: Shortcut for nullable values check --- packages/create-jest/src/runCreate.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index 4714d9976ddf..bc1437d625be 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -57,9 +57,7 @@ export async function runCLI(): Promise { } } -export async function runCreate( - rootDir: string = process.cwd(), -): Promise { +export async function runCreate(rootDir = process.cwd()): Promise { rootDir = tryRealpath(rootDir); // prerequisite checks const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); @@ -88,7 +86,7 @@ export async function runCreate( fs.existsSync(path.join(rootDir, getConfigFilename(ext))), ); - if (hasJestProperty || Boolean(existingJestConfigExt)) { + if (hasJestProperty || existingJestConfigExt != null) { const result: {continue: boolean} = await prompts({ initial: true, message: @@ -143,7 +141,7 @@ export async function runCreate( // Determine Jest config path const jestConfigPath = - typeof existingJestConfigExt !== 'undefined' + existingJestConfigExt != null ? getConfigFilename(existingJestConfigExt) : path.join(rootDir, getConfigFilename(jestConfigFileExt)); From 7b366bebe336ad8080546c611dddd26a90444126 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:04:19 +0400 Subject: [PATCH 13/25] chore: Add `create-jest` to typecheck tests configuration --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6e7271f5ace3..f268055dbcd7 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "test": "yarn lint && yarn jest", "typecheck": "yarn typecheck:examples && yarn typecheck:tests", "typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit", - "typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect,expect-utils,jest-circus,jest-cli,jest-config,jest-console,jest-snapshot,jest-util,jest-validate,jest-watcher,jest-worker,pretty-format}/**/__tests__", + "typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,create-jest,diff-sequences,expect,expect-utils,jest-circus,jest-cli,jest-config,jest-console,jest-snapshot,jest-util,jest-validate,jest-watcher,jest-worker,pretty-format}/**/__tests__", "verify-old-ts": "node ./scripts/verifyOldTs.mjs", "verify-pnp": "node ./scripts/verifyPnP.mjs", "watch": "yarn build:js && node ./scripts/watch.mjs", From 0e9aed88ce48b3f1fb5e1126335fcba3771f81ed Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:05:09 +0400 Subject: [PATCH 14/25] fix: Remove unnecessary type declaration --- packages/create-jest/src/runCreate.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index bc1437d625be..c23eaeec78ac 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -60,7 +60,7 @@ export async function runCLI(): Promise { export async function runCreate(rootDir = process.cwd()): Promise { rootDir = tryRealpath(rootDir); // prerequisite checks - const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); + const projectPackageJsonPath = path.join(rootDir, PACKAGE_JSON); if (!fs.existsSync(projectPackageJsonPath)) { throw new NotFoundPackageJsonError(rootDir); From 48b3d1d3a685e2df3e31df7955c401a8cd3770b1 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:10:34 +0400 Subject: [PATCH 15/25] fix: Remove unnecessary `local` check --- packages/create-jest/bin/create-jest.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/create-jest/bin/create-jest.js b/packages/create-jest/bin/create-jest.js index 09887467fcb9..58c8758edb32 100755 --- a/packages/create-jest/bin/create-jest.js +++ b/packages/create-jest/bin/create-jest.js @@ -5,9 +5,4 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ - -const importLocal = require('import-local'); - -if (!importLocal(__filename)) { - require('..').runCLI(); -} +require('..').runCLI(); From bbae051910f2a0006a0d88212514aa912f48ae90 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:13:25 +0400 Subject: [PATCH 16/25] fix: Remove `bin` from exports --- packages/create-jest/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index 4e8810cbf561..a62bb4f8ed39 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -16,8 +16,7 @@ "types": "./build/index.d.ts", "default": "./build/index.js" }, - "./package.json": "./package.json", - "./bin/create-jest": "./bin/create-jest.js" + "./package.json": "./package.json" }, "dependencies": { "@jest/core": "workspace:^", From 0b4ea0f0a187a87affadcb57143c3106da2dab15 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:24:07 +0400 Subject: [PATCH 17/25] fix: Remove extra CLI interface --- packages/create-jest/package.json | 7 ++----- packages/create-jest/src/runCreate.ts | 12 +----------- packages/create-jest/tsconfig.json | 1 - yarn.lock | 3 --- 4 files changed, 3 insertions(+), 20 deletions(-) diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index a62bb4f8ed39..4d6e47859e5a 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -19,15 +19,13 @@ "./package.json": "./package.json" }, "dependencies": { - "@jest/core": "workspace:^", "@jest/types": "workspace:^", "chalk": "^4.0.0", "exit": "^0.1.2", "graceful-fs": "^4.2.9", "jest-config": "workspace:^", "jest-util": "workspace:^", - "prompts": "^2.0.1", - "yargs": "^17.3.1" + "prompts": "^2.0.1" }, "engines": { "node": "^14.15.0 || ^16.10.0 || >=18.0.0" @@ -38,7 +36,6 @@ "devDependencies": { "@types/exit": "^0.1.30", "@types/graceful-fs": "^4.1.3", - "@types/prompts": "^2.0.1", - "@types/yargs": "^17.0.8" + "@types/prompts": "^2.0.1" } } diff --git a/packages/create-jest/src/runCreate.ts b/packages/create-jest/src/runCreate.ts index c23eaeec78ac..7a8d2fbb0538 100644 --- a/packages/create-jest/src/runCreate.ts +++ b/packages/create-jest/src/runCreate.ts @@ -10,8 +10,6 @@ import chalk = require('chalk'); import exit = require('exit'); import * as fs from 'graceful-fs'; import prompts = require('prompts'); -import yargs = require('yargs'); -import {getVersion} from '@jest/core'; import {constants} from 'jest-config'; import {clearLine, tryRealpath} from 'jest-util'; import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors'; @@ -33,15 +31,7 @@ const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; export async function runCLI(): Promise { try { - const argv = await yargs(process.argv.slice(2)) - .usage('Usage: $0 [rootDir]') - .version(getVersion()) - .alias('help', 'h') - .epilogue('Documentation: https://jestjs.io/').argv; - - const rootDir = - typeof argv._[0] !== 'undefined' ? String(argv._[0]) : undefined; - + const rootDir = process.argv[2]; await runCreate(rootDir); } catch (error: unknown) { clearLine(process.stderr); diff --git a/packages/create-jest/tsconfig.json b/packages/create-jest/tsconfig.json index 7875ef207503..0f1cd728f463 100644 --- a/packages/create-jest/tsconfig.json +++ b/packages/create-jest/tsconfig.json @@ -8,7 +8,6 @@ "exclude": ["./**/__tests__/**/*"], "references": [ {"path": "../jest-config"}, - {"path": "../jest-core"}, {"path": "../jest-types"}, {"path": "../jest-util"} ] diff --git a/yarn.lock b/yarn.lock index 0ee46316c175..99ad31a505bd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8025,19 +8025,16 @@ __metadata: version: 0.0.0-use.local resolution: "create-jest@workspace:packages/create-jest" dependencies: - "@jest/core": "workspace:^" "@jest/types": "workspace:^" "@types/exit": ^0.1.30 "@types/graceful-fs": ^4.1.3 "@types/prompts": ^2.0.1 - "@types/yargs": ^17.0.8 chalk: ^4.0.0 exit: ^0.1.2 graceful-fs: ^4.2.9 jest-config: "workspace:^" jest-util: "workspace:^" prompts: ^2.0.1 - yargs: ^17.3.1 bin: create-jest: ./bin/create-jest.js languageName: unknown From 8fe5b8ddf91bec85d4a3c5c829fd9f8872d8bd9a Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 31 Aug 2023 11:28:09 +0400 Subject: [PATCH 18/25] fix: Restore `bin` export --- packages/create-jest/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/create-jest/package.json b/packages/create-jest/package.json index 4d6e47859e5a..9daa812dda0d 100644 --- a/packages/create-jest/package.json +++ b/packages/create-jest/package.json @@ -16,7 +16,8 @@ "types": "./build/index.d.ts", "default": "./build/index.js" }, - "./package.json": "./package.json" + "./package.json": "./package.json", + "./bin/create-jest": "./bin/create-jest.js" }, "dependencies": { "@jest/types": "workspace:^", From 513ad24cd6c35ecb92e4ef1fbf726e51f2393dad Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sat, 2 Sep 2023 11:42:08 +0400 Subject: [PATCH 19/25] fix: New package name in error message --- packages/create-jest/src/errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-jest/src/errors.ts b/packages/create-jest/src/errors.ts index 7c6afb886893..1e9c3902fe46 100644 --- a/packages/create-jest/src/errors.ts +++ b/packages/create-jest/src/errors.ts @@ -18,7 +18,7 @@ export class MalformedPackageJsonError extends Error { constructor(packageJsonPath: string) { super( `There is malformed json in ${packageJsonPath}\n` + - 'Fix it, and then run "jest --init"', + 'Fix it, and then run "create-jest" again', ); this.name = ''; // eslint-disable-next-line @typescript-eslint/no-empty-function From dedea5fea35c187bab79a0038652a8d606664677 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sat, 2 Sep 2023 11:58:52 +0400 Subject: [PATCH 20/25] docs: Add `create-jest` usage --- README.md | 2 +- docs/GettingStarted.md | 4 ++-- packages/create-jest/README.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ba462358a145..8dc4a0b4e02f 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ If you'd like to learn more about running `jest` through the command line, take 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 +yarn create jest ``` ### Using Babel diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index 04ac8182d165..49643ff8ef0d 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -67,8 +67,8 @@ If you'd like to learn more about running `jest` through the command line, take 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 +```bash npm2yarn +npm init jest@latest ``` ### Using Babel diff --git a/packages/create-jest/README.md b/packages/create-jest/README.md index 002699d38cd2..312a0a817ff5 100644 --- a/packages/create-jest/README.md +++ b/packages/create-jest/README.md @@ -7,5 +7,5 @@ npm init jest@latest # Or for Yarn yarn create jest # Or for pnpm -pnpm dlx create-jest +pnpm create jest ``` From 8ae6c55bbaada53ecb763442f1018416e9da0987 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Sat, 2 Sep 2023 12:29:58 +0400 Subject: [PATCH 21/25] fix: More generic error message for malformed json in `create-jest` --- packages/create-jest/src/errors.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/create-jest/src/errors.ts b/packages/create-jest/src/errors.ts index 1e9c3902fe46..2e0d689f525b 100644 --- a/packages/create-jest/src/errors.ts +++ b/packages/create-jest/src/errors.ts @@ -16,10 +16,7 @@ export class NotFoundPackageJsonError extends Error { export class MalformedPackageJsonError extends Error { constructor(packageJsonPath: string) { - super( - `There is malformed json in ${packageJsonPath}\n` + - 'Fix it, and then run "create-jest" again', - ); + super(`There is malformed json in ${packageJsonPath}`); this.name = ''; // eslint-disable-next-line @typescript-eslint/no-empty-function Error.captureStackTrace(this, () => {}); From 77adb362a3d915dd803038dfc33c88589b5ad5c0 Mon Sep 17 00:00:00 2001 From: Ilya Kuznetsov Date: Thu, 7 Sep 2023 10:50:09 +0400 Subject: [PATCH 22/25] docs: Add `create-jest` to the changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 655628f438dd..0ce2bc5c5d9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[create-jest]` Add `npm init` / `yarn create` initialiser for Jest projects ([#14465](https://github.com/jestjs/jest/pull/14453)) + ### Fixes - `[jest-snapshot]` Allow for strings as well as template literals in inline snapshots ([#14465](https://github.com/jestjs/jest/pull/14465)) @@ -13,6 +15,8 @@ ### Chore & Maintenance +- `[jest-cli]` Move internal config initialisation logic to the `create-jest` package ([#14465](https://github.com/jestjs/jest/pull/14453)) + ## 29.6.4 ### Fixes From 0dcf317b12cec35372f35bede240bb2bc651eee8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Sep 2023 10:14:34 +0200 Subject: [PATCH 23/25] Update packages/create-jest/src/generateConfigFile.ts --- packages/create-jest/src/generateConfigFile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/create-jest/src/generateConfigFile.ts b/packages/create-jest/src/generateConfigFile.ts index afd6fce7ce18..321d9831bc1e 100644 --- a/packages/create-jest/src/generateConfigFile.ts +++ b/packages/create-jest/src/generateConfigFile.ts @@ -14,7 +14,7 @@ const stringifyOption = ( map: Partial, linePrefix = '', ): string => { - const optionDescription = ` // ${descriptions[option] ?? ''}`; + const optionDescription = descriptions[option] ? ` // ${descriptions[option]}` : ''; const stringifiedObject = `${option}: ${JSON.stringify( map[option], null, From cc0e430f93efbf522f04fa3eb8eac833e9905399 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Sep 2023 10:19:46 +0200 Subject: [PATCH 24/25] purdy --- packages/create-jest/src/generateConfigFile.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/create-jest/src/generateConfigFile.ts b/packages/create-jest/src/generateConfigFile.ts index 321d9831bc1e..b52cc77a55ee 100644 --- a/packages/create-jest/src/generateConfigFile.ts +++ b/packages/create-jest/src/generateConfigFile.ts @@ -14,7 +14,9 @@ const stringifyOption = ( map: Partial, linePrefix = '', ): string => { - const optionDescription = descriptions[option] ? ` // ${descriptions[option]}` : ''; + const optionDescription = descriptions[option] + ? ` // ${descriptions[option]}` + : ''; const stringifiedObject = `${option}: ${JSON.stringify( map[option], null, From 9e3ddfa6c2e61a70853369a8e5e97af28e7e8b57 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 7 Sep 2023 11:02:33 +0200 Subject: [PATCH 25/25] strict --- packages/create-jest/src/generateConfigFile.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/create-jest/src/generateConfigFile.ts b/packages/create-jest/src/generateConfigFile.ts index b52cc77a55ee..5f774b163091 100644 --- a/packages/create-jest/src/generateConfigFile.ts +++ b/packages/create-jest/src/generateConfigFile.ts @@ -14,9 +14,9 @@ const stringifyOption = ( map: Partial, linePrefix = '', ): string => { - const optionDescription = descriptions[option] - ? ` // ${descriptions[option]}` - : ''; + const description = descriptions[option]; + const optionDescription = + description != null && description.length > 0 ? ` // ${description}` : ''; const stringifiedObject = `${option}: ${JSON.stringify( map[option], null,