### Install Babel Preset for TypeScript (npm) Source: https://jestjs.io/docs/getting-started/index Installs the Babel preset for TypeScript support for Jest. This is a development dependency. ```bash npm install --save-dev @babel/preset-typescript ``` -------------------------------- ### Install ts-jest for TypeScript (npm) Source: https://jestjs.io/docs/getting-started/index Installs ts-jest, a TypeScript preprocessor with source map support for Jest, as a development dependency. ```bash npm install --save-dev ts-jest ``` -------------------------------- ### Install Babel Preset for TypeScript (pnpm) Source: https://jestjs.io/docs/getting-started/index Installs the Babel preset for TypeScript support for Jest using pnpm. This is a development dependency. ```bash pnpm add --save-dev @babel/preset-typescript ``` -------------------------------- ### Install Jest with npm, Yarn, or pnpm Source: https://jestjs.io/docs/getting-started/index Install Jest as a development dependency using your preferred package manager. This command adds Jest to your project's devDependencies. ```bash npm install --save-dev jest ``` ```bash yarn add --dev jest ``` ```bash pnpm add --save-dev jest ``` -------------------------------- ### Install Babel Preset for TypeScript (Yarn) Source: https://jestjs.io/docs/getting-started/index Installs the Babel preset for TypeScript support for Jest using Yarn. This is a development dependency. ```bash yarn add --dev @babel/preset-typescript ``` -------------------------------- ### Install ts-jest for TypeScript (pnpm) Source: https://jestjs.io/docs/getting-started/index Installs ts-jest using pnpm, a TypeScript preprocessor with source map support for Jest, as a development dependency. ```bash pnpm add --save-dev ts-jest ``` -------------------------------- ### Install ts-jest for TypeScript (Yarn) Source: https://jestjs.io/docs/getting-started/index Installs ts-jest using Yarn, a TypeScript preprocessor with source map support for Jest, as a development dependency. ```bash yarn add --dev ts-jest ``` -------------------------------- ### Run Jest tests from the command line Source: https://jestjs.io/docs/getting-started/index Execute Jest tests directly from the command line with various options. This example shows how to run tests matching 'my-test', use a configuration file, and enable notifications. ```bash jest my-test --notify --config=config.json ``` -------------------------------- ### Install @types/jest for Jest Globals (npm) Source: https://jestjs.io/docs/getting-started/index Installs the @types/jest package using npm, which provides types for Jest globals without explicit imports. ```bash npm install --save-dev @types/jest ``` -------------------------------- ### Install @types/jest for Jest Globals (Yarn) Source: https://jestjs.io/docs/getting-started/index Installs the @types/jest package using Yarn, which provides types for Jest globals without explicit imports. ```bash yarn add --dev @types/jest ``` -------------------------------- ### Install @types/jest for Jest Globals (pnpm) Source: https://jestjs.io/docs/getting-started/index Installs the @types/jest package using pnpm, which provides types for Jest globals without explicit imports. ```bash pnpm add --save-dev @types/jest ``` -------------------------------- ### Install Jest Globals Type Definitions (npm) Source: https://jestjs.io/docs/getting-started/index Installs the Jest globals type definitions package for TypeScript projects using npm. This provides type safety for Jest APIs. ```bash npm install --save-dev @jest/globals ``` -------------------------------- ### Generate a basic Jest configuration file Source: https://jestjs.io/docs/getting-started/index Initialize Jest and generate a basic configuration file by running the respective commands for npm, Yarn, or pnpm. Jest will prompt you with questions to customize the configuration. ```bash npm init jest@latest ``` ```bash yarn create jest ``` ```bash pnpm create jest ``` -------------------------------- ### Install Jest Globals Type Definitions (pnpm) Source: https://jestjs.io/docs/getting-started/index Installs the Jest globals type definitions package for TypeScript projects using pnpm. This provides type safety for Jest APIs. ```bash pnpm add --save-dev @jest/globals ``` -------------------------------- ### Install Jest Globals Type Definitions (Yarn) Source: https://jestjs.io/docs/getting-started/index Installs the Jest globals type definitions package for TypeScript projects using Yarn. This provides type safety for Jest APIs. ```bash yarn add --dev @jest/globals ``` -------------------------------- ### Install Babel dependencies for Jest Source: https://jestjs.io/docs/getting-started/index Install the necessary Babel packages (`babel-jest`, `@babel/core`, `@babel/preset-env`) as development dependencies using npm, Yarn, or pnpm. These are required for Jest to process modern JavaScript syntax. ```bash npm install --save-dev babel-jest @babel/core @babel/preset-env ``` ```bash yarn add --dev babel-jest @babel/core @babel/preset-env ``` ```bash pnpm add --save-dev babel-jest @babel/core @babel/preset-env ``` -------------------------------- ### Configure Babel for Jest Source: https://jestjs.io/docs/getting-started/index Create a `babel.config.js` file to configure Babel for your project, targeting the current Node.js version. This setup ensures Jest can transpile modern JavaScript. ```javascript module.exports = { presets: [['@babel/preset-env', {targets: {node: 'current'}}]], }; ``` -------------------------------- ### Configure ESLint with eslint-plugin-jest Source: https://jestjs.io/docs/getting-started/index Configures ESLint to enable Jest globals using the 'eslint-plugin-jest'. This is an alternative to configuring globals directly. ```json { "overrides": [ { "files": ["tests/**/*"], "plugins": ["jest"], "env": { "jest/globals": true } } ] } ``` -------------------------------- ### Configure Babel for TypeScript Source: https://jestjs.io/docs/getting-started/index Configures Babel to use the preset-env and preset-typescript for Jest. This allows Jest to transpile TypeScript code. ```javascript module.exports = { presets: [ ['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript', ], }; ``` -------------------------------- ### Configure package.json to run Jest tests Source: https://jestjs.io/docs/getting-started/index Add a 'test' script to your `package.json` file to easily run Jest tests using `npm test` or `yarn test`. This script executes the Jest command. ```json { "scripts": { "test": "jest" } } ``` -------------------------------- ### Configure ESLint with Jest Globals Source: https://jestjs.io/docs/getting-started/index Configures ESLint to recognize Jest globals by extending the 'globals.jest' environment. This avoids 'no-undef' errors. ```javascript import {defineConfig} from 'eslint/config'; import globals from 'globals'; export default defineConfig([ { files: ['**/*.js'], languageOptions: { globals: { ...globals.jest, }, }, rules: { 'no-unused-vars': 'warn', 'no-undef': 'warn', }, }, ]); ``` -------------------------------- ### Import Jest APIs in TypeScript Test File Source: https://jestjs.io/docs/getting-started/index Demonstrates importing Jest APIs like describe, expect, and test from '@jest/globals' in a TypeScript test file. ```typescript import {describe, expect, test} from '@jest/globals'; import {sum} from './sum'; describe('sum module', () => { test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); }); }); ``` -------------------------------- ### Disable Babel transformation in Jest configuration Source: https://jestjs.io/docs/getting-started/index If `babel-jest` is automatically transforming files and you want to prevent this, explicitly reset the `transform` option in your `jest.config.js` file to an empty object. ```javascript module.exports = { transform: {}, }; ``` -------------------------------- ### Conditionally configure Babel for Jest environment Source: https://jestjs.io/docs/getting-started/index Use Jest's environment variable (`process.env.NODE_ENV`) within your Babel configuration to conditionally apply transformations. This allows for environment-specific compilation settings. ```javascript module.exports = api => { const isTest = api.env('test'); // You can use isTest to determine what presets and plugins to use. return { // ... }; }; ``` -------------------------------- ### Write a basic Jest test for a sum function Source: https://jestjs.io/docs/getting-started/index Create a JavaScript file for your function and a corresponding test file using Jest's `test` and `expect` syntax. The test verifies that the `sum` function correctly adds two numbers. ```javascript function sum(a, b) { return a + b; } module.exports = sum; ``` ```javascript const sum = require('./sum'); test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.