Last active
April 29, 2020 06:07
-
-
Save eduardopc/519da02d0c22fef1a0e1ec82be5b6d3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dotenv/config'; | |
import { Pact } from '@pact-foundation/pact'; | |
import { eachLike, somethingLike, integer } from '@pact-foundation/pact/dsl/matchers'; | |
import path from 'path'; | |
import { fetchCharacters } from '../../../src/service/api'; | |
const mockProvider = new Pact({ | |
consumer: 'web-application', | |
provider: 'characters-api-application', | |
port: parseInt(process.env.MOCK_SERVER_PORT), | |
log: path.resolve(process.cwd(), 'logs', 'pact.log'), | |
dir: path.resolve(process.cwd(), 'pacts'), | |
logLevel: 'WARN', | |
spec: 2, | |
cors: true | |
}); | |
describe('API Pact Test - Characters Service', () => { | |
beforeAll(async () => | |
await mockProvider.setup().then(() => { | |
mockProvider.addInteraction({ | |
uponReceiving: 'a request to list all characters', | |
withRequest: { | |
method: 'GET', | |
path: '/characters' | |
}, | |
willRespondWith: { | |
status: 200, | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
body: eachLike ( | |
{ | |
id: integer(1), | |
name: somethingLike('Walter White'), | |
age: integer(58) | |
}, | |
{ min: 5 } | |
) | |
} | |
}); | |
}) | |
); | |
afterEach(() => mockProvider.verify()); | |
afterAll(() => mockProvider.finalize()); | |
it('should return the expected data', async () => { | |
const response = await fetchCharacters(); | |
const { name, age } = response.data[0]; | |
expect(response.status).toEqual(200); | |
expect(name).toBe('Walter White'); | |
expect(age).toBe(58); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment