Skip to content

Commit

Permalink
fix: remove cypress.server.defaults, cy.server and cy.route (#24411)
Browse files Browse the repository at this point in the history
* fix: remove server and route commands

* This won't totally pass but it's a step in the right direction.

* Should be down to only two failing tests due to abort.

* fix the alias.<thing> api for intercept to be equivalent to route.

* adjust timeouts to reduce flake, ideally

* skip xhr abort tests

* More clean up

* Increase timeout

* trick next version

* fix unit tests probably

* yeah sure

* Apply suggestions from code review

Co-authored-by: Blue F <blue@cypress.io>

* remove addXhrLog

* Remove more usage of XHR stemming from proxy-logging

* Remove a couple of files that found their way back with the merge from release/12.0.0

* remove more server types

* increase timeouts again to prevent flake

Co-authored-by: Blue F <blue@cypress.io>
Co-authored-by: Zach Bloomquist <git@chary.us>
  • Loading branch information
3 people committed Nov 4, 2022
1 parent 954be79 commit 2f18a8c
Show file tree
Hide file tree
Showing 55 changed files with 367 additions and 5,546 deletions.
133 changes: 0 additions & 133 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,6 @@ declare namespace Cypress {
defaults(options: Partial<KeyboardDefaultsOptions>): void
}

/**
* @see https://on.cypress.io/api/api-server
*/
Server: {
defaults(options: Partial<ServerOptions>): void
}

/**
* @see https://on.cypress.io/screenshot-api
*/
Expand Down Expand Up @@ -1665,66 +1658,6 @@ declare namespace Cypress {
*/
root<E extends Node = HTMLHtmlElement>(options?: Partial<Loggable>): Chainable<JQuery<E>> // can't do better typing unless we ignore the `.within()` case

/**
* @deprecated Use `cy.intercept()` instead.
*
* Use `cy.route()` to manage the behavior of network requests.
* @see https://on.cypress.io/route
* @example
* cy.server()
* cy.route('https://localhost:7777/users', [{id: 1, name: 'Pat'}])
*/
route(url: string | RegExp, response?: string | object): Chainable<null>
/**
* @deprecated Use `cy.intercept()` instead.
*
* Spy or stub request with specific method and url.
*
* @see https://on.cypress.io/route
* @example
* cy.server()
* // spy on POST /todos requests
* cy.route('POST', '/todos').as('add-todo')
*/
route(method: string, url: string | RegExp, response?: string | object): Chainable<null>
/**
* @deprecated Use `cy.intercept()` instead.
*
* Set a route by returning an object literal from a callback function.
* Functions that return a Promise will automatically be awaited.
*
* @see https://on.cypress.io/route
* @example
* cy.server()
* cy.route(() => {
* // your logic here
* // return an appropriate routing object here
* return {
* method: 'POST',
* url: '/comments',
* response: this.commentsFixture
* }
* })
*/
route(fn: () => RouteOptions): Chainable<null>
/**
* @deprecated Use `cy.intercept()` instead.
*
* Spy or stub a given route.
*
* @see https://on.cypress.io/route
* @example
* cy.server()
* cy.route({
* method: 'DELETE',
* url: '/users',
* status: 412,
* delay: 1000
* // and other options, see documentation
* })
*/
route(options: Partial<RouteOptions>): Chainable<null>

/**
* Take a screenshot of the application under test and the Cypress Command Log.
*
Expand Down Expand Up @@ -1770,26 +1703,6 @@ declare namespace Cypress {
*/
select(valueOrTextOrIndex: string | number | Array<string | number>, options?: Partial<SelectOptions>): Chainable<Subject>

/**
* @deprecated Use `cy.intercept()` instead.
*
* Start a server to begin routing responses to `cy.route()` and `cy.request()`.
*
* @example
* // start server
* cy.server()
* // get default server options
* cy.server().should((server) => {
* expect(server.delay).to.eq(0)
* expect(server.method).to.eq('GET')
* expect(server.status).to.eq(200)
* // and many others options
* })
*
* @see https://on.cypress.io/server
*/
server(options?: Partial<ServerOptions>): Chainable<ServerOptions>

/**
* Set a browser cookie.
*
Expand Down Expand Up @@ -3040,8 +2953,6 @@ declare namespace Cypress {
socketIoRoute: string
spec: Cypress['spec'] | null
specs: Array<Cypress['spec']>
xhrRoute: string
xhrUrl: string
}

interface TestConfigOverrides extends Partial<Pick<ConfigOptions, 'animationDistanceThreshold' | 'blockHosts' | 'defaultCommandTimeout' | 'env' | 'execTimeout' | 'includeShadowDom' | 'numTestsKeptInMemory' | 'pageLoadTimeout' | 'redirectionLimit' | 'requestTimeout' | 'responseTimeout' | 'retries' | 'screenshotOnRunFailure' | 'slowTestThreshold' | 'scrollBehavior' | 'taskTimeout' | 'viewportHeight' | 'viewportWidth' | 'waitForAnimations'>>, Partial<Pick<ResolvedConfigOptions, 'baseUrl'>> {
Expand Down Expand Up @@ -3289,28 +3200,6 @@ declare namespace Cypress {
interval: number
}

/**
* Setting default options for cy.server()
* @see https://on.cypress.io/server
*/
interface ServerOptions {
delay: number
method: HttpMethod
status: number
headers: object
response: any
onRequest(...args: any[]): void
onResponse(...args: any[]): void
onAbort(...args: any[]): void
enable: boolean
force404: boolean
urlMatchingOptions: object
ignore(xhr: Request): void
onAnyRequest(route: RouteOptions, proxy: any): void
onAnyResponse(route: RouteOptions, proxy: any): void
onAnyAbort(route: RouteOptions, proxy: any): void
}

interface Session {
// Clear all saved sessions and re-run the current spec file.
clearAllSavedSessions: () => Promise<void>
Expand Down Expand Up @@ -5947,28 +5836,6 @@ declare namespace Cypress {
viewportHeight: number
}

interface WaitXHR {
duration: number
id: string
method: HttpMethod
request: {
body: string | ObjectLike
headers: ObjectLike
}
requestBody: WaitXHR['request']['body']
requestHeaders: WaitXHR['request']['headers']
response: {
body: string | ObjectLike
headers: ObjectLike
}
responseBody: WaitXHR['response']['body']
responseHeaders: WaitXHR['response']['headers']
status: number
statusMessage: string
url: string
xhr: XMLHttpRequest
}

type Encodings = 'ascii' | 'base64' | 'binary' | 'hex' | 'latin1' | 'utf8' | 'utf-8' | 'ucs2' | 'ucs-2' | 'utf16le' | 'utf-16le' | null
type PositionType = 'topLeft' | 'top' | 'topRight' | 'left' | 'center' | 'right' | 'bottomLeft' | 'bottom' | 'bottomRight'
type ViewportPreset = 'macbook-16' | 'macbook-15' | 'macbook-13' | 'macbook-11' | 'ipad-2' | 'ipad-mini' | 'iphone-xr' | 'iphone-x' | 'iphone-6+' | 'iphone-se2' | 'iphone-8' | 'iphone-7' | 'iphone-6' | 'iphone-5' | 'iphone-4' | 'iphone-3' | 'samsung-s10' | 'samsung-note9'
Expand Down
15 changes: 0 additions & 15 deletions cli/types/tests/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,13 @@ result // $ExpectType boolean

Cypress.minimatch('/users/1/comments', '/users/*/comments') // $ExpectType boolean

// check if cy.server() yields default server options
cy.server().should((server) => {
server // $ExpectType ServerOptions
expect(server.delay).to.eq(0)
expect(server.method).to.eq('GET')
expect(server.status).to.eq(200)
})

cy.visit('https://www.acme.com/', {
auth: {
username: 'wile',
password: 'coyote'
}
})

const serverOptions: Partial<Cypress.ServerOptions> = {
delay: 100,
ignore: () => true
}

cy.server(serverOptions)

Cypress.spec.name // $ExpectType string
Cypress.spec.relative // $ExpectType string
Cypress.spec.absolute // $ExpectType string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ You can install and bring [Testing library/Cypress](https://testing-library.com/

```js
it('loads and displays greeting (testing-lib)', () => {
cy.server()
cy.route('/greeting', { greeting: 'Hello there' }).as('greet')
cy.intercept('/greeting', { greeting: 'Hello there' }).as('greet')

const url = '/greeting'
mount(<Fetch url={url} />)
Expand All @@ -19,7 +18,7 @@ it('loads and displays greeting (testing-lib)', () => {
cy.findByRole('heading').should('have.text', 'Hello there')
cy.findByRole('button').should('be.disabled')
cy.get('@greet')
.its('url')
.its('response.url')
.should('match', /\/greeting$/)
})
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { mount } from '@cypress/react'
import Fetcher from './fetcher'

it('loads and displays greeting', () => {
cy.server()
cy.route('/greeting', { greeting: 'Hello there' }).as('greet')
cy.intercept('/greeting', { greeting: 'Hello there' }).as('greet')

const url = '/greeting'

Expand All @@ -14,6 +13,6 @@ it('loads and displays greeting', () => {
cy.get('[role=heading]').should('have.text', 'Hello there')
cy.get('[role=button]').should('be.disabled')
cy.get('@greet')
.its('url')
.its('response.url')
.should('match', /\/greeting$/)
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import Fetcher from './fetcher'

// NOTE: this doesn't work because of update to the @testing/library v7. Looks like build issue with current webpack config
it.skip('loads and displays greeting (testing-lib)', () => {
cy.server()
cy.route('/greeting', { greeting: 'Hello there' }).as('greet')
cy.intercept('/greeting', { greeting: 'Hello there' }).as('greet')

const url = '/greeting'

Expand All @@ -21,6 +20,6 @@ it.skip('loads and displays greeting (testing-lib)', () => {
cy.findByRole('heading').should('have.text', 'Hello there')
cy.findByRole('button').should('be.disabled')
cy.get('@greet')
.its('url')
.its('response.url')
.should('match', /\/greeting$/)
})
18 changes: 5 additions & 13 deletions npm/react/cypress/component/basic/network/1-users.cy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@ context('Users', () => {
})

describe('Network State', () => {
beforeEach(() => {
cy.server()
// mount the component after defining routes in tests
// preventing race conditions where you wait on untouched routes
})

it('can inspect real data in XHR', () => {
cy.route('/users?_limit=3').as('users')
cy.intercept('/users?_limit=3').as('users')
mount(<Users />)
cy.wait('@users')
.its('response.body')
Expand All @@ -34,7 +28,7 @@ context('Users', () => {
it('can display mock XHR response', () => {
const users = [{ id: 1, name: 'foo' }]

cy.route('GET', '/users?_limit=3', users).as('users')
cy.intercept('GET', '/users?_limit=3', users).as('users')
mount(<Users />)
cy.get('li')
.should('have.length', 1)
Expand All @@ -45,7 +39,7 @@ context('Users', () => {
it('can inspect mocked XHR', () => {
const users = [{ id: 1, name: 'foo' }]

cy.route('GET', '/users?_limit=3', users).as('users')
cy.intercept('GET', '/users?_limit=3', users).as('users')
mount(<Users />)
cy.wait('@users')
.its('response.body')
Expand All @@ -55,10 +49,8 @@ context('Users', () => {
it('can delay and wait on XHR', () => {
const users = [{ id: 1, name: 'foo' }]

cy.route({
method: 'GET',
url: '/users?_limit=3',
response: users,
cy.intercept('GET', '/users?_limit=3', {
body: users,
delay: 1000,
}).as('users')

Expand Down
18 changes: 5 additions & 13 deletions npm/react/cypress/component/basic/network/2-users-fetch.cy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ describe('Users with Fetch', () => {

// https://github.com/bahmutov/@cypress/react/issues/347
context('mocking', () => {
beforeEach(() => {
cy.server()
// mount the component after defining routes in tests
// preventing race conditions where you wait on untouched routes
})

it('can inspect real data from the server', () => {
// spy on the request
cy.route('/users?_limit=3').as('users')
cy.intercept('/users?_limit=3').as('users')
mount(<Users />)
cy.wait('@users')
.its('response.body')
Expand All @@ -34,7 +28,7 @@ describe('Users with Fetch', () => {
const users = [{ id: 1, name: 'foo' }]

// stub the request
cy.route('GET', '/users?_limit=3', users).as('users')
cy.intercept('GET', '/users?_limit=3', users).as('users')
mount(<Users />)
cy.get('li')
.should('have.length', 1)
Expand All @@ -45,7 +39,7 @@ describe('Users with Fetch', () => {
it('can inspect mocked network reaponse', () => {
const users = [{ id: 1, name: 'foo' }]

cy.route('GET', '/users?_limit=3', users).as('users')
cy.intercept('GET', '/users?_limit=3', users).as('users')
mount(<Users />)
cy.wait('@users')
.its('response.body')
Expand All @@ -55,10 +49,8 @@ describe('Users with Fetch', () => {
it('can delay and wait on Ajax call', () => {
const users = [{ id: 1, name: 'foo' }]

cy.route({
method: 'GET',
url: '/users?_limit=3',
response: users,
cy.intercept('GET', '/users?_limit=3', {
body: users,
delay: 1000,
}).as('users')

Expand Down
11 changes: 4 additions & 7 deletions npm/vue/cypress/component/advanced/fetch-polyfill/Users.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,18 @@ describe('Fetching users with polyfill', () => {
})

it('can spy on the fetch requests', () => {
cy.server()
cy.route('/users?_limit=3').as('users')
cy.intercept('/users?_limit=3').as('users')
mount(Users)
cy.wait('@users')
.its('responseBody.length')
.its('response.body.length')
.then((length) => {
cy.get('.user').should('have.length', length)
})
})

it('shows loading UI while fetch is happening', () => {
cy.server()
cy.route({
url: '/users?_limit=3',
response: 'fixture:users',
cy.intercept('/users?_limit=3', {
fixture: 'users',
delay: 1000,
})

Expand Down

0 comments on commit 2f18a8c

Please sign in to comment.