diff --git a/README.md b/README.md index 82f61645..418bc83f 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,8 @@ yarn storybook yarn test-storybook ``` -> **NOTE:** The runner assumes that your Storybook is running on port `6006`. If you're running Storybook in another port, either use --url or set the TARGET_URL before running your command like: +> **Note** +> The runner assumes that your Storybook is running on port `6006`. If you're running Storybook in another port, either use --url or set the TARGET_URL before running your command like: > > ```jsx > yarn test-storybook --url http://127.0.0.1:9009 @@ -141,6 +142,9 @@ Usage: test-storybook [options] The test runner is based on [Jest](https://jestjs.io/) and will accept most of the [CLI options](https://jestjs.io/docs/cli) that Jest does, like `--watch`, `--watchAll`, `--maxWorkers`, etc. It works out of the box, but if you want better control over its configuration, you can eject its configuration by running `test-storybook --eject` to create a local `test-runner-jest.config.js` file in the root folder of your project. This file will be used by the test runner. +> **Note** +> The `test-runner-jest.config.js` file can be placed inside of your Storybook config dir as well. If you pass the `--config-dir` option, the test-runner will look for the config file there as well. + The configuration file will accept options for two runners: #### Jest-playwright options @@ -235,7 +239,8 @@ If you are running tests against a local Storybook but for some reason want to r yarn test-storybook --index-json ``` -> **NOTE:** index.json mode is not compatible with watch mode. +> **Note** +> index.json mode is not compatible with watch mode. ## Running in CI @@ -268,7 +273,8 @@ jobs: TARGET_URL: '${{ github.event.deployment_status.target_url }}' ``` -> **_NOTE:_** If you're running the test-runner against a `TARGET_URL` of a remotely deployed Storybook (e.g. Chromatic), make sure that the URL loads a publicly available Storybook. Does it load correctly when opened in incognito mode on your browser? If your deployed Storybook is private and has authentication layers, the test-runner will hit them and thus not be able to access your stories. If that is the case, use the next option instead. +> **Note** +> If you're running the test-runner against a `TARGET_URL` of a remotely deployed Storybook (e.g. Chromatic), make sure that the URL loads a publicly available Storybook. Does it load correctly when opened in incognito mode on your browser? If your deployed Storybook is private and has authentication layers, the test-runner will hit them and thus not be able to access your stories. If that is the case, use the next option instead. ### 2. Running against locally built Storybooks in CI @@ -300,7 +306,8 @@ jobs: run: yarn test-storybook:ci ``` -> **_NOTE:_** Building Storybook locally makes it simple to test Storybooks that could be available remotely, but are under authentication layers. If you also deploy your Storybooks somewhere (e.g. Chromatic, Vercel, etc.), the Storybook URL can still be useful with the test-runner. You can pass it to the `REFERENCE_URL` environment variable when running the test-storybook command, and if a story fails, the test-runner will provide a helpful message with the link to the story in your published Storybook instead. +> **Note** +> Building Storybook locally makes it simple to test Storybooks that could be available remotely, but are under authentication layers. If you also deploy your Storybooks somewhere (e.g. Chromatic, Vercel, etc.), the Storybook URL can still be useful with the test-runner. You can pass it to the `REFERENCE_URL` environment variable when running the test-storybook command, and if a story fails, the test-runner will provide a helpful message with the link to the story in your published Storybook instead. ## Setting up code coverage @@ -392,7 +399,8 @@ Here's an example on how to achieve that: } ``` -> NOTE: If your other tests (e.g. Jest) are using a different coverageProvider than `babel`, you will have issue when merging the coverage files. [More info here](#merging-test-coverage-results-in-wrong-coverage). +> **Note** +> If your other tests (e.g. Jest) are using a different coverageProvider than `babel`, you will have issues when merging the coverage files. [More info here](#merging-test-coverage-results-in-wrong-coverage). ## Experimental test hook API @@ -406,7 +414,8 @@ The render functions are async functions that receive a [Playwright Page](https: All three functions can be set up in the configuration file `.storybook/test-runner.js` which can optionally export any of these functions. -> **NOTE:** These test hooks are experimental and may be subject to breaking changes. We encourage you to test as much as possible within the story's play function. +> **Note** +> These test hooks are experimental and may be subject to breaking changes. We encourage you to test as much as possible within the story's play function. ### DOM snapshot recipe diff --git a/bin/test-storybook.js b/bin/test-storybook.js index cd88a9fb..150e1038 100755 --- a/bin/test-storybook.js +++ b/bin/test-storybook.js @@ -13,6 +13,14 @@ const { getCliOptions } = require('../dist/cjs/util/getCliOptions'); const { getStorybookMetadata } = require('../dist/cjs/util/getStorybookMetadata'); const { transformPlaywrightJson } = require('../dist/cjs/playwright/transformPlaywrightJson'); +const glob_og = require('glob'); + +const glob = function (pattern, options) { + return new Promise((resolve, reject) => { + glob_og(pattern, options, (err, files) => (err === null ? resolve(files) : reject(err))); + }); +}; + // Do this as the first thing so that any code reading it knows the right env. process.env.BABEL_ENV = 'test'; process.env.NODE_ENV = 'test'; @@ -114,9 +122,18 @@ async function executeJestPlaywright(args) { const jest = require(jestPath); let argv = args.slice(2); - const jestConfigPath = fs.existsSync('test-runner-jest.config.js') - ? 'test-runner-jest.config.js' - : path.resolve(__dirname, '../playwright/test-runner-jest.config.js'); + // jest configs could either come in the root dir, or inside of the Storybook config dir + const configDir = process.env.STORYBOOK_CONFIG_DIR || ''; + const [userDefinedJestConfig] = ( + await Promise.all([ + glob(path.join(configDir, 'test-runner-jest*')), + glob(path.join('test-runner-jest*')), + ]) + ).reduce((a, b) => a.concat(b), []); + + const jestConfigPath = + userDefinedJestConfig || + path.resolve(__dirname, path.join('..', 'playwright', 'test-runner-jest.config.js')); argv.push('--config', jestConfigPath); diff --git a/package.json b/package.json index 6ede7e45..10b8bd0a 100644 --- a/package.json +++ b/package.json @@ -114,6 +114,7 @@ "can-bind-to-host": "^1.1.1", "commander": "^9.0.0", "expect-playwright": "^0.8.0", + "glob": "^8.1.0", "jest": "^28.0.0", "jest-circus": "^28.0.0", "jest-environment-node": "^28.0.0", diff --git a/yarn.lock b/yarn.lock index b9e66aec..ea6bb0b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5808,6 +5808,17 @@ glob@^7.0.0, glob@^7.1.2, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.2.0: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^8.1.0: + version "8.1.0" + resolved "https://registry.yarnpkg.com/glob/-/glob-8.1.0.tgz#d388f656593ef708ee3e34640fdfb99a9fd1c33e" + integrity sha512-r8hpEjiQEYlF2QU0df3dS+nxxSIreXQS1qRhMJM0Q5NDdR386C7jb7Hwwod8Fgiuex+k0GFjgft18yvxm5XoCQ== + dependencies: + fs.realpath "^1.0.0" + inflight "^1.0.4" + inherits "2" + minimatch "^5.0.1" + once "^1.3.0" + global-modules@^0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/global-modules/-/global-modules-0.2.3.tgz#ea5a3bed42c6d6ce995a4f8a1269b5dae223828d"