Usually, integration tests are written against DOM/CSS and executed from a separate process with frameworks like Selenium. After experimenting with such approach we learned that they are slow, unstable, hard to develop, debug, maintain and miss actual issues.
Theia comes with own integration testing framework which is designed to overcome the shortcomings of the conventional approach:
New tests should be added in examples/api-tests
package.
This package is published to allow adopters to run tests against end products.
Tests should be decomposed to different test suite files that adopters could include only some.
All test files are loaded one by own in the application process in the global scope. It means that they are sharing variables declared in the global scope. To avoid name conflicts each test file should start with a declaration of the test suite with the function scope. New variables have to be declared only within this function scope.
describe('Editors', function () {
const { assert } = chai;
});
The application is always bundled. Bundles exposing application modules via theia
namespace.
One can access a module with window.theia.moduleName
, where moduleName
is the absolute path to a module file relative to a containing package.
For instance editor-manager.js
can be accessed with window.theia['@theia/editor/lib/browser/editor-manager']
.
Testing framework as well injects require
function to lookup modules.
It can be useful with enabled typescript checks for js files to write statically checked code.
Importing symbols from an exposed module is not enough,
one has to access their implementations from the application container.
The application container is exposed via theia
namespace as well
and can be accessed with window.theia.container
.
// @ts-check
describe('Editors', function () {
const { assert } = chai;
const { EditorManager } = require('@theia/editor/lib/browser/editor-manager');
const Uri = require('@theia/core/lib/common/uri');
const { WorkspaceService } = require('@theia/workspace/lib/browser/workspace-service');
/** @type {import('inversify').Container} */
const container = window['theia'].container;
const editorManager = container.get(EditorManager);
const workspaceService = container.get(WorkspaceService);
});
An example of the complete test suite can be found below. You can see how it follows design principles:
WidgetManager.open
API,
but such logic is already encapsulated in EditorManager.open
which allows keeping a test simple.EditorManager.open
to resolve when a widget is revealed.// @ts-check
describe('Editors', function () {
const { assert } = chai;
const { EditorManager } = require('@theia/editor/lib/browser/editor-manager');
const Uri = require('@theia/core/lib/common/uri');
const { WorkspaceService } = require('@theia/workspace/lib/browser/workspace-service');
/** @type {import('inversify').Container} */
const container = window['theia'].container;
const editorManager = container.get(EditorManager);
const workspaceService = container.get(WorkspaceService);
before(() => editorManager.closeAll({ save: false });
it('open', async () => {
const root = (await workspaceService.roots)[0];
assert.equal(editorManager.all.length, 0);
await editorManager.open(new Uri.default(root.uri).resolve('package.json'), {
mode: 'reveal'
});
assert.equal(editorManager.all.length, 1);
});
});
The framework ensures that tests are executed only when the application is ready, workspace is initialized and all preferences are loaded.
Since tests are executed within the same process, each test suite should take care to bring the application in the proper state. For instance, an example test awaits when all editors are closed before testing the open function.
See theia CLI docs to learn more about how to use
test
command.
Commands below should be executed from examples/browser
.
To run tests once:
npm run test
This command starts the browser example application and runs tests from examples/api-tests
against it.
To inspect tests:
npm run test:debug
This command runs tests but as well opens the Chrome devtools that you can debug the frontend code and test files. After doing changes to source code or tests, reload the page to run new code and tests.
Important! Since tests are relying on focus while running tests keep the page focused.
To inspect tests and backend code:
npm run test:debug --inspect
Use the debug view to attach to the backend server for debugging as usual.
Modify a test case to use it.only
instead of it
and reload the page.
One can also add ?grep=foo
query to the page URL to run only matching tests.
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )