This project aims to create a package that can generate pact files only from TypeScript definitions (without the necessity of writing separated tests with using pact-js package).
All these points are only an early version of implementation for that package.
In pacts.config.js:
module.exports = {
consumer: 'some-consumer',
};
In pacts.config.js:
module.exports = {
providers: [
{
provider: 'some-provider',
files: ['src/api/**/*.ts'],
},
],
};
Pact Specification in version 2.0.0
In pacts.config.js:
module.exports = {
buildDir: '/pacts',
};
/**
* @pact
*/
function fetchComments() {
// ...
}
/**
* @pact
* @pact-description "request to get comments"
*/
function fetchComments() {
// ...
}
If we do not specify description of interaction by @pact-description
, the description is set from the name of function / variable / property.
/**
* @pact
* @pact-method GET
*/
function fetchComments() {
// ...
}
/**
* @pact
* @pact-response-status 200
*/
function fetchComments() {
// ...
}
If we do not use @pact-response-status
, the proper response status is set based on given HTTP method.
/**
* @pact
* @pact-path /api/images/100
*/
function fetchImage(imageId: number) {
// ...
}
/**
* @pact
*/
function fetchComments() {
// ...
return data;
}
Pact-gen-ts recognizes response body type of interaction from returned object.
/**
* @pact
*/
function fetchComments() {
// ...
/** @pact-response-body */
const data = response.body;
// ...
}
ISO8601 Date
Example: 2021-04-13
ISO8601 Date and Time string
Example: 2021-04-13T10:14:53+01:00
ISO8601 DateTime with millisecond precision
Example: 2021-04-13T10:14:53.123+01:00
ISO8601 Time, matches a pettern of the format "'T'HH:mm:ss"
Example: T10.14.53.342Z
RFC3339 Timestamp
Example: Tue, 13 Apr 2021 10:14:53 -0400
UUID v4
Example: ce11b6e-d8e1-11e7-9296-cec278b6b50a
Example: 127.0.0.13
Example: ::ffff:192.0.2.128
Example: A4C3Ff
interface CommentDTO {
id: number;
/** @pact-matcher email */
user: string;
/** @pact-matcher iso-datetime */
datetime: string;
comment: string;
}
interface Address {
city: string;
address: string;
/** @pact-example "99-400" */
postCode: string;
}
@BaseUrl('/api/v1/posts/:postId')
class PostsApi extends ApiClass {
getPost(postId: string) {
const url = this.endpoint(postId);
return axios.get(url);
}
deletePost(postId: string) {
const url = this.endpoint(postId);
return axios.delete(url);
}
@PathApi('/comments')
getCommentsForPost(postId: string) {
const url = this.endpoint(postId);
return axios.get(url);
}
@PathApi('/comments/:commentId')
deleteCommentsForPost(postId: string, commentId: string) {
const url = this.endpoint(postId, commentId);
return axios.get(url);
}
}
function fetchComments(/** @pact-query */ query: Query) {
// ...
}
interface Query {
fromUser: string;
postId: string;
}
function addComment(/** @pact-request-body */ newComment: NewComment) {
// ...
}
interface NewComment {
content: string;
postId: string;
}
or
function addComment(postId: string, commentContent: string) {
/** @pact-request-body */
const newComment = {
postId,
commentContent,
};
// ...
}
In pacts.config.js you can specify common headers for request or response in the range of provider:
module.exports = {
providers: [
{
provider: 'some-provider',
files: ['src/api/**/*.ts'],
requestHeaders: {
authorization: 'auth',
},
responseHeaders: {
'Content-Type': 'application/json',
},
},
],
};
You can also add headers by jsDocs:
/**
* @pact
* @pact-request-header "Content-Type" "application/pdf"
* @pact-response-header "Content-Type" "application/pdf"
*/
function updateReport() {
// ...
}
Add the "verbose" flag if you want the created interactions to be displayed in the console.
In pacts.config.js:
module.exports = {
verbose: true,
};