{"id":13711419,"url":"https://github.com/cypress-io/cypress-fiddle","last_synced_at":"2025-05-06T20:32:56.112Z","repository":{"id":37820004,"uuid":"201104896","full_name":"cypress-io/cypress-fiddle","owner":"cypress-io","description":"Quickly generates Cypress tests from HTML and JS code","archived":true,"fork":false,"pushed_at":"2023-02-27T07:52:13.000Z","size":3969,"stargazers_count":138,"open_issues_count":19,"forks_count":7,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-05-13T00:33:29.052Z","etag":null,"topics":["cypress","cypress-example"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cypress-io.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-08-07T18:18:50.000Z","updated_at":"2023-12-03T10:55:05.000Z","dependencies_parsed_at":"2024-01-29T16:57:38.948Z","dependency_job_id":"50e57fe2-6d57-4326-8c03-135e4902f7a2","html_url":"https://github.com/cypress-io/cypress-fiddle","commit_stats":{"total_commits":287,"total_committers":9,"mean_commits":31.88888888888889,"dds":0.578397212543554,"last_synced_commit":"f95f0a96b52e2047847a12f4a79c0d1e947a76d6"},"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cypress-io%2Fcypress-fiddle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cypress-io%2Fcypress-fiddle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cypress-io%2Fcypress-fiddle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cypress-io%2Fcypress-fiddle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cypress-io","download_url":"https://codeload.github.com/cypress-io/cypress-fiddle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224160357,"owners_count":17265936,"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":["cypress","cypress-example"],"created_at":"2024-08-02T23:01:08.001Z","updated_at":"2024-11-13T21:31:50.474Z","avatar_url":"https://github.com/cypress-io.png","language":"JavaScript","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# cypress-fiddle [![renovate-app badge][renovate-badge]][renovate-app] [![CircleCI](https://circleci.com/gh/cypress-io/cypress-fiddle/tree/master.svg?style=svg)](https://circleci.com/gh/cypress-io/cypress-fiddle/tree/master)\n\u003e Generate Cypress tests live from HTML and JS\n\nInstantly experiment with Cypress tests by creating a tiny live HTML fiddle and running E2E tests against it.\n\n![runExample test](images/runExample.png)\n\n## Install\n\nCypress is a peer dependency of this module. Install the current version of Cypress by running `npm i -D cypress`.\n\nAfter installing Cypress, install this module via npm:\n\n```shell\nnpm i -D @cypress/fiddle\n```\n\nThen load the custom command by adding the following line to `cypress/support/index.js`\n\n```js\n// adds \"cy.runExample()\" command\nimport '@cypress/fiddle'\n```\n\n## Use\n\n### Create a single test\n\nYou can take an object with an `html` property containing HTML and a `test` property containing Cypress commands and run the tests.\n\nFor example in the `cypress/integration/spec.js` file:\n\n```js\n// loads TypeScript definition for Cypress\n// and \"cy.runExample\" custom command\n/// \u003creference types=\"@cypress/fiddle\" /\u003e\n\nconst helloTest = {\n  html: `\n    \u003cdiv\u003eHello\u003c/div\u003e\n  `,\n  test: `\n    cy.get('div').should('have.text', 'Hello')\n  `\n}\n\nit('tests hello', () =\u003e {\n  cy.runExample(helloTest)\n})\n```\n\nWhich produces\n\n![runExample test](images/runExample.png)\n\n### Parameters\n\nThe test object can have multiple properties, see [src/index.d.ts](src/index.d.ts) for all.\n\n- `test` JavaScript with Cypress commands, required\n\nThe rest of the properties are optional\n\n- `html` to mount as DOM nodes before the test begins\n- `name` the name to display at the top of the page, otherwise the test title will be used\n- `description` extra test description under the name, supports Markdown via [safe-marked](https://github.com/azu/safe-marked)\n- `commonHtml` is extra HTML markup to attach to the live HTML (if any) element. Useful for loading external stylesheets or styles without cluttering every HTML block\n\nThe next properties are NOT used by `cy.runExample` but are used by the `testExamples` function from this package.\n\n- `skip` creates a skipped test with `it.skip`\n- `only` creates an exclusive test with `it.only`\n\n### Included scripts\n\n- [jQuery minified](https://code.jquery.com/)\n- [Highlight.js](https://highlightjs.org/)\n\nYou can include your own additional scripts by using environment variable block in `cypress.json` file\n\n```json\n{\n  \"env\": {\n    \"cypress-fiddle\": {\n      \"scripts\": [\n        \"https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js\"\n      ]\n    }\n  }\n}\n```\n\n### Styles\n\nSometimes you want to inject external stylesheets and maybe custom style CSS into the frame (we already include Highlight.js). Pass additional CSS link urls and custom styles through environment variables in `cypress.json` config file.\n\n```json\n{\n  \"env\": {\n    \"cypress-fiddle\": {\n      \"stylesheets\": [\n        \"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css\"\n      ],\n      \"style\": \"body { padding: 1rem; }\"\n    }\n  }\n}\n```\n\n**Tip:** it is more convenient to set multiline environment variables or even load CSS files from plugins file.\n\n### Create multiple tests\n\nInstead of writing the `cy.runExample()` command one by one, you can collect all test definitions into a list or a nested object of suites and create tests automatically.\n\nFor example, here is a list of tests created from an array:\n\n```js\nimport { testExamples } from '@cypress/fiddle'\n\nconst tests = [\n  {\n    name: 'first test',\n    description: 'cy.wrap() example',\n    test: `\n      cy.wrap('hello').should('have.length', 5)\n    `\n  },\n  {\n    name: 'second test',\n    description: 'cy.wrap() + .then() example',\n    test: `\n        cy.wrap()\n          .then(() =\u003e {\n            cy.log('In .then')\n          })\n      `\n  }\n]\ntestExamples(tests)\n```\n\n![List of tests](images/list.png)\n\nWhile working with tests, you can skip a test or make it exclusive. For example to skip the first test add a `skip: true` property.\n\n```js\n{\n  name: 'first test',\n  description: 'cy.wrap example',\n  skip: true\n  ...\n}\n```\n\nOr run just a single test by using the `only: true` property.\n\n```js\n{\n  name: 'first test',\n  description: 'cy.wrap example',\n  only: true\n  ...\n}\n```\n\nYou can create suites by having nested objects. Each object becomes either a suite or a test.\n\n```js\nimport { testExamples } from '@cypress/fiddle'\nconst suite = {\n  'parent suite': {\n    'inner suite': [\n      {\n        name: 'a test',\n        html: `\n          \u003cdiv id=\"name\"\u003eJoe\u003c/div\u003e\n        `,\n        test: `\n          cy.contains('#name', 'Joe')\n        `\n      }\n    ],\n    'list test': {\n      html: `\n        \u003cul\u003e\n          \u003cli\u003eAlice\u003c/li\u003e\n          \u003cli\u003eBob\u003c/li\u003e\n          \u003cli\u003eCory\u003c/li\u003e\n        \u003c/ul\u003e\n      `,\n      test: `\n        cy.get('li').should('have.length', 3)\n          .first().should('contain', 'Alice')\n      `\n    }\n  }\n}\n\ntestExamples(suite)\n```\n\n![Tree of tests](images/tree.png)\n\nFind more examples in [cypress/integration](cypress/integration) folder.\n\n\n\n### Markdown\n\nThis package includes a JS/CoffeeScript/Markdown preprocessor that can find and run tests in `.md` files. Just surround the tests with HTML comments like this:\n\n    \u003c!-- fiddle Test name here --\u003e\n    Add additional text if you want. HTML code block is optional.\n\n    ```html\n    \u003cdiv\u003eExample\u003c/div\u003e\n    ```\n\n    Test code block that should be run as a test\n    ```js\n    cy.contains('Bye').should('be.visible')\n    ```\n    \u003c!-- fiddle-end --\u003e\n\nSee example [bahmutov/vuepress-cypress-test-example](https://github.com/bahmutov/vuepress-cypress-test-example) and [live site](https://vuepress-cypress-test-example.netlify.com/). Read blog posts [Run End-to-end Tests from Markdown Files](https://glebbahmutov.com/blog/cypress-fiddle/) and [Self-testing JAM pages](https://www.cypress.io/blog/2019/11/13/self-testing-jam-pages/).\n\nYou can have common HTML block and split the test across multiple JavaScript code blocks. This is useful to explain the test step by step\n\n    This test has multiple parts. First, it confirms the string value\n    ```js\n    cy.wrap('first').should('equal', 'first')\n    ```\n    Then it checks if 42 is 42\n    ```js\n    cy.wrap(42).should('equal', 42)\n    ```\n\nThe actual test to be executed will be\n```js\ncy.wrap('first').should('equal', 'first')\ncy.wrap(42).should('equal', 42)\n```\n\n### Skip and only\n\nYou can skip a fiddle, or run only a particular fiddle similar to `it.skip` and `it.only`\n\n```\n\u003c!-- fiddle.skip this is a skipped test --\u003e\n\u003c!-- fiddle.only this is an exclusive test --\u003e\n```\n\n**Note:** there is also `fiddle.export` modifier. These fiddles are skipped during normal testing from Markdown, but exported and enabled in the output JavaScript specs.\n\n### Page title\n\nIf the Markdown file has page title line like `# \u003csome text\u003e`, it will be used to create the top level suite of tests\n\n```js\ndescribe('\u003csome text\u003e', () =\u003e {\n  // tests\n})\n```\n\n### Nested suites\n\nYou can put a fiddle into nested suites using `/` as a separator\n\n```\n\u003c!-- fiddle Top / nested / test --\u003e\n```\n\nWill create\n\n```js\ndescribe('Top', () =\u003e {\n  describe('nested', () =\u003e {\n    it('test', () =\u003e {})\n  })\n})\n```\n\n### Live HTML\n\nYou can include \"live\" html blocks in the fiddle - in that case they will become the test fragment.\n\n    \u003c!-- fiddle includes live html --\u003e\n    \u003cdiv id=\"live-block\"\u003eLive Block\u003c/div\u003e\n    ```js\n    cy.contains('#live-block', 'Live Block').should('be.visible')\n    ```\n    \u003c!-- fiddle-end --\u003e\n\nWhen including HTML source fragment and live HTML block, live HTML block wins and will be used as the test fragment.\n\n    \u003c!-- fiddle includes both live and html block --\u003e\n    ```html\n    \u003cdiv id=\"my-block\"\u003eBlock\u003c/div\u003e\n    ```\n\n    \u003cdiv id=\"live-block\"\u003eLive Block\u003c/div\u003e\n\n    ```js\n    // when including both live HTML block and\n    // html code block, the live HTML block wins\n    cy.contains('#live-block', 'Live Block').should('be.visible')\n    cy.contains('#my-block', 'Block').should('not.exist')\n    ```\n    \u003c!-- fiddle-end --\u003e\n\n### Common Live HTML\n\nIf you have common HTML to load before the live HTML block, but do not want to show it in the HTML snippet, put it into a comment like this\n\n    \u003c!-- fiddle-markup\n    \u003clink rel=\"stylesheet\" href=\"some CSS URL\"\u003e\n    \u003cstyle\u003e\n    body {\n      padding: 2rem;\n    }\n    \u003c/style\u003e\n    --\u003e\n\nYou can load styles and external CDN scripts using this approach.\n\n### Hiding fiddle in Markdown\n\nYou can \"hide\" fiddle inside Markdown so the page _can test itself_. See [cypress/integration/hidden-fiddle.md](cypress/integration/hidden-fiddle.md) example.\n\n**Markdown**\n\n![Hidden fiddle Markdown](images/hidden-fiddle.png)\n\n**Rendered page**\n\n![Hidden fiddle Markdown preview](images/hidden-fiddle-preview.png)\n\nNotice how only the summary is displayed\n\n**Test runner**\n\n![Hidden fiddle test](images/hidden-fiddle-test.png)\n\n**Note:** by default the summary element is displayed in the HTML. You can the fiddle completely using\n```html\n\u003cdetails style=\"display:none\"\u003e\n ...\n\u003c/details\u003e\n```\n\n#### Installation\n\nIn your plugins file use\n\n```js\nconst mdPreprocessor = require('@cypress/fiddle/src/markdown-preprocessor')\nmodule.exports = (on, config) =\u003e {\n  on('file:preprocessor', mdPreprocessor)\n}\n```\n\nAnd in `cypress.json` file allow Markdown files\n\n```json\n{\n  \"testFiles\": \"*.md\"\n}\n```\n\nWarning: [issue #5401](https://github.com/cypress-io/cypress/issues/5401)\n\n## Exporting JS specs from Markdown fiddles\n\n```shell\n# saves path/to/md.js file\nnpx export-fiddle \u003cpath/to/md\u003e\n# adds optional \"beforeEach\" hook with cy.visit(url)\nnpx export-fiddle \u003cpath/to/md\u003e --before-each \u003curl\u003e\n# adds optional \"before\" hook with cy.visit(url)\nnpx export-fiddle \u003cpath/to/md\u003e --before \u003curl\u003e\n```\n\n## Debug\n\nTo see debug logs, use `DEBUG=@cypress/fiddle` when running Cypress.\n\n## Publishing\n\nAutomatic semantic release on CircleCI using [Cypress Circle Orb](https://github.com/cypress-io/circleci-orb), see [circle.yml](circle.yml) file.\n\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcypress-io%2Fcypress-fiddle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcypress-io%2Fcypress-fiddle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcypress-io%2Fcypress-fiddle/lists"}