测试
🌐 Testing
我们旨在保持 Electron 的代码覆盖率较高。我们要求所有的拉取请求不仅要通过所有现有的测试,而且最好还添加新的测试来覆盖更改的代码和新场景。确保我们尽可能覆盖 Electron 的所有代码路径和使用场景,可以保证我们发布的应用出现更少的错误。
🌐 We aim to keep the code coverage of Electron high. We ask that all pull request not only pass all existing tests, but ideally also add new tests to cover changed code and new scenarios. Ensuring that we capture as many code paths and use cases of Electron as possible ensures that we all ship apps with fewer bugs.
该代码库附带了针对 JavaScript 和 C++ 的代码检查规则,以及单元测试和集成测试。要了解更多关于 Electron 的编码风格,请参阅 coding-style 文档。
🌐 This repository comes with linting rules for both JavaScript and C++ – as well as unit and integration tests. To learn more about Electron's coding style, please see the coding-style document.
代码检查
🌐 Linting
为了确保你的更改符合 Electron 的编码风格,请运行 npm run lint,它会根据你的更改涉及的代码区域,对你的更改进行各种 lint 检查。
🌐 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.
单元测试
🌐 Unit Tests
如果你没有使用 build-tools,请确保你为本地构建的 Electron 配置的名称是 Testing、Release、Default 之一,或者你已经设置了 process.env.ELECTRON_OUT_DIR。如果没有设置这些,Electron 将无法执行某些预测试步骤。
🌐 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 中定义。
🌐 To run all unit tests, run npm run test. The unit tests are an Electron
app (surprise!) that can be found in the spec folder. Note that it has
its own package.json and that its dependencies are therefore not defined
in the top-level package.json.
要仅运行与某个模式匹配的特定测试,请运行 npm run test -- -g=PATTERN,将 PATTERN 替换为匹配你想运行的测试的正则表达式。例如:如果你只想运行 IPC 测试,则运行 npm run test -- -g ipc。
🌐 To run only specific tests matching a pattern, run npm run test -- -g=PATTERN, replacing the PATTERN with a regex that matches the tests
you would like to run. As an example: If you want to run only IPC tests, you
would run npm run test -- -g ipc.
Node.js 冒烟测试
🌐 Node.js Smoke Tests
如果你做了一些可能影响 Node.js 在 Electron 中嵌入方式的更改,我们有一个测试运行器,可以使用 Electron 定制的 Node.js 分支运行 Node.js 的所有测试。
🌐 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.
要运行所有 Node.js 测试:
🌐 To run all of the Node.js tests:
$ node script/node-spec-runner.js
要运行单个 Node.js 测试:
🌐 To run a single Node.js test:
$ node script/node-spec-runner.js parallel/test-crypto-keygen
传递给运行器的参数是 Node.js 源代码树中测试的路径。
🌐 where the argument passed to the runner is the path to the test in the Node.js source tree.
在 Windows 10 设备上测试
🌐 Testing on Windows 10 devices
运行单元测试的额外步骤:
🌐 Extra steps to run the unit test:
-
必须安装 Visual Studio 2019。
-
必须根据你的配置编译节点标头。
ninja -C out\Testing electron:node_headers -
Electron.lib 必须复制为 node.lib。
cd out\Testing
mkdir gen\node_headers\Release
copy electron.lib gen\node_headers\Release\node.lib
缺少字体
🌐 Missing fonts
某些 Windows 10 设备 未预装 Meiryo 字体,这可能导致字体回退测试失败。要安装 Meiryo 字体:
- 按下 Windows 键并搜索“管理可选功能”。
- 点击_添加功能_。
- 选择“日语补充字体”,然后点击“安装”。
像素测量
🌐 Pixel measurements
某些依赖精确像素测量的测试在使用高分辨率屏幕设置的设备上可能无法正确运行,这是由于浮点精度错误引起的。要正确运行这些测试,请确保设备设置为100%缩放。
🌐 Some tests which rely on precise pixel measurements may not work correctly on devices with Hi-DPI screen settings due to floating point precision errors. To run these tests correctly, make sure the device is set to 100% scaling.
要配置显示缩放:
🌐 To configure display scaling:
- 按下 Windows 键,然后搜索“显示设置”。
- 在“缩放与布局”下,确保设备设置为100%。