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

Overview

Stencil has a built-in test runner that uses Jest and Puppeteer as its testing libraries, and allows developers to install both libraries using their preferred package manager.

If you created a project using npm init stencil, these libraries were installed for you. Depending on when your project was created, you may or may not have the latest supported version installed.

To view current version support for both Jest and Puppeteer, please see the Stencil support policy for testing libraries.

注記

For users wanting to use Jest v30+, please refer to the Jest v30+ documentation for setup and configuration instructions.

Testing Commands​

Stencil tests are run using the command stencil test, followed by one or more optional flags:

  • --spec to run unit tests
  • --e2e to run end-to-end tests
  • --watchAll to watch the file system for changes, and rerun tests when changes are detected

When the --spec and/or --e2e flags are provided, Stencil will automatically run the tests associated with each flag.

Below a series of example npm scripts which can be added to the project's package.json file to run Stencil tests:

{
"scripts": {
"test": "stencil test --spec",
"test.watch": "stencil test --spec --watchAll",
"test.end-to-end": "stencil test --e2e"
}
}

Each command above begins with stencil test, which tells Stencil to run tests. Note that each stencil test command in the example above is followed by one or more of the optional flags. Looking at each script, one at a time:

  • the test script runs unit tests for our Stencil project.
  • the test.watch script runs unit tests for our Stencil project. It watches the filesystem for changes, and reruns tests when changes are detected.
  • the test.end-to-end script runs the end-to-end tests for our Stencil project.

If you created a project using npm init stencil, these scripts are provided to you automatically.

Stencil does not prescribe any specific naming convention for the names of your scripts. The test.watch script could as easily be named test-watch, test.incremental, etc. As long as the script itself uses the stencil test command, your tests should be run.

構成のテスト​

ステンシルは、すでに収集したデータからデフォルトを適用します。 たとえば、ステンシルは、どのディレクトリを調べるか、どのファイルがspecファイルおよびe2eファイルであるかをすでに認識しています。 Jestは、同じ構成名を使用して構成できますが、ステンシル構成の testingプロパティを使用するようになりました。 詳細については、テスト構成ドキュメントを参照してください。

import { Config } from '@stencil/core';

export const config: Config = {
testing: {
testPathIgnorePatterns: [...]
}
};

Command Line Arguments​

While the Stencil CLI offers a certain set of command line flags to specify e.g. which types of tests to run, you also have access to all Jest options through the CLI. For example to specify a single test, you can pass in a positional argument to Jest by adding a --, e.g.:

# run a single unit test
npx stencil test --spec -- src/components/my-component/my-component.spec.ts
# run a single e2e test
npx stencil test --e2e -- src/components/my-component/my-component.e2e.ts

Next to positional arguments, Stencil also passes along certain Jest specific flags, e.g.:

# enable code coverage
npx stencil test --spec --coverage

You can find more information about Jest CLI options in the project documentation.

VSCodeでのテストの実行とデバッグ​

Adding the following configurations to .vscode/launch.json will allow you to use the VS Code Debugger to run the Stencil test runner for the currently active file in your editor.

To use the below configuration:

  1. Ensure the test file you want to run is open and in the current active window in VS Code.
  2. Select the debug configuration to run:
    1. 'E2E Test Current File' will run the end-to-end tests in the active test file
    2. 'Spec Test Current File' will run the spec tests in the active test file
  3. Hit the play button to start the test.
.vscode/launch.json
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "E2E Test Current File",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/node_modules/.bin/stencil",
"args": ["test", "--e2e", "--", "--maxWorkers=0", "${fileBasename}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Spec Test Current File",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/node_modules/.bin/stencil",
"args": ["test", "--spec", "--", "${fileBasename}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
ヒント

Windows users: The program value should be set to "${workspaceFolder}/node_modules/bin/stencil". If that value does not work, please try"${workspaceFolder}/node_modules/@stencil/core/bin/stencil".

The configuration above makes use of special VS Code variables, such as ${workspaceFolder}. These variables are substituted with actual values upon starting the tests. For more information regarding the values these variables resolve to, please see VS Code's Variables Reference documentation.

Other Resources​