
Nx 插件 E2E 測試工程生成指南使用 nx/plugin:e2e-project 生成器搭建插件端到端測試【免費下載鏈接】nxThe Monorepo Platform that amplifies both developers and AI agents. Nx optimizes your builds, scales your CI, and fixes failed PRs automatically. Ship in half the time.項目地址: https://gitcode.com/GitHub_Trending/nx/nxnx/plugin包提供的e2e-project生成器用于為已存在的 Nx 插件自動搭建端到端E2E測試工程它默認生成基于 Jest 的測試項目也支持切換到 Vitest。本文以倉庫中的官方文檔 e2e-project-examples.md 為主線結合生成器的參數 Schema、核心實現與單元測試完整講解命令用法、全部參數、生成產物與底層工作原理幫助你為插件寫出真正可運行的端到端測試。e2e-project 生成器能做什么在 Nx 中插件Nx Plugin的開發流程通常包括編寫生成器generator與執行器executor而驗證它們在真實工作區中是否按預期工作需要一套 E2E 測試工程它會在測試環境中臨時創建一個全新的 Nx 工作區把剛構建的插件發布到本地 npm registry再在臨時工作區中安裝該插件并執行相關命令來驗證行為。e2e-project生成器generators.json 中注冊為e2e-project正是為此服務的腳手架給定一個已存在的插件項目它會生成一個名為pluginName-e2e的測試工程并配置好基于 Jest 或 Vitest 的e2etarget通過 Verdaccio 搭建本地 npm registry將插件發布后供測試工作區安裝一個包含默認冒煙測試的*.spec.ts文件驗證插件可以被安裝到新建的工作區與插件項目之間的隱式依賴關系。核心參數一覽生成器的全部參數定義在 schema.json 中其中pluginName與npmPackageName為必填項參數類型默認值必填說明pluginNamestring-是待測試插件的項目名x-priority: importantnpmPackageNamestring-是插件發布到 npm 時的包名x-priority: importantpluginOutputPathstring-重要插件構建后的輸出路徑x-priority: importanttestRunnerjest|vitestjest否E2E 測試使用的測試運行器projectDirectorystring項目名否插件所在目錄決定 E2E 工程放置位置linternone|eslint|oxlint工作區當前使用的 linter否用于運行 lint 檢查的工具jestConfigstring-否Jest 配置文件路徑minimalbooleanfalse否生成最小化 E2E 工程不生成默認 executor/generator 的測試useProjectJsonboolean-否使用project.json承載 Nx 配置而不是內聯到package.jsonskipFormatbooleanfalse否跳過文件格式化內部選項其中testRunner由枚舉約束只能取jest或vitest默認jest。如果省略testRunner生成器內部會執行options.testRunner options.testRunner ?? jest見 e2e.ts因此默認即 Jest 方案。使用 Jest 的 E2E 工程默認方案官方文檔給出的第一個示例是基于 Jest 的默認用法。假設插件項目名為my-plugin構建輸出到dist/my-pluginnpm 包名為my-plugin執行nx g nx/plugin:e2e-project --pluginName my-plugin --npmPackageName my-plugin --pluginOutputPath dist/my-plugin該命令會在工作區生成my-plugin-e2e項目。從 e2e.spec.ts 的斷言可以還原出生成的工程結構與關鍵配置my-plugin-e2e/tsconfig.json繼承根目錄tsconfig.base.json若不存在tsconfig.base.json則回退為繼承根tsconfig.json見 e2e.spec.tsmy-plugin-e2e/src/my-plugin.spec.ts默認冒煙測試文件my-plugin-e2e/jest.config.ctsJest 配置使用ts-jest做 TypeScript 轉換并配置globalSetup/globalTeardown指向本地 registry 腳本my-plugin-e2e/tsconfig.spec.json測試專用的 TypeScript 配置項目targets.e2eexecutor 為nx/jest:jestdependsOn: [^build]options.jestConfig指向my-plugin-e2e/jest.config.cts并開啟runInBand: true見 e2e.spec.ts項目與插件之間建立了隱式依賴implicitDependencies: [my-plugin]見 e2e.spec.ts。Jest 配置的生成邏輯Jest 方案的配置由addJest函數完成e2e.ts其核心步驟為注冊my-plugin-e2e項目配置projectType: applicationsourceRoot: root/src調用nx/jest的configurationGenerator以targetName: e2e生成 Jest 配置通過addLocalRegistryScripts生成tools/scripts/start-local-registry.ts與tools/scripts/stop-local-registry.ts并寫入 Jest 配置的globalSetup/globalTeardown為e2etarget 補上dependsOn: [^build]與runInBand: true。生成的jest.config.cts大致如下以非 TS solution 布局為例見 e2e.spec.tsmodule.exports { displayName: my-plugin-e2e, preset: ../jest.preset.js, transform: { ^.\\.[tj]s$: [ts-jest, { tsconfig: rootDir/tsconfig.spec.json }], }, moduleFileExtensions: [ts, js, html], coverageDirectory: ../coverage/my-plugin-e2e, globalSetup: ../tools/scripts/start-local-registry.ts, globalTeardown: ../tools/scripts/stop-local-registry.ts, };注意runInBand: true與dependsOn: [^build]的組合^build保證在測試啟動前先構建插件并將產物發布到本地 registryrunInBand則確保測試串行執行避免多個 E2E 套件并發搶占共享的臨時目錄。使用 Vitest 的 E2E 工程官方文檔的第二個示例是切換到 Vitest 的用法只需在命令中追加--testRunner vitestnx g nx/plugin:e2e-project --pluginName my-plugin --npmPackageName my-plugin --pluginOutputPath dist/my-plugin --testRunner vitestVitest 方案由addVitest函數實現e2e.ts與 Jest 方案有幾處關鍵差異調用nx/vitest的configurationGenerator以testTarget: e2e生成配置并指定testEnvironment: node、coverageProvider: none見 e2e.tse2etarget 的 executor 為nx/vitest:test同樣配置dependsOn: [^build]并額外設置maxWorkers: 1與isolate: false見 e2e.spec.ts——這是因為多個測試套件共享tmp/test-project目錄必須串行執行且 Vitest 4 移除了poolOptions嵌套選項也無法在 executor 的 argv 往返中存活因此使用標量選項Vitest 沒有globalTeardown選項其globalSetup文件必須同時導出setup與teardown。因此生成器會創建tools/scripts/vitest-global-setup.ts包裝器e2e.ts內容為export { default as setup } from ./start-local-registry; export { default as teardown } from ./stop-local-registry;若缺少teardownsetup 階段啟動的 Verdaccio 進程會在測試結束后殘留見 e2e.spec.ts 的對應斷言。生成器會在vitest.config.ts或vitest.config.mts的test配置塊中注入globalSetup路徑。一個值得注意的細節nx/vitest只會推斷testtarget如果沒有顯式的e2etargetnx e2e命令就不存在^build也就不會在本地 registry 發布插件前運行見 e2e.spec.ts 的測試說明。因此 Vitest 方案的e2etarget 是顯式注冊的。底層原理臨時工作區、本地 registry 與冒煙測試E2E 測試之所以能驗證插件的真實行為靠的是 e2e.ts 中編排的完整流程校驗插件存在validatePlugin會讀取插件的項目配置若項目不存在則拋出Project name pluginName doesnt not exist.e2e.ts并在 e2e.spec.ts 中通過正反兩個用例驗證生成工程文件基于模板files/目錄渲染my-plugin-e2e下的文件配置 Verdaccio 本地 registrysetupVerdaccio與addLocalRegistryScripts一起完成本地 npm registry 的搭建與啟停腳本生成按testRunner分支接入 Jest 或 Vitest解除插件的 private 標記updatePluginPackageJson會刪除插件package.json中的private字段因為發布到本地 registry 需要非私有包e2e.ts按需配置 lint 與 TS solution 工程。默認冒煙測試模板生成的測試文件來自模板simplePluginName.spec.ts__tmpl__其測試策略是beforeAll中通過createTestProject()使用create-nx-workspacelatest在tmp/test-project目錄創建一個全新的臨時工作區--preset apps --nxCloudskip --no-interactive隨后把構建好的插件以e2e版本標簽安裝進去安裝預算為 300 秒beforeAll(..., 300_000)因為該 hook 會同步執行兩次安裝必須覆蓋冷包管理器緩存場景見模板文件注釋以及 e2e.spec.ts 中預算不得低于 240_000 的回歸測試it(should be installed)通過執行包管理器的 list 命令驗證插件確實被安裝npm ls在未正確安裝時會失敗afterAll遞歸清理臨時工作區。運行 E2E 測試nx e2e my-plugin-e2e高級選項與邊界情況控制項目放置位置projectDirectory當插件位于嵌套目錄如namespace/my-plugin時可通過projectDirectory指定生成的 E2E 工程會落到directory-e2e下nx g nx/plugin:e2e-project --pluginName my-plugin --npmPackageName my-plugin --pluginOutputPath dist/namespace/my-plugin --projectDirectory namespace/my-plugin此時項目根目錄為namespace/my-plugin-e2e見 e2e.spec.ts。最小化工程minimal設置--minimal后生成器不會生成針對默認 executor 和 generator 的測試適合只想驗證插件可安裝、可加載的場景。Lint 配置linter--linter支持eslint、oxlint或none默認繼承工作區現有 lint 工具。指定--linter eslint時會生成my-plugin-e2e/eslint.config.mjs內容為導入根配置并展開import baseConfig from ../eslint.config.mjs; export default [...baseConfig];見 e2e.spec.ts 的快照斷言。TypeScript solution 布局當工作區采用 TS solution 布局package.json中配置workspaces、根tsconfig.json使用references時生成器會走isTsSolutionSetup分支為 E2E 工程生成獨立的package.json把新的 tsconfig 引用追加到根tsconfig.json并更新工作區文件Jest 轉換器也隨之切換為swc/jest并生成.spec.swcrc見 e2e.spec.ts??偨Ynx/plugin:e2e-project生成器把「為插件搭建端到端測試工程」這一重復性工作封裝為一條命令默認 Jest 方案nx g nx/plugin:e2e-project --pluginName my-plugin --npmPackageName my-plugin --pluginOutputPath dist/my-pluginVitest 方案追加--testRunner vitest核心機制構建插件 → 發布到 Verdaccio 本地 registry → 在create-nx-workspace創建的臨時工作區中安裝e2e版本插件 → 冒煙測試驗證安裝成功。其參數定義、生成邏輯與全部行為均可在倉庫中直接追溯schema.json、e2e.ts 與 e2e.spec.ts模板文件位于 files 目錄。閱讀這些文件可以進一步了解每個選項對生成產物的具體影響并按需裁剪你的插件測試策略?!久赓M下載鏈接】nxThe Monorepo Platform that amplifies both developers and AI agents. Nx optimizes your builds, scales your CI, and fixes failed PRs automatically. Ship in half the time.項目地址: https://gitcode.com/GitHub_Trending/nx/nx創作聲明:本文部分內容由AI輔助生成(AIGC),僅供參考