メインコンテンツまでスキップ
バージョン: v4.34

エンドツーエンドのテスト

E2Eテストは、実際のブラウザーでコンポーネントを検証します。たとえば、 my-componentにX属性がある場合、子コンポーネントはテキストYをレンダリングし、イベントZを受信することを期待します。レンダリングテストにPuppeteerを使用することにより(ブラウザの動作をシミュレートするノード環境ではなく)、より良い結果を得るために、エンドツーエンドのテストを実際のブラウザ内で実行できます。

ステンシルは、JestおよびPuppeteerのテストに役立つ多くのユーティリティ関数を提供します。たとえば、コンポーネントのシャドウdomは、Puppeteer上に構築されたStencilユーティリティ関数を使用してクエリおよびテストできます。テストは、模擬HTMLコンテンツを提供できるだけでなく、Puppeteerが開いてStencilの開発サーバーでテストできるアプリのURLにアクセスすることもできます。

エンドツーエンドのテストでは、テストを実際に実行する前に、新しいビルド、dev-server、およびpuppeteerブラウザーインスタンスを作成する必要があります。追加されたビルドの複雑さにより、 stenciltestコマンドはビルド要件を事前に整理することができます。

E2Eテストを実行するには、 stencil test--e2eを実行します。デフォルトでは、 .e2e.tsで終わるファイルが実行されます。

ステンシルのE2Eテストは、次のAPIで提供され、 @stencil/core/testingから入手できます。 ほとんどのメソッドは非同期であり、Promisesを返します。 asyncとawaitを使用して、テストを整理します。

  • newE2EPage:新しい E2EPageオブジェクトをインスタンス化するには、各テストの開始時に呼び出す必要があります

E2EPage is a wrapper utility to Puppeteer to simplify writing tests. Some helpful methods on E2EPage include:

  • find(selector: string):セレクターに一致する要素を検索します。 document.querySelectorに似ています。
  • setContent(html: string):ページのコンテンツを設定します。 これは、テスト対象のコンポーネントのマークアップを含める場所です。
  • goto(url: string): open a page served by the dev server. URL has to start with /.
  • setViewport(viewport: Viewport): Updates the page to emulate a device display. This is helpful for testing a component's behavior in different orientations and viewport sizes.
  • waitForChanges():StencilとPuppeteerの両方に非同期アーキテクチャがあり、これはパフォーマンスにとって良いことです。 すべての呼び出しは非同期であるため、コンポーネントに変更が加えられたときに await page.waitForChanges()が呼び出される必要があります。

E2Eテストの例には、次の定型文があります。

import { newE2EPage } from '@stencil/core/testing';

describe('example', () => {
it('should render a foo-component', async () => {
const page = await newE2EPage();
await page.setContent(`<foo-component></foo-component>`);
const el = await page.find('foo-component');
expect(el).not.toBeNull();
});
});

Options​

The newE2EPage accepts the following options:

  • html: a HTML template, e.g. containing your Stencil component to be rendered in the browser (same as calling page.setContent('...'))
  • url: opens a url to open a page served by the dev server (option will be ignored if html is set, same as calling page.goto('...'))
  • failOnConsoleError: If set to true, Stencil will throw an error if a console error occurs (default: false)
  • failOnNetworkError: If set to true, Stencil will throw an error if a network request fails (default: false)
  • logFailingNetworkRequests: If set to true, Stencil will log failing network requests (default: false)

E2Eテストの例​

import { newE2EPage } from '@stencil/core/testing';

it('should create toggle, unchecked by default', async () => {
const page = await newE2EPage();

await page.setContent(`
<ion-toggle class="pretty-toggle"></ion-toggle>
`);

const ionChange = await page.spyOnEvent('ionChange');

const toggle = await page.find('ion-toggle');

expect(toggle).toHaveClasses(['pretty-toggle', 'hydrated']);

expect(toggle).not.toHaveClass('toggle-checked');

toggle.setProperty('checked', true);

await page.waitForChanges();

expect(toggle).toHaveClass('toggle-checked');

expect(ionChange).toHaveReceivedEventDetail({
checked: true,
value: 'on'
});
});

E2Eテストレシピ​

Find an element in the Shadow DOM​

「ピアス」セレクター >>>を使用して、コンポーネントのシャドウルート内のオブジェクトをクエリします。

const el = await page.find('foo-component >>> .close-button');

By default Stencil will look into all shadow roots to find the element. However if you want to restrict your query by specifying the shadow root of a particular component, you can chain multiple deep selectors within the same query, e.g.:

const el = await page.find('foo-component >>> div bar-component >>> div');

Set a @Prop() on a component​

page。$ eval(Puppeteer APIの一部)を使用して、Propを設定するか、コンポーネントを操作します。

// create a new puppeteer page
// load the page with html content
await page.setContent(`
<prop-cmp></prop-cmp>
`);

// select the "prop-cmp" element
// and run the callback in the browser's context
await page.$eval('prop-cmp', (elm: any) => {
// within the browser's context
// let's set new property values on the component
elm.first = 'Marty';
elm.lastName = 'McFly';
});

// we just made a change and now the async queue need to process it
// make sure the queue does its work before we continue
await page.waitForChanges();

Set a @Prop() on a component using an external reference​

Because page.$eval has an isolated scope, you’ll have to explicitly pass in outside references otherwise you’ll an encounter an undefined error. This is useful in case you’d like to import data from another file, or re-use mock data across multiple tests in the same file.

const props = {
first: 'Marty',
lastName: 'McFly',
};

await page.setContent(`<prop-cmp></prop-cmp>`);

await page.$eval('prop-cmp',
(elm: any, { first, lastName }) => {
elm.first = first;
elm.lastName = lastName;
},
props
);

await page.waitForChanges();

Call a @Method() on a component​

const elm = await page.find('method-cmp');
elm.setProperty('someProp', 88);
const methodRtnValue = await elm.callMethod('someMethod');

Type inside an input field​

const page = await newE2EPage({
html: `
<dom-interaction></dom-interaction>
`
});

const input = await page.find('dom-interaction >>> .input');

let value = await input.getProperty('value');
expect(value).toBe('');

await input.press('8');
await input.press('8');
await input.press(' ');

await page.keyboard.down('Shift');
await input.press('KeyM');
await input.press('KeyP');
await input.press('KeyH');
await page.keyboard.up('Shift');

Checking the text of a rendered component​

await page.setContent(`
<prop-cmp first="Marty" last-name="McFly"></prop-cmp>
`);

const elm = await page.find('prop-cmp >>> div');
expect(elm).toEqualText('Hello, my name is Marty McFly');

Checking a component's HTML​

shadowRootコンテンツの場合:

        expect(el.shadowRoot).toEqualHtml(`<div>
<div class=\"nav-desktop\">
<slot></slot>
</div>
</div>`);
});

For non-shadow content:

        expect(el).toEqualHtml(`<div>
<div class=\"nav-desktop\">
<slot></slot>
</div>
</div>`);
});

Emulate a display​

If you need to test how a component behaves with a particular viewport you can set the viewport width and height like so:

const page = await new E2EPage();

await page.setViewport({
width: 900,
height: 600
});

// Query an element that is hidden by a media query when width < 901px
const el = await page.find('.desktop');
expect(el).not.toBeDefined();

Asynchronous Global Script​

If you are using an asynchronous global script, it may happen that your Stencil components haven't been hydrated at the time you are trying to interact with them. In this case it is recommend to wait for the hydration flag to be set on the component, e.g.:

const page = await newE2EPage();
await page.setContent(`<my-cmp></my-cmp>`);
await page.waitForSelector('.hydrated')

Note: the hydrate selector may be different depending on whether you have a custom hydratedFlag options set.

CD/CIでのe2eテストの自動化に関する警告​

As it is a fairly common practice, you might want to automatically run your end-to-end tests on your Continuous Deployment/Integration (CD/CI) system. However, some environments might need you to tweak your configuration at times. If so, the config object in your stencil.config.ts file has a testing attribute that accepts parameters to modify how Headless Chrome is actually used in your pipeline.

Gitlab CI環境で必要になる可能性のある構成の例:

export const config: Config = {
namespace: 'Foo',
testing: {
/**
* Gitlab CI doesn't allow sandbox, therefor this parameters must be passed to your Headless Chrome
* before it can run your tests
*/
browserArgs: ['--no-sandbox', '--disable-setuid-sandbox'],
},
outputTargets: [
{ type: 'dist' },
{
type: 'www',
},
],
};

Check the testing config docs to learn more about the possibilities on this matter.