![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
jest-pact
Advanced tools
Changelog
Readme
Jest-Pact
Roadmapnpm i jest-pact --save-dev
OR
yarn add jest-pact --dev
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
// regular pact tests go here
}
Say that your API layer looks something like this:
import axios from 'axios';
const defaultBaseUrl = "http://your-api.example.com"
export const api = (baseUrl = defaultBaseUrl) => ({
getHealth: () => axios.get(`${baseUrl}/health`)
.then(response => response.data.status)
/* other endpoints here */
})
Then your test might look like:
import { pactWith } from 'jest-pact';
import { Matchers } from '@pact-foundation/pact';
import api from 'yourCode';
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
let client;
beforeEach(() => {
client = api(provider.mockService.baseUrl)
});
describe('health endpoint', () => {
// Here we set up the interaction that the Pact
// mock provider will expect.
//
// jest-pact takes care of validating and tearing
// down the provider for you.
beforeEach(() =>
provider.addInteraction({
state: "Server is healthy",
uponReceiving: 'A request for API health',
willRespondWith: {
status: 200,
body: {
status: Matchers.like('up'),
},
},
withRequest: {
method: 'GET',
path: '/health',
},
})
);
// You also test that the API returns the correct
// response to the data layer.
//
// Although Pact will ensure that the provider
// returned the expected object, you need to test that
// your code recieves the right object.
//
// This is often the same as the object that was
// in the network response, but (as illustrated
// here) not always.
it('returns server health', () =>
client.health().then(health => {
expect(health).toEqual('up');
}));
});
You can make your tests easier to read by extracting your request and responses:
/* pact.fixtures.js */
import { Matchers } from '@pact-foundation/pact';
export const healthRequest = {
uponReceiving: 'A request for API health',
withRequest: {
method: 'GET',
path: '/health',
},
};
export const healthyResponse = {
status: 200,
body: {
status: Matchers.like('up'),
},
}
import { pactWith } from 'jest-pact';
import { healthRequest, healthyResponse } from "./pact.fixtures";
import api from 'yourCode';
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
let client;
beforeEach(() => {
client = api(provider.mockService.baseUrl)
});
describe('health endpoint', () => {
beforeEach(() =>
provider.addInteraction({
state: "Server is healthy",
...healthRequest,
willRespondWith: healthyResponse
})
);
it('returns server health', () =>
client.health().then(health => {
expect(health).toEqual('up');
}));
});
pactWith(PactOptions, provider => {
// regular pact tests go here
}
interface PactOptions {
provider: string;
consumer: string;
port?: number; // defaults to a random port if not provided
pactfileWriteMode?: PactFileWriteMode;
dir? string // defaults to pact/pacts if not provided
}
type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
type PactFileWriteMode = "overwrite" | "update" | "merge";
By default Jest will watch all your files for changes, which means it will run in an infinite loop as your pact tests will generate json pact files and log files.
You can get round this by using the following watchPathIgnorePatterns: ["pact/logs/*","pact/pacts/*"]
in your jest.config.js
Example
module.exports = {
testMatch: ["**/*.test.(ts|js)", "**/*.it.(ts|js)", "**/*.pacttest.(ts|js)"],
watchPathIgnorePatterns: ["pact/logs/*", "pact/pacts/*"]
};
You can now run your tests with jest --watch
and when you change a pact file, or your source code, your pact tests will run
jest-pact
See Jest-Pact-Typescript which showcases a full consumer workflow written in Typescript with Jest, using this adaptor
git@github.com:YOU54F/jest-pact-typescript.git
yarn install
yarn run pact-test
Generated pacts will be output in pact/pacts
Log files will be output in pact/logs
FAQs
a pact adaptor for jest
The npm package jest-pact receives a total of 26,546 weekly downloads. As such, jest-pact popularity was classified as popular.
We found that jest-pact demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.