{"id":18463846,"url":"https://github.com/smartive/testcafe-utils","last_synced_at":"2025-04-08T07:32:48.299Z","repository":{"id":55645079,"uuid":"321680161","full_name":"smartive/testcafe-utils","owner":"smartive","description":"This package contains a FetchInterceptor class and some other helper functions that we at smartive often need when working with TestCafe.","archived":false,"fork":false,"pushed_at":"2025-03-11T16:42:51.000Z","size":483,"stargazers_count":0,"open_issues_count":10,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-05T18:12:35.605Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/smartive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2020-12-15T13:43:17.000Z","updated_at":"2025-02-20T02:27:00.000Z","dependencies_parsed_at":"2024-08-27T02:41:11.464Z","dependency_job_id":"dcd49fd1-77e4-470e-890b-80c21040ff02","html_url":"https://github.com/smartive/testcafe-utils","commit_stats":{"total_commits":104,"total_committers":3,"mean_commits":"34.666666666666664","dds":"0.13461538461538458","last_synced_commit":"6508b095151c63707aedb649c94e145ec99c6187"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Ftestcafe-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Ftestcafe-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Ftestcafe-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Ftestcafe-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smartive","download_url":"https://codeload.github.com/smartive/testcafe-utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247796344,"owners_count":20997553,"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":[],"created_at":"2024-11-06T09:08:08.558Z","updated_at":"2025-04-08T07:32:48.010Z","avatar_url":"https://github.com/smartive.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @smartive/testcafe-utils\n\nThis package is a toolbox which contains various helpers we often use when working with TestCafe.\n\n## `FakeTimers`\n\nThe `FakeTimers` class is a TestCafe adaption of (@sinonjs/fake-timers)[https://github.com/sinonjs/fake-timers] and helps to mock/intercept time related functionality.\n\nFrom their website:\n\n\u003e `@sinonjs/fake-timers` can be used to simulate passing time in automated tests and other situations where you want the scheduling semantics, but don't want to actually wait.\n\n### Example\n\n```Typescript\n// The following snippet does not include all needed imports and code it is intended\n// to give you a starting point and an idea how this class can be used.\n\nimport { FakeTimers } from '@smartive/testcafe-utils';\n\nfixture('My Fixture').page`http://localhost:3000`;\n\nconst mockClock = new FakeTimers({ toFake: ['setTimeout'] });\n\ntest.clientScripts([\n  // IMPORTANT!\n  // This ensures the mock is loaded before your app under test.\n  mockClock.clientScript(),\n])('My Test', (t) =\u003e\n  t\n    .click(page.loadPeople)\n    .expect(page.spinner.exists)\n    .ok()\n\n    // It is also possible to `await mockClock.execute({ t, method: 'next', methodArgs: [] });`\n    // instead of chaining the command.\n    .expect(mockClock.execute({ t, method: 'next', methodArgs: [] }))\n\n    .ok()\n    .expect(page.list.childElementCount)\n    .gt(0)\n    .click(page.loadMorePeople)\n    .expect(page.spinner.exists)\n    .ok()\n);\n```\n\n## `FetchInterceptor` or `XHRInterceptor`\n\nThe `FetchInterceptor` class can be used to intercept the `window.fetch`-calls within the browser and the `XHRInterceptor` does the same for `XMLHttpRequest`. Both classes take a key/value object as constructor argument, where the values are `string`, `RegExp` or `{ method: HTTPMethod, pattern: RegExp | string }` to intercept an URL. Afterwards it is possible to resolve a running `fetch` call at any desired time with `await fetchInterceptor.resolve('interceptURLKey')({ t })`. (where `t` is the TestCafe `TestController`)\n\n### Example\n\nThe example only shows the `FetchInterceptor` because the `XHRInterceptor` is used the same way.\n\n```Typescript\n// The following snippet does not include all needed imports and code it is intended\n// to give you a starting point and an idea how this class can be used.\n\nimport { FetchInterceptor } from '@smartive/testcafe-utils';\n\nfixture('My Fixture').page`http://localhost:3000`;\n\nconst fetchInterceptor = new FetchInterceptor({\n  fetchLuke: 'https://swapi.dev/api/people/1/',\n  fetchPeople: /.+swapi\\.dev.+\\/people\\/$/,\n  fetchMore: { method: 'GET', pattern: /.+swapi\\.dev.+\\/people\\/\\?page=.+/ },\n});\n\ntest.clientScripts([\n  // IMPORTANT!\n  // This ensures the mock is loaded before your app under test.\n  fetchInterceptor.clientScript(),\n])('My Test', async (t) =\u003e {\n  await t.click(page.loadPeople).expect(page.spinner.exists).ok();\n\n  // It is also possible to \"inline/chain\" `fetchInterceptor.resolve` like\n  // `.expect(fetchInterceptor.resolve('fetchPeople')({ t })).ok()`\n  await fetchInterceptor.resolve('fetchPeople')({ t });\n\n  await t\n    .expect(page.list.childElementCount)\n    .gt(0)\n    .click(page.loadMorePeople)\n    .expect(page.spinner.exists)\n    .ok();\n  await fetchInterceptor.resolve('fetchMore')({ t });\n  await t\n    .expect(page.list.childElementCount)\n    .gt(10)\n    .click(page.loadLuke)\n    .expect(page.spinner.exists)\n    .ok();\n  await fetchInterceptor.resolve('fetchLuke')({ t });\n  await t.expect(detailPage.title.textContent).eql('Luke Skywalker');\n});\n```\n\n## `mockHeaders`\n\nThe `mockHeaders` object can be used to fix the cross origin problem which often occurs because TestCafe does not set an often desired `access-control-allow-credentials` and `access-control-allow-origin` header property.\n\n### Example\n\n```Typescript\nimport { mockHeaders } from '@smartive/testcafe-utils';\nimport { RequestMock } from 'testcafe';\n\nconst mockCrossOriginRequest = RequestMock()\n  .onRequestTo(({ method, url }) =\u003e method === 'post' \u0026\u0026 RegExp(/\\/people\\/$/).test(url))\n  .respond(null, 200, mockHeaders);\n\n```\n\n## `mockResponse`\n\nThe `mockResponse` function can be used to simplify the creation of mock responses. Additionally it adds a new header field `x-testcafe-mock` to make a mocked request better traceable.\n\n### Example\n\n```Typescript\nimport { mockResponse } from '@smartive/testcafe-utils';\nimport { RequestMock } from 'testcafe';\n\nconst PeopleRequestMock = RequestMock()\n  .onRequestTo(/.+swapi\\.dev.+\\/people\\/$/)\n  .respond(\n    mockResponse(\n      {}, // response data\n      400, // statusCode (default: 200)\n      'PUT', // method (default: 'GET') somehow TestCafe needs this on response object ¯\\_(ツ)_/¯\n      { 'my-foobar-header': 42 } // additionalHeaders (default: {})\n    )\n  );\n```\n\n## `RequestCallCountMock`\n\nThe `RequestCallCountMock` class can be used to add a `RequestMock` to a TestCafe `fixture` or `test` which returns the given responses according to the call count. An extra header `'x-testcafe-call-count'` will be added to the response to make a mocked request better traceable.\n\n### Example\n\n```Typescript\nimport { mockResponse, RequestCallCountMock } from '@smartive/testcafe-utils';\n\ntest.requestHooks([\n  new RequestCallCountMock(/.+swapi\\.dev.+\\/people\\/$/, [\n    { body: mockResponse(['a', 'b', 'c']) }, // On the first api call to `/.+swapi\\.dev.+\\/people\\/$/` the response is `['a', 'b', 'c']`\n    { body: mockResponse([1, 2, 3]) }, // on the second call the response is `[1,2,3]`\n    { body: mockResponse({}, 400) }, // on the third and all following calls the repsonse is a `statusCode` 400 and `{}`\n  ]),\n]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmartive%2Ftestcafe-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmartive%2Ftestcafe-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmartive%2Ftestcafe-utils/lists"}