Skip to content

Commit

Permalink
test: Postman CollectionのexecをTypeScriptで書けるように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
windchime-yk committed Oct 18, 2024
1 parent 7c5a696 commit 9caba46
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 164 deletions.
154 changes: 154 additions & 0 deletions src/collection.newman.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Collection } from "postman-collection";

declare namespace Postman {
interface PM {
test(
testName: string,
specFunction: (done?: (error?: unknown) => void) => void,
): void;
expect: Chai.ExpectStatic;
response: {
to: Chai.Assertion;
json(): {
status: number;
method: string;
text: string;
[key: string]: string | number;
};
text(): string;
};
}
}

declare const pm: Postman.PM;

const convertPostmanTestToStringArray = (pmTest: () => void): string[] => {
// TODO: 実行に支障はないが、出力されたexecがすごく汚いので、どうにかして直したい
return [pmTest.toString().replace(/\(\)\=\>(.*)/g, "$1")];
};

export const collection = new Collection({
name: "verify-cfw-drizzle-crud",
item: [
{
name: "GET",
event: [
{
listen: "test",
script: {
exec: convertPostmanTestToStringArray(() =>
pm.test("レスポンスボディのJSONが想定通りか", () => {
const jsonData = pm.response.json();
pm.expect(jsonData.status).to.eql(200);
pm.expect(jsonData.method).to.eql("GET");
pm.expect(jsonData.text).to.eql("OK!");
pm.expect(jsonData.apiKey).to.eql("Example_0000");
}),
),
type: "text/javascript",
},
},
],
request: {
method: "GET",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "POST",
event: [
{
listen: "test",
script: {
exec: convertPostmanTestToStringArray(() =>
pm.test("レスポンスボディのJSONが想定通りか", () => {
const jsonData = pm.response.json();
pm.expect(jsonData.status).to.eql(200);
pm.expect(jsonData.method).to.eql("POST");
pm.expect(jsonData.text).to.eql("OK!");
pm.expect(jsonData.apiKey).to.eql("Example_0000");
}),
),
type: "text/javascript",
},
},
],
request: {
method: "POST",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "PATCH",
event: [
{
listen: "test",
script: {
exec: convertPostmanTestToStringArray(() =>
pm.test("レスポンスボディのJSONが想定通りか", () => {
const jsonData = pm.response.json();
pm.expect(jsonData.status).to.eql(200);
pm.expect(jsonData.method).to.eql("PATCH");
pm.expect(jsonData.text).to.eql("OK!");
pm.expect(jsonData.apiKey).to.eql("Example_0000");
}),
),
type: "text/javascript",
},
},
],
request: {
method: "PATCH",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "DELETE",
event: [
{
listen: "test",
script: {
exec: convertPostmanTestToStringArray(() =>
pm.test("レスポンスボディのJSONが想定通りか", () => {
const jsonData = pm.response.json();
pm.expect(jsonData.status).to.eql(200);
pm.expect(jsonData.method).to.eql("DELETE");
pm.expect(jsonData.text).to.eql("OK!");
pm.expect(jsonData.apiKey).to.eql("Example_0000");
}),
),
type: "text/javascript",
},
},
],
request: {
method: "DELETE",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
],
});
128 changes: 1 addition & 127 deletions src/index.newman.ts
Original file line number Diff line number Diff line change
@@ -1,132 +1,6 @@
import { writeFileSync } from "node:fs";
import * as newman from "newman";
import { Collection } from "postman-collection";

const collection = new Collection({
name: "verify-cfw-drizzle-crud",
item: [
{
name: "GET",
event: [
{
listen: "test",
script: {
exec: [
'pm.test("レスポンスボディのJSONが想定通りか", () => {\r',
" const jsonData = pm.response.json();\r",
" pm.expect(jsonData.status).to.eql(200);\r",
' pm.expect(jsonData.method).to.eql("GET");\r',
' pm.expect(jsonData.text).to.eql("OK!");\r',
' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r',
"});",
],
type: "text/javascript",
},
},
],
request: {
method: "GET",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "POST",
event: [
{
listen: "test",
script: {
exec: [
'pm.test("レスポンスボディのJSONが想定通りか", () => {\r',
" const jsonData = pm.response.json();\r",
" pm.expect(jsonData.status).to.eql(200);\r",
' pm.expect(jsonData.method).to.eql("POST");\r',
' pm.expect(jsonData.text).to.eql("OK!");\r',
' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r',
"});",
],
type: "text/javascript",
},
},
],
request: {
method: "POST",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "PATCH",
event: [
{
listen: "test",
script: {
exec: [
'pm.test("レスポンスボディのJSONが想定通りか", () => {\r',
" const jsonData = pm.response.json();\r",
" pm.expect(jsonData.status).to.eql(200);\r",
' pm.expect(jsonData.method).to.eql("PATCH");\r',
' pm.expect(jsonData.text).to.eql("OK!");\r',
' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r',
"});",
],
type: "text/javascript",
},
},
],
request: {
method: "PATCH",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
{
name: "DELETE",
event: [
{
listen: "test",
script: {
exec: [
'pm.test("レスポンスボディのJSONが想定通りか", () => {\r',
" const jsonData = pm.response.json();\r",
" pm.expect(jsonData.status).to.eql(200);\r",
' pm.expect(jsonData.method).to.eql("DELETE");\r',
' pm.expect(jsonData.text).to.eql("OK!");\r',
' pm.expect(jsonData.apiKey).to.eql("Example_0000");\r',
"});",
],
type: "text/javascript",
},
},
],
request: {
method: "DELETE",
header: [],
url: {
protocol: "http",
host: ["localhost"],
port: "8787",
},
},
response: [],
},
],
});
import { collection } from "./collection.newman";

writeFileSync(
"src/verify-cfw-drizzle-crud.postman_collection.json",
Expand Down
Loading

0 comments on commit 9caba46

Please sign in to comment.