Files
openidea/docs/zh/languages/javascript.mdx
T
LU XIANGRUIandwildcatDownstairs 9b4ee2a6ff feat(run): run Playwright tests with a test tree and Rerun Failed Tests (#5169)
A Playwright project had no test tree and no Rerun Failed Tests: its output went through the Jest / Vitest parser, single tests ran with Jest's `-t`, and projects without a `test` script weren't detected at all.

- Detection: a `package.json` script that runs `playwright test` is a test configuration and records `testRunner: "playwright"` (new optional `RunExtras.test_runner`, `test-runner` in `run.toml`); a package with a `playwright.config.*` that no script runs gets `npx playwright test`.
- Test tree: test runs add `--reporter=list` unless a reporter is named, parsed into file › [project] › describe chain › test with status, duration, location and failure details.
- Gutter: `test.describe` blocks and `fixme` / `fail` / `serial` tests get run / debug icons and run by declaration line (`file:line`); in a project with Playwright and another runner, specs run on Playwright's configuration and other tests on the other runner's.
- Rerun Failed Tests / run selected tests: the files plus `--grep` on the describe chain and title, with `--project` when the run named projects.

New fixture `fixtures/playwright-app`; `smoke/run.test.ts` runs it after `ts-app`.

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-22 14:30:06 +08:00

115 lines
12 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "JavaScript 与 TypeScript"
description: "JavaScript、TypeScript、Vue、Svelte、Astro 和 Angular 的语言服务器、Node.js 识别、npm 脚本、测试、调试与框架支持。"
---
OpenIDEA 打开 JavaScript(`.js`、`.mjs`、`.cjs`、`.jsx`)和 TypeScript(`.ts`、`.mts`、`.cts`、`.tsx`)文件即可获得语法高亮和代码结构,无需任何配置;补全、跳转、重构和诊断由 TypeScript 语言服务器提供。Vue、Svelte、Astro 和 Angular 项目还会额外启动各自框架的语言服务器。所有语言的支持情况见[支持矩阵](/zh/languages/index)。
## 语言服务器
| 文件 | 服务器 | 说明 |
| --------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| JS、JSX、TS、TSX | `vtsls` | 默认的 TypeScript 服务器。 |
| JS、JSX、TS、TSX | `typescript-language-server` | 备选:仅在未安装 `vtsls` 时使用。 |
| `.vue` | `vue-language-server` + `vtsls` | Vue 的 hybrid 模式:`.vue` 中的 TypeScript 由加载了 `@vue/typescript-plugin` 的 `vtsls` 处理。 |
| `.svelte` | `svelte-language-server` | |
| `.astro` | `astro-ls` | 需要 Node.js 和 TypeScript 库。 |
| Angular 项目中的 TS 和 HTML | `angular-language-server`(`ngserver`) | 只在 `angular.json` 或 `node_modules/@angular/core` 所在目录之下启用。 |
| 以上全部 | `oxlint` | 只在存在 `.oxlintrc.json`、`.oxlintrc.jsonc`、`oxlint.config.ts` 或 `oxlint.config.mts` 的目录之下启用。 |
| 以上全部 | `vscode-eslint-language-server`(ESLint) | 只在 ESLint 配置(`eslint.config.*`、`.eslintrc*` 或 `package.json` 中的 `eslintConfig`)所在目录之下启用,运行项目自己的 `eslint`。 |
TypeScript 服务器以 `tsconfig.json`、`jsconfig.json` 或 `package.json` 所在目录为工作区根。不属于任何 `tsconfig.json` / `jsconfig.json` 的文件也会注册给 tsserver,零散脚本同样有代码智能。
有两处行为与服务器默认值不同:重命名符号时不会生成 `import { a as b }` 这样的别名;移动文件时服务器不会自行改写 import,而是由 IDE 先给出需要更新的 import,以预览形式展示,并可撤销。
嵌入提示会显示字面量实参的参数名、推断出的变量类型和枚举成员的值;可以在 **设置 › 编辑器 › 嵌入提示** 中按类型关闭。
### TypeScript 版本
**设置 › 语言和框架 › TypeScript** 决定 `vtsls` 和 `typescript-language-server` 加载哪个 TypeScript:
- **捆绑**(默认):语言服务器自带的 TypeScript。
- 项目中已安装的 `node_modules/typescript`。
- 通过文件夹按钮选择任意 TypeScript `lib` 目录。
Vue、Astro 和 Angular 服务器也会使用这里选择的版本。TypeScript 7(原生移植版)没有语言服务 API,因此 IDE 在安装 `typescript-language-server` 以及 Vue、Svelte、Astro 服务器时会一并安装 `typescript@6`。
## 环境配置
### 安装语言服务器
打开某类文件而对应服务器缺失时,IDE 会弹出通知(例如 “vtsls (TypeScript) 未安装”),提供 **安装** 和 **语言服务器…** 两个按钮。**安装** 会调用 npm(或 pnpm)把包装进 IDE 自己的工具目录,不做全局安装,也不需要 `sudo`。前提是 `PATH` 上有 `npm` 或 `pnpm`。
所有语言服务器、调试器和格式化工具都列在 **设置 › 语言和框架 › 语言服务器** 中,可以在这里安装、重新安装、改用自己的可执行文件,或修改启动参数、环境变量和初始化选项。`PATH` 上已有的服务器也会被直接使用。
| 工具 | 安装方式 |
| -------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
| `vtsls`、`typescript-language-server` | npm(`@vtsls/language-server`;`typescript-language-server` + `typescript@6`) |
| `vue-language-server`、`svelte-language-server`、`astro-ls`、`angular-language-server` | npm |
| `oxlint` | npm |
| Prettier、oxfmt | npm |
| JavaScript 调试器(vscode-js-debug) | GitHub release(`microsoft/vscode-js-debug`) |
如果项目自己的 `node_modules/.bin` 里有 Astro 或 Angular 服务器,会优先使用项目里的。
### JavaScript 运行时
**设置 › 语言和框架 › JavaScript 运行时** 包含:
- **首选运行时**:从行号栏和编辑器的 **运行** 操作运行 JavaScript、TypeScript 文件时用 Node.js、Bun 还是 Deno。**自动** 会从文件所在目录向上找到项目根目录,最近的 `deno.json` / `deno.jsonc` 用 Deno,`bun.lock` / `bun.lockb` 用 Bun,都没有时用 Node.js。
- **Node 解释器**:运行 npm 脚本、Node 文件以及基于 Node 的语言服务器所用的 `node`。**默认** 表示使用 `PATH` 上的 `node`。列表会列出 `PATH` 上找到的所有 Node.js,以及 nvm、Volta、fnm、mise、asdf 安装的版本。
- **在目标中运行**:在本机,或在 **设置 › 构建、执行、部署 › 运行目标** 中配置的某个运行目标里执行 Node 和包管理器命令。
- **软件包管理器**:检测出的脚本配置使用 `npm`、`pnpm`、`yarn` 还是 `bun`。**自动** 先读 `package.json` 的 `packageManager`,再看锁文件(`pnpm-lock.yaml`、`yarn.lock`、`bun.lock` / `bun.lockb`、`package-lock.json`),都没有时用 npm。
## 格式化
**重新格式化代码**(`⌥⌘L` / `Ctrl+Alt+L`)默认交给语言服务器,除非配置了优先使用外部格式化工具:
- **Prettier**(**设置 › 语言和框架 › Prettier**)默认处理 JavaScript、JSX、TypeScript、TSX、JSON、Vue、HTML、CSS、SCSS、Less、GraphQL、YAML 和 Markdown。未设置 **Prettier 路径** 时,依次查找项目 `node_modules`、IDE 管理的安装、`PATH`。
- **oxfmt**(**设置 › 语言和框架 › oxfmt**)在 oxfmt 配置文件(`.oxfmtrc.json`、`oxfmt.config.ts`)所在目录之下,或被选为格式化工具时,替代 Prettier 处理已启用的语言。
两个页面上,**格式化工具** 可选 **自动(优先语言服务器)** 或工具本身,**保存时运行** 控制保存时格式化,**启用的语言** 限定工具处理哪些语言。
## 运行与测试
打开项目时,OpenIDEA 会自动检测运行配置:
- 每个 `package.json` 脚本一个配置(`npm run build`、`pnpm run dev` 等),覆盖根包、嵌套包和 workspace 成员。名为 `test`、`jest`、`vitest` 或 `test:*` 的脚本,以及运行 `playwright test` 的脚本,是测试配置。
- 有 `playwright.config.*` 但没有脚本运行它的包:通过 `npx` 运行的 `playwright test`。
- `deno.json` / `deno.jsonc` 中每个 task 一个配置(`deno task <name>`)。
在 `package.json` 中,每个 `scripts` 条目旁的边栏图标可以直接运行或调试该脚本。
在源文件中使用 **运行上下文配置**(`⌃⇧R` / `Ctrl+Shift+F10`):
- 测试文件(`*.test.*`、`*.spec.*`,或 `__tests__/` 下的文件)通过最近的测试脚本运行,并把该文件作为参数传入。`test`、`it`、`describe` 块旁的边栏图标通过追加 `-t <name>` 只运行单个测试或测试组。
- 其他 `.js` 文件用 `node` 运行(项目含 `deno.json` 或 `bun.lock` 时改用 `deno run` 或 `bun run`)。
- `.ts` 文件在 Node.js 22.18+ / 23.6+ 上直接运行,在 22.6–22.17 和 23.0–23.5 上加 `--experimental-strip-types`,更早的版本则通过 `tsx` 或 `ts-node` 运行。
Jest、Vitest 和 Playwright Test 的输出会解析成测试树,支持 **重新运行失败的测试**。Playwright 运行时使用它的 `list` 报告器(配置里指定了其他报告器时除外);从行号栏运行单个测试时按行号选择(`file:line`),有多个 project 的运行会在文件下按 project 分组。**使用覆盖率运行** 支持调用 `vitest` 或 `jest` 的脚本(Vitest 需要 `@vitest/coverage-v8` 之类的覆盖率 provider,缺失时 IDE 会提示添加),以及通过 `c8` 运行的 `node` / `tsx` / `ts-node`。详见[测试](/zh/guide/testing)和[覆盖率](/zh/guide/coverage)。
## 调试
调试使用 JavaScript 调试器(vscode-js-debug);未安装时,第一次调试会提示安装。
- **调试** 适用于 npm 脚本配置、Node.js 文件以及 `deno run` / `deno test`,子进程会自动附加。
- **附加到 Node.js** 是一种运行配置类型,用于附加到本机或其他机器上以 `--inspect` 启动的进程(默认端口 9229),可设置本地与远程根目录映射。
- 运行菜单中的 **附加到进程…** 会列出正在运行的 Node.js 进程。
- **JavaScript 调试** 是一种运行配置类型:在 Chrome 或 Edge 中打开一个 URL,通过 source map 调试页面中的 JavaScript 和 TypeScript。先启动页面的服务(例如 `dev` npm 脚本);**Web 根目录** 是该 URL 对应的文件夹,默认为项目根目录。
开启 **不要步入库脚本**(**设置 › 构建、执行、部署 › 调试器 › 单步执行**,默认开启)后,单步调试会跳过 Node.js 内部代码和 `node_modules`。更多内容见[调试](/zh/guide/debugging)。
## 框架支持
- **React**:JSX 和 TSX 有各自的高亮模式,并获得完整的 TypeScript 服务器支持。输入 `>` 会自动闭合标签,修改开始或结束标签名会同步修改另一端,补全属性名或在属性名后输入 `=` 会按属性类型插入 `""`(字符串)或 `{}`(其他类型)。在元素的子内容中或 `return` / `=>` 之后,`⇥` / `Tab` 会展开 Emmet 缩写:`.card` 会变成 `<div className="card"></div>`。
- **Vue**:`.vue` 文件按块高亮,由 Vue Language Server 以 hybrid 模式提供支持。没有 `tsconfig.json` 的 Vue CLI 项目会读取 webpack 的 `resolve.alias`(如 `@/`),让 import 能正确解析。Prettier 可格式化 `.vue` 文件,模板中可用 Emmet。
- **Svelte**:使用 Svelte Language Server;高亮沿用 HTML 模式(script 按 TypeScript、style 按 CSS),标记部分可用 Emmet。
- **Astro**:使用 Astro Language Server,有专门的高亮模式。它需要 Node.js,找不到时请在 **设置 › 语言和框架 › JavaScript 运行时** 中选择。
- **Angular**:在含 `angular.json` 或已安装 `@angular/core` 的项目中,HTML 模板和组件里内联的 `template:` 字符串使用 Angular 高亮,Angular Language Server 提供模板补全、跳转和重命名,并按项目的 Angular 版本配置。
## 已知限制
- 调试 Bun 需要 `bun-dap`,它还没有发布版本:需从 `morf-bls/bun-dap` 自行构建并放到 `PATH` 中(缺少时 IDE 会给出步骤)。vscode-js-debug 无法附加到 Bun 使用 WebKit 协议的 inspector。
- Deno 支持运行和调试,但没有 Deno 语言服务器,Deno 文件使用 TypeScript 服务器。
- 不支持 Deno task 的覆盖率。