Skip to content

Commit

Permalink
refactor: add comments and moved unused exports
Browse files Browse the repository at this point in the history
  • Loading branch information
gminetoma committed Feb 3, 2025
1 parent b69b655 commit a3674fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
4 changes: 2 additions & 2 deletions test/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Cypress.Commands.add('login', () => {
autoEnd: false
})

log.snapshot('before')
log.snapshot('Connecting to auth0')

cy.session(`auth0-${auth0UserName}`, () => {
auth0Login()
Expand All @@ -49,6 +49,6 @@ Cypress.Commands.add('login', () => {
cacheAcrossSpecs: true
})

log.snapshot('after')
log.snapshot('Successfully logged in')
log.end()
})
1 change: 1 addition & 0 deletions test/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="cypress" />

// https://docs.cypress.io/app/tooling/typescript-support#Types-for-Custom-Commands
declare namespace Cypress{
interface Chainable<Subject> {
login(): Chainable<Subject>
Expand Down
48 changes: 33 additions & 15 deletions test/cypress/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,40 @@ export const auth0Login = () => {
cy.url().should('equal', baseUrl)
}

// https://docs.cypress.io/api/commands/intercept#Interception-lifecycle
export const requestHandler = (req: IncomingHttpRequest) => {
req.on('before:response', res => {
// force all API responses to not be cached
res.headers['cache-control'] = 'no-store'
})
}

// https://docs.cypress.io/app/guides/network-requests#Working-with-GraphQL
/**
* https://docs.cypress.io/app/guides/network-requests#Working-with-GraphQL
*
* Set the alias for a request based on the operation name. After setting it we can use cy.wait(@{alias}).
*
* e.g. operation: query Submissions / cy.wait('@query Submissions')
**/
export const aliasQuery = (req: IncomingHttpRequest, operation: string, responseBody: unknown) => {
requestHandler(req)
/**
* Check if the GraphQL operation is included in the request body
**/
const hasOperation = (req: IncomingHttpRequest, operation: string): boolean => {
const { query } = req.body
return query && query.includes(operation)
}

/**
* https://docs.cypress.io/api/commands/intercept#Interception-lifecycle
*
* https://docs.cypress.io/api/commands/intercept#cyintercept-and-request-caching
*
* Sometimes Cypress.intercept() cannot intercept a request due to the request being cached and not hitting the
* network layer (where Cypress.intercept() works).
*
* To prevent this, we set 'cache-control' to 'no-store', this prevents all the requests to be cached.
**/
const preventRequestCache = (req: IncomingHttpRequest) => {
req.on('before:response', res => {
// force all API responses to not be cached
res.headers['cache-control'] = 'no-store'
})
}

preventRequestCache(req)

if (!hasOperation(req, operation)) {
req.continue()
Expand All @@ -44,8 +67,3 @@ export const aliasQuery = (req: IncomingHttpRequest, operation: string, response
body: responseBody
})
}

export const hasOperation = (req: IncomingHttpRequest, operation) => {
const { query } = req.body
return query && query.includes(operation)
}

0 comments on commit a3674fc

Please sign in to comment.