{"id":18463840,"url":"https://github.com/smartive/xstate-test-toolbox","last_synced_at":"2025-04-08T07:32:47.839Z","repository":{"id":55645062,"uuid":"321731125","full_name":"smartive/xstate-test-toolbox","owner":"smartive","description":"This package contains the helper createTestPlans which can be used with `xstate` and `@xstate/test`.","archived":false,"fork":false,"pushed_at":"2025-04-03T02:44:50.000Z","size":631,"stargazers_count":13,"open_issues_count":11,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-05T18:12:35.624Z","etag":null,"topics":["guards","testing","tests","xstate"],"latest_commit_sha":null,"homepage":"https://blog.smartive.ch/3-problems-we-encountered-using-xstate-test-with-testcafe-and-their-solutions-492c237b5f85","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-15T16:52:39.000Z","updated_at":"2025-04-03T02:43:19.000Z","dependencies_parsed_at":"2024-01-13T16:25:40.062Z","dependency_job_id":"81f46c5f-c96a-43bd-86be-a34f3524e8f1","html_url":"https://github.com/smartive/xstate-test-toolbox","commit_stats":{"total_commits":226,"total_committers":3,"mean_commits":75.33333333333333,"dds":0.04424778761061943,"last_synced_commit":"a62f8edb25dd69e8361e8d23ca3148a838fa529a"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Fxstate-test-toolbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Fxstate-test-toolbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Fxstate-test-toolbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smartive%2Fxstate-test-toolbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smartive","download_url":"https://codeload.github.com/smartive/xstate-test-toolbox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247796343,"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":["guards","testing","tests","xstate"],"created_at":"2024-11-06T09:08:07.648Z","updated_at":"2025-04-08T07:32:47.483Z","avatar_url":"https://github.com/smartive.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @smartive/xstate-test-toolbox\n\nThis package contains the helper `createTestPlans` which can be used with xstate and @xstate/test.\n\n## `createTestPlans`\n\nThis function adds the `meta`-property to every state and a test if it is defined within `tests`. (see in example) Beside that it generates all possible test simple path plans for all possible combinations of your guards.\n\n⚠️ Attention: Your statechart must consist of only string references to guards, actions and services otherwise the testing will break.\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 the `createTestPlans`-function can be used.\n\nimport { createTestPlans, StatesTestFunctions } from '@smartive/xstate-test-toolbox';\nimport { FetchInterceptor, mockHeaders, mockResponse, RequestCallCountMock } from '@smartive/testcafe-utils';\nimport { TestEventsConfig } from '@xstate/test/lib/types';\nimport { RequestMock } from 'testcafe';\nimport { Context, machine } from './machine-under-test';\n// ...\n\ntype TestContext = {\n  t: TestController,\n  plan: string,\n  path: string\n};\n\nconst fetchInterceptor = new FetchInterceptor({\n  fetchPeople: /.+swapi\\.dev.+\\/people\\/$/,\n  fetchMore: /.+swapi\\.dev.+\\/people\\/\\?page=.+/,\n  searchPeople: /.+swapi\\.dev.+\\/people\\/\\?search=.+/,\n});\n\nconst getRequestMocks = (plan: string, path: string): object[] =\u003e {\n  const peopleUrl = /.+swapi\\.dev.+\\/people\\/.*/\n  if (\n    plan.includes('NoResults') ||\n    (plan.includes('Searching') \u0026\u0026 path.includes('NoResults'))\n  ) {\n    return [\n      RequestMock()\n        .onRequestTo(peopleUrl)\n        .respond(empty, 200, mockHeaders),\n    ];\n  }\n\n  if (plan.includes('Error')) {\n    switch (path) {\n      case 'Pending → error.platform.fetchPeople':\n        return [\n          RequestMock().onRequestTo(peopleUrl).respond({}, 400, mockHeaders),\n        ];\n      case 'Pending → done.invoke.fetchPeople → Idle → END_REACHED → LoadingMore → error.platform.fetchMore':\n        return [\n          new RequestCallCountMock(peopleUrl, [\n            { body: mockResponse(peoples) },\n            { body: mockResponse({}, 400) },\n          ]),\n        ];\n      case 'Pending → done.invoke.fetchPeople → NoResults → QUERY_DISPATCHED → Searching → error.platform.searchPeople':\n        return [\n          RequestMock()\n            .onRequestTo(fetchInterceptor.interceptUrls.searchPeople)\n            .respond({}, 400, mockHeaders),\n          RequestMock()\n            .onRequestTo(peopleUrl)\n            .respond(empty, 200, mockHeaders),\n        ];\n      case 'Pending → done.invoke.fetchPeople → Idle → QUERY_DISPATCHED → Searching → error.platform.searchPeople':\n        return [\n          RequestMock()\n            .onRequestTo(fetchInterceptor.interceptUrls.searchPeople)\n            .respond({}, 400, mockHeaders),\n        ];\n    }\n  }\n\n  return [];\n};\n\nconst tests: StatesTestFunctions\u003cContext, TestContext\u003e = {\n  Pending: ({ t }) =\u003e t.expect(page.spinner.exists).ok(),\n  Idle: ({ t }) =\u003e t.expect(page.listItem.count).gt(0),\n  LoadingMore: ({ t }) =\u003e t.expect(page.listItem.count).gt(1).expect(page.spinner.exists).ok(),\n  Error: ({ t }) =\u003e t.expect(page.error.exists).ok(),\n  NoResults: ({ t }) =\u003e t.expect(page.notify.exists).ok(),\n  Searching: ({ t }) =\u003e t.expect(page.search.value).contains('luke').expect(page.spinner.exists).ok(),\n};\n\nconst testEvents: TestEventsConfig\u003cTestContext\u003e = {\n  END_REACHED: ({ t }) =\u003e t.hover(page.listItem.nth(9), { speed: 0.8 }),\n  QUERY_DISPATCHED: ({ t }) =\u003e t.typeText(page.search, 'luke', { speed: 0.8 }),\n  'done.invoke.fetchPeople': fetchInterceptor.resolve('fetchPeople'),\n  'error.platform.fetchPeople': fetchInterceptor.resolve('fetchPeople'),\n  'error.platform.fetchMore': fetchInterceptor.resolve('fetchMore'),\n  'error.platform.searchPeople': fetchInterceptor.resolve('searchPeople'),\n};\n\ncreateTestPlans({\n  machine,\n  tests,\n  testEvents,\n  // add logLevel: LogLevel.INFO for some output which plans/paths are generated\n}).forEach(\n  ({ description: plan, paths }) =\u003e {\n    fixture(plan).page(`http://localhost:3000/peoples`);\n\n    paths.forEach(({ test: run, description: path }) =\u003e {\n      test\n        .clientScripts([fetchInterceptor.clientScript()])\n        .requestHooks(getRequestMocks(plan, path))(`via ${path} ⬏`, (t) =\u003e run({ plan, path, t }));\n    });\n  }\n);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmartive%2Fxstate-test-toolbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmartive%2Fxstate-test-toolbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmartive%2Fxstate-test-toolbox/lists"}