Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cypress Plugin #1580

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/plugin-cypress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<div align="center">

<!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
<h1>@kubb/plugin-cypress</h1>

<p>
Swagger integration to create Cypress requests commands.
</p>
<img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />

[![npm version][npm-version-src]][npm-version-href]
[![npm downloads][npm-downloads-src]][npm-downloads-href]
[![Coverage][coverage-src]][coverage-href]
[![License][license-src]][license-href]

<h4>
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/main//examples/typescript" target="_blank">View Demo</a>
<span> · </span>
<a href="https://kubb.dev/" target="_blank">Documentation</a>
<span> · </span>
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
<span> · </span>
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
</h4>
</div>

## Supporting Kubb

Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:

- [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)

<p align="center">
<a href="https://github.com/sponsors/stijnvanhulle">
<img src="https://raw.githubusercontent.com/stijnvanhulle/sponsors/main/sponsors.svg" alt="My sponsors" />
</a>
</p>


<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-msw?flat&colorA=18181B&colorB=f58517
[npm-version-href]: https://npmjs.com/package/@kubb/plugin-msw
[npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/plugin-msw?flat&colorA=18181B&colorB=f58517
[npm-downloads-href]: https://npmjs.com/package/@kubb/plugin-msw
[license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
[license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
[build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
[build-href]: https://www.npmjs.com/package/@kubb/plugin-msw
[minified-src]: https://img.shields.io/bundlephobia/min/@kubb/plugin-msw?style=flat&colorA=18181B&colorB=f58517
[minified-href]: https://www.npmjs.com/package/@kubb/plugin-msw
[coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
[coverage-href]: https://www.npmjs.com/package/@kubb/plugin-msw
130 changes: 130 additions & 0 deletions packages/plugin-cypress/mocks/petStore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Pets'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
summary: Create a pet
operationId: createPets
tags:
- pets
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- 'name'
- 'tag'
properties:
name:
type: string
tag:
type: string
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
- name: testId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Pets:
type: array
items:
$ref: '#/components/schemas/Pet'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
114 changes: 114 additions & 0 deletions packages/plugin-cypress/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"name": "@kubb/plugin-cypress",
"version": "3.6.0",
"description": "Generator swagger",
"keywords": [
"typescript",
"plugins",
"kubb",
"codegen",
"swagger",
"openapi"
],
"repository": {
"type": "git",
"url": "git://github.com/kubb-labs/kubb.git",
"directory": "packages/plugin-cypress"
},
"license": "MIT",
"author": "Stijn Van Hulle <[email protected]",
"sideEffects": false,
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs",
"default": "./dist/index.cjs"
},
"./utils": {
"import": "./dist/utils.js",
"require": "./dist/utils.cjs",
"default": "./dist/utils.cjs"
},
"./hooks": {
"import": "./dist/hooks.js",
"require": "./dist/hooks.cjs",
"default": "./dist/hooks.cjs"
},
"./components": {
"import": "./dist/components.js",
"require": "./dist/components.cjs",
"default": "./dist/components.cjs"
},
"./generators": {
"import": "./dist/generators.js",
"require": "./dist/generators.cjs",
"default": "./dist/generators.cjs"
},
"./package.json": "./package.json",
"./*": "./*"
},
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"utils": [
"./dist/utils.d.ts"
],
"hooks": [
"./dist/hooks.d.ts"
],
"components": [
"./dist/components.d.ts"
],
"generators": [
"./dist/generators.d.ts"
]
}
},
"files": [
"src",
"dist",
"!/**/**.test.**",
"!/**/__tests__/**"
],
"scripts": {
"build": "tsup",
"clean": "npx rimraf ./dist",
"lint": "bun biome lint .",
"lint:fix": "bun biome lint --apply-unsafe .",
"release": "pnpm publish --no-git-check",
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
"start": "tsup --watch",
"test": "vitest --passWithNoTests",
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
},
"dependencies": {
"@kubb/core": "workspace:*",
"@kubb/fs": "workspace:*",
"@kubb/oas": "workspace:*",
"@kubb/plugin-faker": "workspace:*",
"@kubb/plugin-oas": "workspace:*",
"@kubb/plugin-ts": "workspace:*",
"@kubb/react": "workspace:*"
},
"devDependencies": {
"@kubb/config-ts": "workspace:*",
"@kubb/config-tsup": "workspace:*",
"@types/react": "catalog:",
"react": "catalog:",
"tsup": "catalog:",
"typescript": "catalog:"
},
"peerDependencies": {
"@kubb/react": "^3.0.0"
},
"engines": {
"node": ">=20"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
}
}
19 changes: 19 additions & 0 deletions packages/plugin-cypress/src/components/Handlers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { File } from '@kubb/react'
import type { ReactNode } from 'react'

type HandlersProps = {
/**
* Name of the function
*/
name: string
// custom
handlers: string[]
}

export function Handlers({ name, handlers }: HandlersProps): ReactNode {
return (
<File.Source name={name} isIndexable isExportable>
{`export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, '')} as const`}
</File.Source>
)
}
34 changes: 34 additions & 0 deletions packages/plugin-cypress/src/components/Mock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { File, Function, FunctionParams } from '@kubb/react'

import type { HttpMethod } from '@kubb/oas'
import type { ReactNode } from 'react'
import { URLPath } from '@kubb/core/utils'

type Props = {
/**
* Name of the function
*/
name: string
typeName: string
fakerName: string
url: string
baseURL: string | undefined
method: HttpMethod
}

export function Mock({ baseURL = '', name, typeName, url, method }: Props): ReactNode {
const params = FunctionParams.factory({
data: {
type: typeName,
optional: true,
},
})

return (
<File.Source name={name} isIndexable isExportable>
<Function name={name} export params={params.toConstructor()} returnType={`Chainable<${typeName}>`}>
{`return cy.request('${method}', '${new URLPath(`${baseURL ?? ''}${url}`).toURLPath()}', data || undefined)`}
</Function>
</File.Source>
)
}
34 changes: 34 additions & 0 deletions packages/plugin-cypress/src/components/MockWithFaker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { File, Function, FunctionParams } from '@kubb/react'

import type { HttpMethod } from '@kubb/oas'
import type { ReactNode } from 'react'
import { URLPath } from '@kubb/core/utils'

type Props = {
/**
* Name of the function
*/
name: string
typeName: string
fakerName: string
baseURL: string | undefined
url: string
method: HttpMethod
}

export function MockWithFaker({ baseURL = '', name, fakerName, typeName, url, method }: Props): ReactNode {
const params = FunctionParams.factory({
data: {
type: typeName,
optional: true,
},
})

return (
<File.Source name={name} isIndexable isExportable>
<Function name={name} export params={params.toConstructor()} returnType={`Chainable<${typeName}>`}>
{`return cy.request('${method}', '${new URLPath(`${baseURL ?? ''}${url}`).toURLPath()}', data || undefined)`}
</Function>
</File.Source>
)
}
3 changes: 3 additions & 0 deletions packages/plugin-cypress/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { Mock } from './Mock.tsx'
export { Handlers } from './Handlers.tsx'
export { MockWithFaker } from './MockWithFaker.tsx'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type CreatePetsMutationResponse from 'createPet.ts'

export function createPets(data?: CreatePetsMutationResponse): Chainable<CreatePetsMutationResponse> {
return cy.request('post', '/pets', data || undefined)
}
Loading
Loading