Kyle Shrader
12/15/2023, 6:43 PMMatchersV3.eachValueMatches
Or, can I use this in a v4 pactWith?Kyle Shrader
12/15/2023, 6:48 PMMatt (pactflow.io / pact-js / pact-go)
Kyle Shrader
12/16/2023, 2:12 AMKyle Shrader
12/16/2023, 2:16 AM.addInteraction()
....
.withResponse(200, (builder)=>{})
But I suspect that's v3 pactWith, while v4's addInteraction only accepts an object for body.Kyle Shrader
12/16/2023, 4:28 AMimport { Matchers, MatchersV3 } from '@pact-foundation/pact'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render } from '@testing-library/react'
import { pactWith } from 'jest-pact'
import FilterPane from '@/modules/properties/filtering/FilterPane'
const queryClient = new QueryClient()
const originalFetch = fetch
pactWith(
{
consumer: 'environment-comparison-application',
provider: 'environment-comparison-service',
},
(provider) => {
beforeAll(() => {
const { port } = provider.opts
jest.spyOn(global, 'fetch').mockImplementation((input, init) => {
const url = typeof input === 'string' ? input : (input as Request).url
if (url.startsWith('/api')) {
return originalFetch(`<http://localhost>:${port}${input}`, init)
}
return originalFetch(input, init)
})
})
afterAll(() => {
jest.restoreAllMocks()
})
beforeEach(async () => {
await provider.addInteraction({
uponReceiving: 'a request for supported items',
withRequest: {
method: 'GET',
path: '/api/supported',
},
willRespondWith: {
status: 200,
body: {
itemGroups: {
'Database Tables': Matchers.eachLike('Table1', { min: 1 }),
'Property Files': Matchers.eachLike('Service1', { min: 1 }),
Environments: Matchers.eachLike('Env1', { min: 1 }),
},
environmentSupport: MatchersV3.eachValueMatches(
{
Env1: ['Service1'],
},
MatchersV3.eachLike('Service1', 1)
),
},
},
})
})
describe('returns a list of supported items', async () => {
const { findByText } = render(
<QueryClientProvider client={queryClient}>
<FilterPane />
</QueryClientProvider>
)
const env1 = await findByText('ENV1')
expect(env1).toBeInTheDocument()
const service1 = await findByText('Service1')
expect(service1).toBeInTheDocument()
const table1 = await findByText('Table1')
expect(table1).toBeInTheDocument()
})
}
)
The ts error on body:
Type '{ itemGroups: { 'Database Tables': ArrayMatcher<string[]>; 'Property Files': ArrayMatcher<string[]>; Environments: ArrayMatcher<string[]>; }; environmentSupport: MatchersV3.RulesMatcher<...>; }' is not assignable to type 'AnyTemplate | undefined'.
Types of property 'environmentSupport' are incompatible.
Type 'RulesMatcher<string[]>' is not assignable to type 'string | number | boolean | JsonArray | JsonMap | Matcher<AnyTemplate> | ArrayMatcher<AnyTemplate> | TemplateMap | ArrayTemplate | null | undefined'.
Type 'RulesMatcher<string[]>' is not assignable to type 'TemplateMap'.
Index signature for type 'string' is missing in type 'RulesMatcher<string[]>'.ts(2322)
Working approach using V3 pactWith:
import { Matchers, MatchersV3 } from '@pact-foundation/pact'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render } from '@testing-library/react'
import { pactWith } from 'jest-pact/dist/v3'
import FilterPane from '@/modules/properties/filtering/FilterPane'
const queryClient = new QueryClient()
const originalFetch = fetch
const mockFetch = (port: number) => {
jest.spyOn(global, 'fetch').mockImplementation((input, init) => {
const url = typeof input === 'string' ? input : (input as Request).url
if (url.startsWith('/api')) {
return originalFetch(`<http://localhost>:${port}${input}`, init)
}
return originalFetch(input, init)
})
}
pactWith(
{
consumer: 'environment-comparison-application',
provider: 'environment-comparison-service',
},
(interaction) => {
interaction('has a list of supported items', ({ provider, execute }) => {
beforeAll(() => {})
afterAll(() => {
jest.restoreAllMocks()
})
beforeEach(async () => {
await provider.addInteraction({
uponReceiving: 'a request for supported items',
withRequest: {
method: 'GET',
path: '/api/supported',
},
willRespondWith: {
status: 200,
body: {
itemGroups: {
'Database Tables': Matchers.eachLike('Table1', { min: 1 }),
'Property Files': Matchers.eachLike('Service1', { min: 1 }),
Environments: Matchers.eachLike('Env1', { min: 1 }),
},
environmentSupport: MatchersV3.eachValueMatches(
{
Env1: ['Service1'],
},
MatchersV3.eachLike('Service1', 1)
),
},
},
})
})
describe('returns a list of supported items', () => {
execute('api call', async (mockserver) => {
mockFetch(mockserver.port)
const { findByText } = render(
<QueryClientProvider client={queryClient}>
<FilterPane />
</QueryClientProvider>
)
const env1 = await findByText('ENV1')
expect(env1).toBeInTheDocument()
const service1 = await findByText('Service1')
expect(service1).toBeInTheDocument()
const table1 = await findByText('Table1')
expect(table1).toBeInTheDocument()
})
})
})
}
)
Kyle Shrader
12/18/2023, 5:39 PMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Yousaf Nabi (pactflow.io)