{"id":14967401,"url":"https://github.com/hugomrdias/playwright-test","last_synced_at":"2025-05-16T10:07:37.497Z","repository":{"id":37086875,"uuid":"240317939","full_name":"hugomrdias/playwright-test","owner":"hugomrdias","description":"Run unit tests with several test runners or benchmark inside real browsers with playwright and other Javascript runtimes.","archived":false,"fork":false,"pushed_at":"2025-04-28T15:51:21.000Z","size":1843,"stargazers_count":102,"open_issues_count":15,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-05T01:18:30.688Z","etag":null,"topics":["benchmark","benchmarkjs","code-coverage","cover","istanbul","mocha","mochajs","nyc","playwright","tape","test","testing","testing-tools","tests","typescript","zora"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hugomrdias.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"license","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-13T17:17:29.000Z","updated_at":"2025-02-11T14:47:25.000Z","dependencies_parsed_at":"2023-12-03T08:24:33.247Z","dependency_job_id":"d4f91c29-fb9a-4a4d-8a9f-b0f0c41ff99a","html_url":"https://github.com/hugomrdias/playwright-test","commit_stats":{"total_commits":319,"total_committers":10,"mean_commits":31.9,"dds":"0.46394984326018807","last_synced_commit":"5482e988d7529602775f999ac5dc34cbd74a0107"},"previous_names":[],"tags_count":100,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugomrdias%2Fplaywright-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugomrdias%2Fplaywright-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugomrdias%2Fplaywright-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hugomrdias%2Fplaywright-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hugomrdias","download_url":"https://codeload.github.com/hugomrdias/playwright-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252420918,"owners_count":21745154,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmark","benchmarkjs","code-coverage","cover","istanbul","mocha","mochajs","nyc","playwright","tape","test","testing","testing-tools","tests","typescript","zora"],"created_at":"2024-09-24T13:38:00.218Z","updated_at":"2025-05-16T10:07:32.481Z","avatar_url":"https://github.com/hugomrdias.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# playwright-test [![NPM Version](https://img.shields.io/npm/v/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) [![NPM Downloads](https://img.shields.io/npm/dt/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) [![NPM License](https://img.shields.io/npm/l/playwright-test.svg)](https://www.npmjs.com/package/playwright-test) ![tests](https://github.com/hugomrdias/playwright-test/workflows/tests/badge.svg)\n\n\u003e Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with `playwright`.\n\n## Install\n\n```shell\npnpm install playwright-test\n```\n\n## Usage\n\n```shell\nplaywright-test [files] [options]\n# or\npw-test [files] [options]\n\n```\n\n## Options\n\n```shell\nDescription\n    Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with `playwright` and in Node.\n\n  Usage\n    $ playwright-test [files] [options]\n\n  Options\n    -r, --runner       Test runner. Options: mocha, tape, zora, uvu, none, taps and benchmark. Internal runners are autodetected by default. It also accepts a path to a module or a module ID that exports a `playwrightTestRunner` object.\n    -b, --browser      Browser to run tests. Options: chromium, firefox, webkit.  (default chromium)\n    -m, --mode         Run mode. Options: main, worker and node.  (default main)\n    -d, --debug        Debug mode, keeps browser window open.  (default false)\n    -w, --watch        Watch files for changes and re-run tests.\n    -i, --incognito    Use incognito window to run tests.  (default false)\n    -e, --extension    Use extension background_page to run tests.  (default false)\n    --cov              Enable code coverage in istanbul format. Outputs '.nyc_output/coverage-pw.json'.  (default false)\n    --report-dir       Where to output code coverage in instanbul format.  (default .nyc_output)\n    --before           Path to a script to be loaded on a separate tab before the main script.\n    --sw               Path to a script to be loaded in a service worker.\n    --assets           Folder with assets to be served by the http server.  (default process.cwd())\n    --cwd              Current directory.  (default /Users/hd/code/playwright-test)\n    --extensions       File extensions allowed in the bundle.  (default js,cjs,mjs,ts,tsx)\n    --config           Path to the config file\n    -v, --version      Displays current version\n    -h, --help         Displays this message\n\n\n  Examples\n    $ playwright-test test.js --runner tape\n    $ playwright-test test --debug\n    $ playwright-test \"test/**/*.spec.js\" --browser webkit --mode worker --incognito --debug\n\n    $ playwright-test bench.js --runner benchmark\n    # Uses benchmark.js to run your benchmark see playwright-test/mocks/benchmark.js for an example.\n\n    $ playwright-test test --cov \u0026\u0026 npx nyc report --reporter=html\n    # Enable code coverage in istanbul format which can be used by nyc.\n\n    $ playwright-test \"test/**/*.spec.js\" --debug --before ./mocks/before.js\n    # Run a script in a separate tab. Check ./mocks/before.js for an example.\n    # Important: You need to call `self.PW_TEST.beforeEnd()` to start the main script.\n\n  Runner Options\n    All arguments passed to the cli not listed above will be fowarded to the runner.\n    $ playwright-test test.js --runner mocha --bail --grep 'should fail'\n\n    To send a `false` flag use --no-bail.\n    Check https://mochajs.org/api/mocha for `mocha` options or `npx mocha --help`.\n\n  Notes\n    DEBUG env var filtering for 'debug' package logging will work as expected.\n    $ DEBUG:app playwright-test test.js\n\n    Do not let your shell expand globs, always wrap them.\n    $ playwright-test \"test/**\" GOOD\n    $ playwright-test test/** BAD\n```\n\n## Client\n\nThis client package exposes the `playwright-test` options and some Playwright browser context methods to be used in tests.\n\n```ts\nimport * as Client from 'playwright-test/client'\n\nit('should setoffline', async () =\u003e {\n  if (Client.mode === 'main' \u0026\u0026 Client.options.extension === false) {\n    globalThis.addEventListener('offline', () =\u003e {\n      console.log('offlineee')\n    })\n    await Client.context.setOffline(true)\n    equal(navigator.onLine, false)\n    await Client.context.setOffline(false)\n    equal(navigator.onLine, true)\n  }\n})\n\nit('should geolocation', async () =\u003e {\n  if (Client.mode === 'main') {\n    const deferred = pdefer()\n    await Client.context.setGeolocation({\n      latitude: 59.95,\n      longitude: 30.316_67,\n    })\n    await Client.context.grantPermissions(['geolocation'])\n\n    navigator.geolocation.getCurrentPosition((position) =\u003e {\n      deferred.resolve(position)\n    })\n\n    const position = (await deferred.promise) as GeolocationPosition\n    equal(position.coords.latitude, 59.95)\n  }\n})\n```\n\n## Flow control\n\nAll test runners support automatic flow control, which means you don't need to call special function or trigger any event in your tests to stop the run. The `none` runner does not support flow control.\n\nTo manually stop the run you can use `process.exit`:\n\n```js\nprocess.exit(0) // stops the run and exits with success\nprocess.exit(1) // stops the run and exits with failure\n```\n\n## Custom test runner\n\nYou can define a custom test runner by passing a path to a file or a node module id that exports an object called `playwrightTestRunner` that implements the `TestRunner` interface.\n\n```shell\n\n$ playwright-test test.js --runner ./my-runner.js\n# or\n$ playwright-test test.js --runner my-runner\n\n```\n\nYou can also just define you test runner in the config file.\n\n```js\n// playwright-test.config.js\n/** @type {import('../src/runner.js').RunnerOptions} */\nconst config = {\n  testRunner: {\n    compileRuntime: (options, paths) =\u003e {\n      return `\nimport mocha from 'mocha/mocha.js'\nmocha.setup({\n    reporter: 'spec',\n    timeout: 5000,\n    ui: 'bdd',\n})\n\n${paths.map((url) =\u003e `await import('${url}')`).join('\\n')}\n\n  mocha\n    .run((f) =\u003e{\n      process.exit(f)\n    })\n        `\n    },\n  },\n}\n\nexport default config\n```\n\n````ts\nexport interface TestRunner {\n  /**\n   * Module ID name used to import the test runner runtime.\n   * Used in auto detection of the test runner.\n   */\n  moduleId: string\n  /**\n   * Options made available to the compiled runtime.\n   * This is useful to pass options to the test runner.\n   *\n   * @example\n   * ```js\n   * const options = JSON.parse(process.env.PW_OPTIONS)\n   * const testRunnerOptions = options.testRunner.options\n   * ```\n   */\n  options?: unknown\n  /**\n   * Esbuild config for the test runner\n   */\n  buildConfig?: BuildOptions\n  /**\n   * Compile runtime entry point for esbuild\n   *\n   * @param options - Runner options\n   * @param testPaths - Test paths\n   * @returns\n   */\n  compileRuntime: (options: RunnerOptions, testPaths: string[]) =\u003e string\n}\n````\n\n## Config\n\n\u003e The config file needs to be commonjs for now, so if your package is pure ESM you need to use `.cjs` extension.\n\nConfiguration can be done with cli flags or config files.\n\n```text\npackage.json, // using property `pw-test` or `playwright-test`\n.playwright-testrc.json,\n.playwright-testrc.js,\n.playwright-testrc.cjs,\nplaywright-test.config.js,\nplaywright-test.config.cjs,\n.pw-testrc.json,\n.pw-testrc.js,\n.pw-testrc.cjs,\npw-test.config.js,\npw-test.config.cjs,\n.config/playwright-testrc.json\n.config/playwright-testrc.js\n.config/playwright-testrc.cjs\n.config/pw-testrc.json\n.config/pw-testrc.js\n.config/pw-testrc.cjs\n```\n\nThe config type can be imported from the entrypoint.\n\n```ts\n/** @type {import('playwright-test').RunnerOptions} */\nconst config = {\n  // ...\n}\n\nexport default config\n```\n\nThe config file can also export a function that receives the cli options as argument.\n\n```ts\n/** @type {import('playwright-test').ConfigFn} */\nfunction buildConfig(cliOptions) {\n  return {\n    buildConfig: {\n      bundle: cliOptions.mode !== 'node',\n    },\n  }\n}\n\nexport default buildConfig\n```\n\n### Interface\n\n```ts\nexport interface RunnerOptions {\n  input?: string[]\n  testRunner: TestRunner\n  cwd: string\n  extensions: string\n  browser: 'chromium' | 'firefox' | 'webkit'\n  debug: boolean\n  mode: 'main' | 'worker' | 'node'\n  incognito: boolean\n  extension: boolean\n  assets: string\n  before?: string\n  sw?: string\n  cov: boolean\n  reportDir: string\n  buildConfig: BuildOptions\n  buildSWConfig: BuildOptions\n  browserContextOptions?: BrowserContextOptions\n  beforeTests: (opts: RunnerOptions) =\u003e Promise\u003cunknown\u003e\n  afterTests: (\n    opts: RunnerOptions,\n    beforeTestsOutput: unknown\n  ) =\u003e Promise\u003cunknown\u003e\n}\n```\n\n## Run in CI\n\nCheck our CI config `.github/workflows/main.yml` and the playwright [Github Action](https://playwright.dev/docs/ci/#github-actions)\n\n## License\n\nMIT © [Hugo Dias](http://hugodias.me)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugomrdias%2Fplaywright-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhugomrdias%2Fplaywright-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhugomrdias%2Fplaywright-test/lists"}