Testando
We aim to keep the code coverage of Electron high. Nós pedidos que todos os pull requests não sejam apenas aprovados em todos os testes já existentes, mas de preferência adicione novos senários de testes para cobrir o código alterado. Ensuring that we capture as many code paths and use cases of Electron as possible ensures that we all ship apps with fewer bugs.
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
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.
Testes Unitários
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.
Para executar todos os testes unitários, execute: npm run test. Em uma aplicação Electron os testes podem ser encontrados na pasta spec. Note that it has its own package.json and that its dependencies are therefore not defined in the top-level 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. As an example: If you want to run only IPC tests, you would run 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.
Testing on Windows 10 devices
Extra steps to run the unit test:
-
Visual Studio 2019 must be installed.
-
Node headers have to be compiled for your configuration.
ninja -C out\Testing electron:node_headers -
The electron.lib has to be copied as node.lib.
cd out\Testingmkdir gen\node_headers\Releasecopy electron.lib gen\node_headers\Release\node.lib
Missing fonts
Some Windows 10 devices do not ship with the Meiryo font installed, which may cause a font fallback test to fail. To install Meiryo:
- Push the Windows key and search for Manage optional features.
- Click Add a feature.
- Select Japanese Supplemental Fonts and click Install.
Pixel measurements
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:
- Push the Windows key and search for Display settings.
- Under Scale and layout, make sure that the device is set to 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