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

add wait options #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pactflow/pact-cypress-adapter",
"version": "1.3.0",
"version": "1.3.1",
"description": "A cypress adapter for pact",
"keywords": [
"pact",
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AUTOGEN_HEADER_BLOCKLIST } from './constants'
import { AliasType, AnyObject, PactConfigType, XHRRequestAndResponse, RequestOptionType } from 'types'
import { AliasType, AnyObject, PactConfigType, XHRRequestAndResponse, RequestOptionType, WaitOptions } from 'types'
import { formatAlias, writePact } from './utils'
import { env } from 'process'

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace Cypress {
interface Chainable {
usePactWait: (alias: AliasType) => Chainable
usePactWait: (alias: AliasType, options?: WaitOptions) => Chainable
usePactRequest: (option: AnyObject, alias: string) => Chainable
usePactGet: (alias: string, pactConfig: PactConfigType) => Chainable
setupPact: (consumerName: string, providerName: string) => Chainable<null>
Expand Down Expand Up @@ -36,7 +35,7 @@ const setupPactHeaderBlocklist = (headers: string[]) => {
headersBlocklist = [...headers, ...headersBlocklist]
}

const usePactWait = (alias: AliasType) => {
const usePactWait = (alias: AliasType, options?: Partial<WaitOptions>) => {
const formattedAlias = formatAlias(alias)
// Cypress versions older than 8.2 do not have a currentTest objects
const testCaseTitle = Cypress.currentTest ? Cypress.currentTest.title : ''
Expand All @@ -53,7 +52,7 @@ const usePactWait = (alias: AliasType) => {
})
})
} else {
cy.wait(formattedAlias).then((intercept) => {
cy.wait(formattedAlias, options).then((intercept) => {
const flattenIntercept = Array.isArray(intercept) ? intercept[0] : intercept
writePact({
intercept: flattenIntercept,
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ export type PactFileType = {
blocklist?: string[],
content?: any
}

export type WaitOptions = NonNullable<Parameters<Cypress.Chainable['wait']>[1]>;
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { AliasType, Interaction, PactConfigType, XHRRequestAndResponse, PactFile
const pjson = require('../package.json')
export const formatAlias = (alias: AliasType) => {
if (Array.isArray(alias)) {
return [...alias].map((a) => `@${a}`)
return [...alias].map((a) => addAtIfNotPresent(a))
}
return [addAtIfNotPresent(alias)]
}

const addAtIfNotPresent = (alias: string): string => {
if (alias.startsWith('@')) {
return alias;
} else {
return `@${alias}`;
}
return [`@${alias}`]
}

const constructFilePath = ({ consumerName, providerName }: PactConfigType) =>
Expand Down
5 changes: 5 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ describe('formatAlias', () => {
it('should format single string to a formatted array', () => {
expect(formatAlias('a')).toEqual(['@a'])
})

it('should not change format if the input is already in alias format', () => {
const formattedAlias = "@alias";
expect(formatAlias(formattedAlias)).toEqual([formattedAlias])
})
})

describe('constructPactFile', () => {
Expand Down