-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(create-jest): Add npm init
/ yarn create
initialiser
#14453
Changes from 11 commits
30e957f
5b580b1
e804edb
546db25
e503b28
0ffab46
df988bf
92db787
66384ac
e8d2043
d160788
c9b4ecb
b126c72
7b366be
0e9aed8
48b3d1d
bbae051
0b4ea0f
8fe5b8d
810fec8
513ad24
dedea5f
8ae6c55
8ca2c6a
77adb36
0dcf317
cc0e430
9e3ddfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
**/__mocks__/** | ||
**/__tests__/** | ||
__typetests__ | ||
src | ||
tsconfig.json | ||
tsconfig.tsbuildinfo | ||
api-extractor.json | ||
.eslintcache |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('..').runCLI(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"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": "./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" | ||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
"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" | ||
}, | ||
"engines": { | ||
"node": "^14.15.0 || ^16.10.0 || >=18.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@types/exit": "^0.1.30", | ||
"@types/graceful-fs": "^4.1.3", | ||
"@types/prompts": "^2.0.1", | ||
"@types/yargs": "^17.0.8" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "../../../../tsconfig.test.json", | ||
"include": ["./**/*"], | ||
"references": [{"path": "../../"}] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
export {runCreate, runCLI} from './runCreate'; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thinking out loud: it feels too much to ship Currently this package is 15.5 kB (unpacked). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds fair, I think keeping this package light is more important than having universal parsing for arguments and |
||||||
import {getVersion} from '@jest/core'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Importing |
||||||
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 default async function init( | ||||||
rootDir: string = tryRealpath(process.cwd()), | ||||||
export async function runCLI(): Promise<void> { | ||||||
try { | ||||||
const argv = await yargs(process.argv.slice(2)) | ||||||
.usage('Usage: $0 [rootDir]') | ||||||
.version(getVersion()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is decided to keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||||||
.alias('help', 'h') | ||||||
.epilogue('Documentation: https://jestjs.io/').argv; | ||||||
|
||||||
const rootDir = | ||||||
typeof argv._[0] !== 'undefined' ? String(argv._[0]) : undefined; | ||||||
|
||||||
await runCreate(rootDir); | ||||||
} catch (error: unknown) { | ||||||
clearLine(process.stderr); | ||||||
clearLine(process.stdout); | ||||||
if (error instanceof Error && Boolean(error?.stack)) { | ||||||
console.error(chalk.red(error.stack)); | ||||||
} else { | ||||||
console.error(chalk.red(error)); | ||||||
} | ||||||
|
||||||
exit(1); | ||||||
throw error; | ||||||
} | ||||||
} | ||||||
|
||||||
export async function runCreate( | ||||||
rootDir: string = process.cwd(), | ||||||
): Promise<void> { | ||||||
dj-stormtrooper marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
rootDir = tryRealpath(rootDir); | ||||||
// prerequisite checks | ||||||
const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting why ESLint did not catch this:
Suggested change
|
||||||
|
||||||
|
@@ -45,7 +75,7 @@ export default async function init( | |||||
try { | ||||||
projectPackageJson = JSON.parse( | ||||||
fs.readFileSync(projectPackageJsonPath, 'utf-8'), | ||||||
); | ||||||
) as ProjectPackageJson; | ||||||
} catch { | ||||||
throw new MalformedPackageJsonError(projectPackageJsonPath); | ||||||
} | ||||||
|
@@ -58,7 +88,7 @@ export default async function init( | |||||
fs.existsSync(path.join(rootDir, getConfigFilename(ext))), | ||||||
); | ||||||
|
||||||
if (hasJestProperty || existingJestConfigExt) { | ||||||
if (hasJestProperty || Boolean(existingJestConfigExt)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const result: {continue: boolean} = await prompts({ | ||||||
initial: true, | ||||||
message: | ||||||
|
@@ -112,9 +142,10 @@ export default async function init( | |||||
: JEST_CONFIG_EXT_JS; | ||||||
|
||||||
// Determine Jest config path | ||||||
const jestConfigPath = existingJestConfigExt | ||||||
? getConfigFilename(existingJestConfigExt) | ||||||
: path.join(rootDir, getConfigFilename(jestConfigFileExt)); | ||||||
const jestConfigPath = | ||||||
typeof existingJestConfigExt !== 'undefined' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
? getConfigFilename(existingJestConfigExt) | ||||||
: path.join(rootDir, getConfigFilename(jestConfigFileExt)); | ||||||
|
||||||
const shouldModifyScripts = results.scripts; | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ export type PromptsResults = { | |
useTypescript: boolean; | ||
clearMocks: boolean; | ||
coverage: boolean; | ||
coverageProvider: boolean; | ||
environment: boolean; | ||
coverageProvider: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the extra diff, I had to fix I decided to do a good thing and fix the errors instead of excluding the package from the script |
||
environment: string; | ||
scripts: boolean; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "build" | ||
}, | ||
"include": ["./src/**/*"], | ||
"exclude": ["./**/__tests__/**/*"], | ||
"references": [ | ||
{"path": "../jest-config"}, | ||
{"path": "../jest-core"}, | ||
{"path": "../jest-types"}, | ||
{"path": "../jest-util"} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm..
import-local
is not included independencies
list?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not so sure if
import-local
is needed in this case. It does not make sense installingcreate-jest
locally. To be honest, I don’t understand whatimport-local
does ;D So it can be I missed something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with removing that, local installation indeed seems strange in this case