[
  {
    "path": ".gitignore",
    "content": ".idea\nnode_modules\ncypress/mocks\ncypress/fixtures"
  },
  {
    "path": ".travis.yml",
    "content": "language: node_js\nnode_js:\n  - 10\naddons:\n  apt:\n    packages:\n      # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves\n      - libgconf-2-4\ncache:\n  # Caches $HOME/.npm when npm ci is default script command\n  # Caches node_modules in all other cases\n  npm: true\n  directories:\n    # we also need to cache folder with Cypress binary\n    - ~/.cache\ninstall:\n  - npm ci\nscript:\n  - npm test\n"
  },
  {
    "path": "CHANGELOG.md",
    "content": "# Changelog\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## [3.1.2] - 2021-09-13\n### Fixed\n- `interceptPattern` doesn't work with regex, resolving issue [#56](https://github.com/Nanciee/cypress-autorecord/issues/56) [[ERuseckas](https://github.com/ERuseckas)]\n- FixtureId is not working with `req.reply`, resolving issues [#32](https://github.com/Nanciee/cypress-autorecord/issues/32), [#54](https://github.com/Nanciee/cypress-autorecord/issues/54), [#55](https://github.com/Nanciee/cypress-autorecord/issues/55) [[mhssmnn](https://github.com/mhssmnn)]\n## [3.1.1] - 2021-06-10\n### Fixed\n- Fix crash when loading request stored in a fixture [[lefta](https://github.com/lefta)]\n## [3.1.0] - 2021-05-26\n### Added\n- Config `interceptPattern` which allows you to specify which endpoints you want to listen and mock [[Ika-x](https://github.com/Ika-x)]\n### Fixed\n- Sending request to actual server instead of mocking, resolving issues [#51](https://github.com/Nanciee/cypress-autorecord/issues/51) [[Ika-x](https://github.com/Ika-x)]\n## [3.0.0] - 2021-05-05\n### Changed\n- __[BREAKING CHANGE]__ Replace the underlying mechanism for stubbing and mocking to align with Cypress' new API in v6.x and v7.x  [[mhssmnn](https://github.com/mhssmnn)]\n### Fixed\n- Restore cy.clock in the beforeEach to allow specs to continue running [[ha404](https://github.com/ha404)]\n\n## [2.0.1] - 2021-01-14\n### Fixed\n- `Fetch()` 'get' requests no longer breaks tests when running with the recorded mocks, resolving issue [#40](https://github.com/Nanciee/cypress-autorecord/issues/40)\n\n## [2.0.0] - 2020-09-18\n\n### Added\n- __[BREAKING CHANGE]__ Organize fixtures by spec [[bautistaaa](https://github.com/bautistaaa)]\n- Use cy.now(...) to insert new stubbed route [[bebop23](https://github.com/bebop23)]\n- Add timestamp to tests so browser is brought back to the time where the mocks live [[bebop23](https://github.com/bebop23)]\n- Add new stub when response data changes [[bebop23](https://github.com/bebop23)]\n### Fixed\n- Convert blob responses to plain text so it can be properly recorded, resolving issues [#7](https://github.com/Nanciee/cypress-autorecord/issues/7) and [#22](https://github.com/Nanciee/cypress-autorecord/issues/22) [[lcnandre](https://github.com/lcnandre)]\n- Resolve cypress 5.x incompatibility, resolving issue [#34](https://github.com/Nanciee/cypress-autorecord/issues/34) [[jrocketfingers](https://github.com/jrocketfingers)]\n\n## [1.1.1] - 2019-07-22\n### Fixed\n- Only record if the request coming in doesn't have a matching URL, METHOD, and BODY, resolving issue [#5](https://github.com/Nanciee/cypress-autorecord/issues/5) [[bautistaaa](https://github.com/bautistaaa)]\n\n## [1.1.0] - 2019-07-22\n### Added\n- Feature for recording HEAD requests [[alejo90](https://github.com/alejo90)]\n- `index.d.ts` to remove Typescript compiler warning [[alejo90](https://github.com/alejo90)]\n- Feature for storing a set of whitelisted headers [[alejo90](https://github.com/alejo90)]\n- CHANGELOG.md\n### Changed\n- README.md is updated to reflect changes\n## Removed\n- Remove the need to pass in `__filename` when calling `autoRecord()` [[alejo90](https://github.com/alejo90)]\n## Fixed\n- All requests that has the same url and methods but different request bodies no longer just return the last request body\n\n## [1.0.13] - 2019-07-20\n### Added\n- Feature for recording PUT requests [[chauey](https://github.com/chauey)]\n### Changed\n- README.md now includes the \"Known Issues\" section\n\n## [1.0.12] - 2019-04-21\n### Fixed\n- Filename for mocks copies entire test name minus the extension [[fraserxu](https://github.com/fraserxu)]\n\n## [1.0.11] - 2019-04-11\n### Fixed\n- Test cases using global asserts\n\n## [1.0.10] - 2019-04-01\n### Changed\n- README.md now include the \"How It Works\" section\n\n## [1.0.9] - 2019-03-31\n### Added\n- README.md explaining current features\n### Fixed\n- POST requests that has the same url but different request bodies no longer just return the last request body \n\n## [1.0.8] - 2019-03-27\n### Added\n- Feature to auto record and stub xhr requests\n- Feature to update mocks by inserting [r] in the name of the test\n- Feature to clean mocks\n- Feature to blacklist routes and prevent it from being recorded\n\n\n"
  },
  {
    "path": "README.md",
    "content": "# Cypress Autorecord\n\nCypress Autorecord is a plugin built to be used with Cypress.io. It simplifies mocking by auto-recording/stubbing HTTP interactions and automating the process of updating/deleting recordings. Spend more time writing integration tests instead of managing your mock data. Refer to the [changelog](https://github.com/Nanciee/cypress-autorecord/blob/master/CHANGELOG.md) for more details on all the changes.\n\n## v3.0.0 is now live!\nVersion 3 is now compatible with Cypress 6 and 7 and includes a few fixes. If you are using an earlier cypress version, you will need to use cypress-autorecord v2.x.\n\n## Getting Started\n\nInstall from npm\n\n```\nnpm install --save-dev cypress-autorecord\n```\n\nAdd this snippet in your project's `/cypress/plugins/index.js`\n\n```js\nconst fs = require('fs');\nconst autoRecord = require('cypress-autorecord/plugin');\n\nmodule.exports = (on, config) => {\n  autoRecord(on, config, fs);\n};\n```\nTo allow for auto-recording and stubbing to work, require cypress-autorecord in each of your test file and call the function at the beginning of your parent `describe` block.\n\n```js\nconst autoRecord = require('cypress-autorecord'); // Require the autorecord function\n  \ndescribe('Home Page', function() { // Do not use arrow functions\n  autoRecord(); // Call the autoRecord function at the beginning of your describe block\n  \n  // Your hooks (beforeEach, afterEach, etc) goes here\n  \n  it('...', function() { // Do not use arrow functions\n    // Your test goes here\n  });\n});\n```\n\n**_NOTE: Do not use ES6 arrow functions for your describe or it callback. This will cause the recording function to break._**\n\nThat is it! Now, just run your tests and the auto-record will take care of the rest!\n\n## Updating Mocks\n\nIn the case you need to update your mocks for a particular test:\n```js\nconst autoRecord = require('cypress-autorecord');\n  \ndescribe('Home Page', function() {\n  autoRecord();\n  \n  it('[r] my awesome test', function() { // Insert [r] at the beginning of your test name\n    // ...\n  });\n});\n```\nThis will force the test to record over your existent mocks for **ONLY** this test on your next run.\n\nThis can also be done through the configurations by adding the test name in the file `cypress.json`:\n\n```json\n{\n  \"autorecord\": {\n    \"recordTests\": [\"my awesome test\"]\n  }\n}\n```\n\nAlternatively, you can update recordings for all tests by setting `forceRecord` to `true` before rerunning your tests:\n\n```json\n{\n  \"autorecord\": {\n    \"forceRecord\": true\n  }\n}\n```\n\n## Removing Stale Mocks\n\nStale mocks that are no longer being used can be automatically removed when you run your tests by setting `cleanMocks` to `true` in the file `cypress.json`:\n\n```json\n{\n  \"autorecord\": {\n    \"cleanMocks\": true\n  }\n}\n```\n\n**_NOTE: Only mocks that are used during the run are considered \"active\". Make sure to only set `cleanMocks` to `true` when you are running ALL your tests. Remove any unintentional `.only` or `.skip`._**\n\n## Set Recording Pattern For Cypress Intercept\n\nBy default autorecorder is recording all outgoing requests but if you want to record only specific calls based on pattern(Ex. just record api calls on backend), you can set `interceptPattern` in `cypress.json`. it can be string, regex or glob\n\n```json\n{\n  \"autorecord\": {\n    \"interceptPattern\": \"http://localhost:3000/api/*\"\n  }\n}\n```\n\n## How It Works\n\n### How does the recording and stubbing work?\nCypress Autorecord uses Cypress' built-in `cy.intercept` to hook into every request, including GET, POST, DELETE and PUT. If mocks doesn't exist for a test, the http calls (requests and responses) are captured and automatically written to a local file. If mocks exist for a test, each http call will be stubbed in the `beforeEach` hook.\n\n### Where are the mocks saved?\nThe mocks will be automatically generated and saved in the `/cypress/mocks/` folder. Mocks are grouped by test name and test file name. You will find mock files matching the name of your test files. Within your mock files, mocks are organized by test names in the order that they were called. Changing the test file name or test name will result to a disconnection to the mocks and trigger a recording on your next run.\n\n### Can I manually update the mocks?\nMocks are saved as a simple json object and can be updated manually. This is **not** recommended since any manual change you make will be overwritten when you automatically update the mocks. Leave the data management to cypress-autorecord. Make any modifications to the http calls inside your test so that it will be consistent across recordings.\n\n```js\nit('should display an error message when send message fails', function() {\n  cy.route({\n    url: '/message',\n    method: 'POST',\n    status: 404,\n    response: { error: 'It did not work' },\n  });\n\n  cy.get('[data-cy=\"msgInput\"]').type('Hello World!');\n  cy.get('[data-cy=\"msgSend\"]').click();\n  cy.get('[data-cy=\"errorMessage\"]').should('contain', 'Looks like we ran into a problem. Please try again.');\n});\n```\n\n## Known Issues\n\n#### Only XMLHttpRequests will be recorded and stubbed\nCypress-autorecord leverages Cypress' built in `cy.route` to handle stubbing, which means that it inherits some limitations as well. This is the disclaimer on the `cy.route` documentation page with some potential workarounds:\n>Please be aware that Cypress only currently supports intercepting XMLHttpRequests. Requests using the Fetch API and other types of network requests like page loads and <script> tags will not be intercepted or visible in the Command Log. See [#95](https://github.com/cypress-io/cypress/issues/95) for more details and temporary workarounds.\n\n## Contributions\nI would really appreciate any help with bug fixes or any new features you think might be relevant! Feel free to submit a PR!"
  },
  {
    "path": "cypress/integration/fixture.spec.js",
    "content": "// Set config before importing the autorecord file\nCypress.config('autorecord', { maxInlineResponseSize: 0.00001 });\n\nconst autoRecord = require('../../index');\nconst testName = 'records a mock after the test has finished';\n\n// Ensures the next test doesn't load fixtures before they're\n// deleted!\ndescribe('beforeSetup', function () {\n  beforeEach(function () {\n    cy.task('removeAllMocks');\n  });\n\n  it('deletes the mocks', function () {\n    cy.readFile('../mocks/fixture.spec.json').should('not.exist');\n  });\n});\n\ndescribe('setup', function () {\n  autoRecord();\n\n  beforeEach(function () {\n    cy.visit('cypress/integration/index.html');\n  });\n\n  it(testName, function () {\n    cy.readFile('../mocks/fixture.spec.json').should('not.exist');\n    cy.readFile('../fixtures/fixture-spec').should('not.exist');\n    // Ensure the http request has finished\n    cy.contains(/\"userId\":1/i);\n  });\n});\n\ndescribe('test', function () {\n  context('the generated mock file', function () {\n    it('should contain the fixtureId', function () {\n      cy.readFile('cypress/mocks/fixture.spec.json').then((mock) => {\n        cy.wrap(mock).its(testName).should('exist');\n\n        const { routes } = mock[testName];\n        const [route] = routes;\n\n        expect(route).to.have.property('fixtureId');\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "cypress/integration/index.html",
    "content": "<body>\n  <main>\n    <h1>cypress-autorecord</h1>\n    <div>\n      <pre id=\"json\"></pre>\n    </div>\n  </main>\n  <script>\n    // var oReq = new XMLHttpRequest();\n    // oReq.addEventListener('load', function () {\n    //   var json = JSON.parse(this.responseText);\n    //   document.getElementById('json').innerText = JSON.stringify(json);\n    // });\n    // oReq.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');\n    // oReq.send();\n\n    fetch('https://jsonplaceholder.typicode.com/todos/1')\n      .then((response) => response.json())\n      .then((json) => {\n        document.getElementById('json').innerText = JSON.stringify(json);\n      });\n  </script>\n</body>\n"
  },
  {
    "path": "cypress/integration/spec.js",
    "content": "const autoRecord = require('../../index');\nconst testName = 'records a mock after the test has finished';\n\ndescribe('setup', function () {\n  autoRecord();\n\n  beforeEach(function () {\n    cy.task('removeAllMocks');\n    cy.visit('cypress/integration/index.html');\n  });\n\n  it(testName, function () {\n    cy.readFile('../mocks/spec.json').should('not.exist');\n    // Ensure the http request has finished\n    cy.contains(/\"userId\":1/i);\n  });\n});\n\ndescribe('test', function () {\n  context('the generated mock file', function () {\n    it('should contain the json response', function () {\n      cy.readFile('cypress/mocks/spec.json').then((mock) => {\n        cy.wrap(mock).its(testName).should('exist');\n\n        const { routes } = mock[testName];\n        const [{ response }] = routes;\n\n        expect(response).to.include({ userId: 1, id: 1 });\n      });\n    });\n  });\n});\n"
  },
  {
    "path": "cypress/plugins/index.js",
    "content": "/// <reference types=\"cypress\" />\n// ***********************************************************\n// This example plugins/index.js can be used to load plugins\n//\n// You can change the location of this file or turn off loading\n// the plugins file with the 'pluginsFile' configuration option.\n//\n// You can read more here:\n// https://on.cypress.io/plugins-guide\n// ***********************************************************\n\n// This function is called when a project is opened or re-opened (e.g. due to\n// the project's config changing)\n\n/**\n * @type {Cypress.PluginConfig}\n */\n\nconst fs = require('fs');\nconst autoRecord = require('../../plugin');\n\nmodule.exports = (on, config) => {\n  autoRecord(on, config, fs);\n};\n"
  },
  {
    "path": "cypress/support/commands.js",
    "content": "// ***********************************************\n// This example commands.js shows you how to\n// create various custom commands and overwrite\n// existing commands.\n//\n// For more comprehensive examples of custom\n// commands please read more here:\n// https://on.cypress.io/custom-commands\n// ***********************************************\n//\n//\n// -- This is a parent command --\n// Cypress.Commands.add(\"login\", (email, password) => { ... })\n//\n//\n// -- This is a child command --\n// Cypress.Commands.add(\"drag\", { prevSubject: 'element'}, (subject, options) => { ... })\n//\n//\n// -- This is a dual command --\n// Cypress.Commands.add(\"dismiss\", { prevSubject: 'optional'}, (subject, options) => { ... })\n//\n//\n// -- This will overwrite an existing command --\n// Cypress.Commands.overwrite(\"visit\", (originalFn, url, options) => { ... })\n"
  },
  {
    "path": "cypress/support/index.js",
    "content": "// ***********************************************************\n// This example support/index.js is processed and\n// loaded automatically before your test files.\n//\n// This is a great place to put global configuration and\n// behavior that modifies Cypress.\n//\n// You can change the location of this file or turn off\n// automatically serving support files with the\n// 'supportFile' configuration option.\n//\n// You can read more here:\n// https://on.cypress.io/configuration\n// ***********************************************************\n\n// Import commands.js using ES2015 syntax:\nimport './commands';\n\n// Alternatively you can use CommonJS syntax:\n// require('./commands')\n"
  },
  {
    "path": "cypress.json",
    "content": "{\n  \"video\": false,\n  \"screenshotOnRunFailure\": false,\n  \"testFiles\": \"**/*spec.{js,jsx,ts,tsx}\"\n}\n"
  },
  {
    "path": "index.d.ts",
    "content": "declare function autoRecord(): void;\n\nexport = autoRecord;\n"
  },
  {
    "path": "index.js",
    "content": "'use strict';\nconst path = require('path');\nconst util = require('./util');\n\nconst guidGenerator = util.guidGenerator;\nconst sizeInMbytes = util.sizeInMbytes;\nconst blobToPlain = util.blobToPlain;\n\nconst cypressConfig = Cypress.config('autorecord') || {};\nconst isCleanMocks = cypressConfig.cleanMocks || false;\nconst isForceRecord = cypressConfig.forceRecord || false;\nconst recordTests = cypressConfig.recordTests || [];\nconst blacklistRoutes = cypressConfig.blacklistRoutes || [];\n\nlet interceptPattern = cypressConfig.interceptPattern || '*';\nconst interceptPatternFragments =\n  interceptPattern.match(/\\/(.*?)\\/([a-z]*)?$/i);\nif (interceptPatternFragments) {\n  interceptPattern = new RegExp(\n    interceptPatternFragments[1],\n    interceptPatternFragments[2] || \"\"\n  );\n}\n\nconst whitelistHeaders = cypressConfig.whitelistHeaders || [];\nconst maxInlineResponseSize = cypressConfig.maxInlineResponseSize || 70;\nconst supportedMethods = ['get', 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD'];\n\nconst fileName = path.basename(\n    Cypress.spec.name,\n    path.extname(Cypress.spec.name),\n);\n// The replace fixes Windows path handling\nconst fixturesFolder = Cypress.config('fixturesFolder').replace(/\\\\/g, '/');\nconst fixturesFolderSubDirectory = fileName.replace(/\\./, '-');\nconst mocksFolder = path.join(fixturesFolder, '../mocks');\n\nbefore(function() {\n  if (isCleanMocks) {\n    cy.task('cleanMocks');\n  }\n\n  if (isForceRecord) {\n    cy.task('removeAllMocks');\n  }\n});\n\nmodule.exports = function autoRecord() {\n  const whitelistHeaderRegexes = whitelistHeaders.map((str) => RegExp(str));\n\n  // For cleaning, to store the test names that are active per file\n  const testNames = [];\n  // For cleaning, to store the clean mocks per file\n  const cleanMockData = {};\n  // Locally stores all mock data for this spec file\n  let routesByTestId = {};\n  // For recording, stores data recorded from hitting the real endpoints\n  let routes = [];\n  // Stores any fixtures that need to be added\n  const addFixture = {};\n  // Stores any fixtures that need to be removed\n  const removeFixture = [];\n  // For force recording, check to see if [r] is present in the test title\n  let isTestForceRecord = false;\n  // Timestamp for when this test was executed\n  let timestamp = null;\n\n  before(function() {\n    // Get mock data that relates to this spec file\n    cy.task('readFile', path.join(mocksFolder, `${fileName}.json`)).then((data) => {\n      routesByTestId = data === null ? {} : data;\n    });\n  });\n\n  beforeEach(function() {\n    // Reset routes before each test case\n    routes = [];\n\n    cy.intercept(interceptPattern, (req) => {\n      // This is cypress loading the page\n      if (\n        Object.keys(req.headers).some((k) => k === 'x-cypress-authorization')\n      ) {\n        return;\n      }\n\n      req.reply((res) => {\n        const url = req.url;\n        const status = res.statusCode;\n        const method = req.method;\n        const data =\n          res.body.constructor.name === 'Blob'\n            ? blobToPlain(res.body)\n            : res.body;\n        const body = req.body;\n        const headers = Object.entries(res.headers)\n          .filter(([key]) =>\n            whitelistHeaderRegexes.some((regex) => regex.test(key)),\n          )\n          .reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});\n\n        // We push a new entry into the routes array\n        // Do not rerecord duplicate requests\n        if (\n          !routes.some(\n            (route) =>\n              route.url === url &&\n              route.body === body &&\n              route.method === method &&\n              // when the response has changed for an identical request signature\n              // add this entry as well.  This is useful for polling-oriented endpoints\n              // that can have varying responses.\n              route.response === data,\n          )\n        ) {\n          routes.push({ url, method, status, data, body, headers });\n        }\n      });\n    });\n\n    // check to see if test is being force recorded\n    // TODO: change this to regex so it only reads from the beginning of the string\n    isTestForceRecord = this.currentTest.title.includes('[r]');\n    this.currentTest.title = isTestForceRecord ? this.currentTest.title.split('[r]')[1].trim() : this.currentTest.title;\n\n    // Load stubbed data from local JSON file\n    // Do not stub if...\n    // This test is being force recorded\n    // there are no mock data for this test\n    if (\n      !recordTests.includes(this.currentTest.title)\n      && !isTestForceRecord\n      && routesByTestId[this.currentTest.title]\n    ) {\n      // This is used to group routes by method type and url (e.g. { GET: { '/api/messages': {...} }})\n      const sortedRoutes = {};\n      supportedMethods.forEach((method) => {\n        sortedRoutes[method] = {};\n      });\n\n      // set the browser's Date to the timestamp at which this spec's endpoints were recorded.\n      cy.clock(routesByTestId[this.currentTest.title].timestamp, ['Date']);\n\n      routesByTestId[this.currentTest.title].routes.forEach((request) => {\n        if (!sortedRoutes[request.method][request.url]) {\n          sortedRoutes[request.method][request.url] = [];\n        }\n\n        sortedRoutes[request.method][request.url].push(request);\n      });\n\n      const createStubbedRoute = (method, url) => {\n        let index = 0;\n        const response = sortedRoutes[method][url][index];\n\n        cy.intercept(\n          {\n            url,\n            method,\n          },\n          (req) => {\n            req.reply((res) => {\n              const newResponse = sortedRoutes[method][url][index];\n              res.send(\n                newResponse.status,\n                newResponse.fixtureId\n                  ? {\n                      fixture: `${fixturesFolderSubDirectory}/${newResponse.fixtureId}.json`,\n                    }\n                  : newResponse.response,\n                newResponse.headers,\n              );\n\n              if (sortedRoutes[method][url].length > index + 1) {\n                index++;\n              }\n            });\n          },\n        );\n      };\n\n      // Stub all recorded routes\n      Object.keys(sortedRoutes).forEach((method) => {\n        Object.keys(sortedRoutes[method]).forEach((url) => createStubbedRoute(method, url));\n      });\n    } else {\n      // lock the browser's timestamp in place so that there is no variation with the\n      // timestamp REST APIs use as an argument due to undeterministic page load times\n      // which will cause varying timestamps.  `cy.clock` locks the timestamp.\n      timestamp = Date.now();\n      cy.clock(timestamp, ['Date']);\n    }\n\n    // Store test name if isCleanMocks is true\n    if (isCleanMocks) {\n      testNames.push(this.currentTest.title);\n    }\n\n    cy.clock().invoke('restore');\n  });\n\n  afterEach(function() {\n    // Check to see if the current test already has mock data or if forceRecord is on\n    if (\n      (!routesByTestId[this.currentTest.title]\n      || isTestForceRecord\n      || recordTests.includes(this.currentTest.title))\n      && !isCleanMocks\n    ) {\n      // Construct endpoint to be saved locally\n      const endpoints = routes.map((request) => {\n        // Check to see of mock data is too large for request header\n        const isFileOversized = sizeInMbytes(request.data) > maxInlineResponseSize;\n        let fixtureId;\n\n        // If the mock data is too large, store it in a separate json\n        if (isFileOversized) {\n          fixtureId = guidGenerator();\n          addFixture[path.join(fixturesFolder, fixturesFolderSubDirectory, `${fixtureId}.json`)] = request.data;\n        }\n\n        return {\n          fixtureId: fixtureId,\n          url: request.url,\n          method: request.method,\n          status: request.status,\n          headers: request.headers,\n          body: request.body,\n          response: isFileOversized ? undefined : request.data\n        };\n      });\n\n      // Delete fixtures if we are overwriting mock data\n      if (routesByTestId[this.currentTest.title]) {\n        routesByTestId[this.currentTest.title].routes.forEach((route) => {\n          // If fixtureId exist, delete the json\n          if (route.fixtureId) {\n            removeFixture.push(path.join(fixturesFolder, fixturesFolderSubDirectory, `${route.fixtureId}.json`));\n          }\n        });\n      }\n\n      // Store the endpoint for this test in the mock data object for this file if there are endpoints for this test\n      if (endpoints.length > 0) {\n        routesByTestId[this.currentTest.title] = {\n          // since REST APIs can pass a timestamp argument, we need to keep track\n          // of the time at which this spec was recorded so we can set the browser's Date\n          // to that specific time so that the endpoints can be properly stubbed as the\n          // the timestamp is part of many of the APIs' signature as well as POST body and uniquely identifies it.\n          timestamp,\n          routes: endpoints\n        };\n      }\n    }\n  });\n\n  after(function() {\n    // Transfer used mock data to new object to be stored locally\n    if (isCleanMocks) {\n      Object.keys(routesByTestId).forEach((testName) => {\n        if (testNames.includes(testName)) {\n          cleanMockData[testName] = routesByTestId[testName];\n        } else {\n          routesByTestId[testName].routes.forEach((route) => {\n            if (route.fixtureId) {\n              cy.task('deleteFile', path.join(fixturesFolder, fixturesFolderSubDirectory, `${route.fixtureId}.json`));\n            }\n          });\n        }\n      });\n    }\n\n    removeFixture.forEach((fixtureName) => cy.task('deleteFile', fixtureName));\n    cy.writeFile(path.join(mocksFolder, `${fileName}.json`), isCleanMocks ? cleanMockData : routesByTestId);\n    Object.keys(addFixture).forEach((fixtureName) => {\n      cy.writeFile(fixtureName, addFixture[fixtureName]);\n    });\n  });\n};\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"cypress-autorecord\",\n  \"version\": \"3.1.2\",\n  \"description\": \"It simplifies mocking by auto-recording/stubbing HTTP interactions and automate the process of updating/deleting recordings.\",\n  \"main\": \"index.js\",\n  \"types\": \"index.d.ts\",\n  \"scripts\": {\n    \"test\": \"cypress run --headless\",\n    \"test:watch\": \"cypress open\"\n  },\n  \"author\": \"Nancy Du\",\n  \"keywords\": [\n    \"Cypress\",\n    \"http\",\n    \"https\",\n    \"record\",\n    \"playback\",\n    \"mock\",\n    \"vcr\"\n  ],\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"https://github.com/Nanciee/cypress-autorecord\"\n  },\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"cypress\": \"6.1.0\"\n  }\n}\n"
  },
  {
    "path": "plugin.js",
    "content": "const path = require('path');\n\nmodule.exports = (on, config, fs) => {\n  // `on` is used to hook into various events Cypress emits\n  // `config` is the resolved Cypress config\n  const mocksFolder = path.resolve(config.fixturesFolder, '../mocks');\n\n  const readFile = (filePath) => {\n    if (fs.existsSync(filePath)) {\n      return JSON.parse(fs.readFileSync(filePath, 'utf8'));\n    }\n\n    return null;\n  };\n\n  const deleteFile = (filePath) => {\n    if (fs.existsSync(filePath)) {\n      fs.unlinkSync(filePath);\n      return true;\n    }\n\n    return null;\n  };\n\n  const deleteFolder = (directoryPath) => {\n    if (fs.existsSync(directoryPath)) {\n      fs.readdirSync(directoryPath).forEach((file, index) => {\n        const curPath = path.join(directoryPath, file);\n        if (fs.lstatSync(curPath).isDirectory()) {\n          // recurse\n          deleteFolder(curPath);\n        } else {\n          // delete file\n          fs.unlinkSync(curPath);\n        }\n      });\n      fs.rmdirSync(directoryPath);\n    }\n  }\n\n  const cleanMocks = () => {\n    // TODO: create error handling\n    const specFiles = fs.readdirSync(config.integrationFolder);\n    const mockFiles = fs.readdirSync(mocksFolder);\n    mockFiles.forEach((mockName) => {\n      const isMockUsed = specFiles.find((specName) => specName.split('.')[0] === mockName.split('.')[0]);\n      if (!isMockUsed) {\n        const mockData = readFile(path.join(mocksFolder, mockName));\n        Object.keys(mockData).forEach((testName) => {\n          mockData[testName].forEach((route) => {\n            if (route.fixtureId) {\n              deleteFile(path.join(config.fixturesFolder, `${route.fixtureId}.json`));\n            }\n          });\n        });\n\n        deleteFile(path.join(mocksFolder, mockName));\n      }\n    });\n\n    return null;\n  };\n\n  const removeAllMocks = () => {\n    \n    if (fs.existsSync(config.fixturesFolder)) {\n      const fixtureFiles = fs.readdirSync(config.fixturesFolder);\n      fixtureFiles.forEach((fileName) => {\n        const file = path.join(config.fixturesFolder, fileName);\n        if (fs.lstatSync(file).isDirectory()) {\n          deleteFolder(file);\n        } else {\n          deleteFile(file);\n        }\n      });\n    }\n\n    if (fs.existsSync(mocksFolder)) {\n      const mockFiles = fs.readdirSync(mocksFolder);\n      mockFiles.forEach((fileName) => {\n        deleteFile(path.join(mocksFolder, fileName));\n      });\n    }\n\n    return null;\n  };\n\n  on('task', {\n    readFile,\n    deleteFile,\n    cleanMocks,\n    removeAllMocks\n  });\n};\n"
  },
  {
    "path": "util.js",
    "content": "const sizeInMbytes = (obj) => {\n  let bytes = 0;\n\n  const sizeOf = (obj) => {\n    let objClass;\n    if (obj !== null && obj !== undefined) {\n      switch (typeof obj) {\n        case 'number':\n          bytes += 8;\n          break;\n        case 'string':\n          bytes += obj.length * 2;\n          break;\n        case 'boolean':\n          bytes += 4;\n          break;\n        case 'object':\n          objClass = Object.prototype.toString.call(obj).slice(8, -1);\n          if (objClass === 'Object' || objClass === 'Array') {\n            for (const key in obj) {\n              if (!obj.hasOwnProperty(key)) continue;\n              sizeOf(obj[key]);\n            }\n          } else bytes += obj.toString().length * 2;\n          break;\n      }\n    }\n    return bytes;\n  };\n\n  return sizeOf(obj) / 1024;\n};\n\nconst guidGenerator = () => {\n  const s4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);\n  return (s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4());\n};\n\nconst blobToPlain = (blob) => {\n  let uri = URL.createObjectURL(blob);\n  let xhr = new XMLHttpRequest();\n\n  xhr.open('GET', uri, false);\n  xhr.send();\n\n  URL.revokeObjectURL(uri);\n\n  return blob.type === 'application/json'\n    ? JSON.parse(xhr.response)\n    : xhr.response;\n}\n\nmodule.exports = {\n  sizeInMbytes,\n  guidGenerator,\n  blobToPlain\n};\n"
  }
]