跳转到主内容

测试

我们的目的是保持 Electron 代码的高覆盖率。我们要求所有的pull request不仅要通过所有现有的测试,还要增加新的测试来覆盖变更的代码和新的场景。我们要尽可能多地覆盖Electron应用中的代码路径和用例,以确保我们发布的应用程序都能拥有更少的bug。

这个仓库为 JavaScript 和 C ++ 提供了linting 规则 - 以及单元和集成测试。通过 coding-style文档来学习更多的Electron's编码风格

提示​

To ensure that your changes are in compliance with the Electron coding style, run npm run lint, which will run a variety of linting checks against your changes depending on which areas of the code they touch.

Many of these checks are included as precommit hooks, so it's likely your error would be caught at commit time.

单元测试​

If you are not using build-tools, ensure that the name you have configured for your local build of Electron is one of Testing, Release, Default, or you have set process.env.ELECTRON_OUT_DIR. Without these set, Electron will fail to perform some pre-testing steps.

要运行所有单元测试,请执行 npm run test。单元测试其实也是一个Electron应用!你可以在spec 文件夹中找到它。请注意,单元测试也有自己的 package.json,因此它的依赖没有在最顶层的package.json中定义。

The suite is driven by vitest: the vitest CLI runs under Node.js and starts several instances of the spec app, each of which runs one spec file at a time in Electron's main process. Suites that need the machine to themselves (window focus, the clipboard, the screen, global shortcuts) are declared with describe(title, { tags: ['serial'] }, fn) and run one at a time after everything else has finished. Set ELECTRON_SPEC_WORKERS to change how many Electron processes run at once.

To run only some spec files, pass them with --files: npm run test -- --files spec/api-app.spec.ts. To run only tests whose name matches a pattern, run npm run test -- -g=PATTERN, replacing PATTERN with a regex. 例:如果想运行IPC测试,你可以执行npm run test -- -g ipc。

Specs import describe, it, expect and the hooks from vitest and assert with its matchers (expect(x).toBe(y), toEqual, toHaveProperty, ...); chai-style chains (expect(x).to.equal(y)) are rejected by the linter because their property form can silently assert nothing. A few Electron specific behaviours (a macrotask between tests, defer() cleanup, spec/disabled-tests.json) live in spec/vitest/runner.ts.

Node.js Smoke Tests​

If you've made changes that might affect the way Node.js is embedded into Electron, we have a test runner that runs all of the tests from Node.js, using Electron's custom fork of Node.js.

To run all of the Node.js tests:

$ node script/node-spec-runner.js

To run a single Node.js test:

$ node script/node-spec-runner.js parallel/test-crypto-keygen

where the argument passed to the runner is the path to the test in the Node.js source tree.

在Windows10上测试​

Extra steps to run the unit test:​

  1. Visual Studio 2019 must be installed.

  2. Node headers have to be compiled for your configuration.

    ninja -C out\Testing electron:node_headers
  3. The electron.lib has to be copied as node.lib.

    cd out\Testing
    mkdir gen\node_headers\Release
    copy electron.lib gen\node_headers\Release\node.lib

Missing fonts​

一些 Windows 10 设备上没有默认安装Meriryo字体,这会导致字体回退测试的失败。要安装Meiryo字体:

  1. 按下Windows key并搜索 管理可选功能。
  2. 单击_添加功能_
  3. 选择_Japanese Supplemental Fonts_并单击 安装

Pixel measurements​

由于浮点数精度误差,某些依赖于精确像素测量的测试可能无法正常在Hi-DPI屏幕的设备上工作。为了使这些测试能正常运行,请确保设备的缩放比为100%。

要配置缩放比:

  1. 按下Windows key并搜索_显示设置_.
  2. 在_Scale and layout_下,确保缩放比为100%。

Multi-Monitor Tests​

Some Electron APIs require testing across multiple displays, such as screen detection, window positioning, and display-related events. For contributors working on these features, the virtualDisplay native addon enables you to create and position virtual displays programmatically, making it possible to test multi-monitor scenarios without any physical hardware.

For detailed information on using virtual displays in your tests, see Multi-Monitor Testing.

Platform support: macOS only