From 00471820358a6b58c8518d5aaf57b16723e50008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Sch=C3=BCtte?= Date: Mon, 21 Aug 2023 16:26:58 +0200 Subject: [PATCH] add wait options --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 9 ++++----- src/types.ts | 2 ++ src/utils.ts | 12 ++++++++++-- test/utils.test.ts | 5 +++++ 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 85060d5..eaee8c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@pactflow/pact-cypress-adapter", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@pactflow/pact-cypress-adapter", - "version": "1.3.0", + "version": "1.3.1", "license": "MIT", "dependencies": { "@types/lodash": "^4.14.178", diff --git a/package.json b/package.json index b8f1954..8ba35a7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index fc0e993..12f05d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 @@ -36,7 +35,7 @@ const setupPactHeaderBlocklist = (headers: string[]) => { headersBlocklist = [...headers, ...headersBlocklist] } -const usePactWait = (alias: AliasType) => { +const usePactWait = (alias: AliasType, options?: Partial) => { const formattedAlias = formatAlias(alias) // Cypress versions older than 8.2 do not have a currentTest objects const testCaseTitle = Cypress.currentTest ? Cypress.currentTest.title : '' @@ -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, diff --git a/src/types.ts b/src/types.ts index c2dbcec..9267ff4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -76,3 +76,5 @@ export type PactFileType = { blocklist?: string[], content?: any } + +export type WaitOptions = NonNullable[1]>; \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 87d3384..dc8225a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) => diff --git a/test/utils.test.ts b/test/utils.test.ts index c4e4b54..7532eea 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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', () => {