Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test cases #26

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
run: npm install
- name: Run tests
run: npm run test-ci
- name: Smoke test
run: node lib/index.js
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
Expand Down
117 changes: 117 additions & 0 deletions test/lib/logger.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { afterEach, describe, expect, it, vi } from "vitest"
import Logger from "../../lib/logger"
import { makeFakeExecution } from "../utils/fake"

describe("Logger", () => {
afterEach(() => {
vi.resetAllMocks()
vi.unstubAllEnvs()
})

describe("getLogging", () => {
it("always url from the newman execution in the log", () => {
// arrange
const logger = new Logger()
const execution = makeFakeExecution()

// act
const log = logger.getLogging(execution)

// assert
expect(log.url).toContain("https://example.com/api/v1")
})

it("includes query parameters in the url if present", () => {
const logger = new Logger()
const execution = makeFakeExecution({
request: {
method: "GET",
url: {
protocol: "https",
host: ["example", "com"],
path: ["api", "v1"],
query: { members: [{ key: "param", value: "value" }] },
},
headers: [{ key: "Content-Type", value: "application/json" }],
body: "",
},
})

const log = logger.getLogging(execution)

expect(log.url).toBe(
"Request: GET https://example.com/api/v1?param=value",
)
})

it("only returns url when TESTRAIL_LOGGING env variable is set to 'none'", () => {
vi.stubEnv("TESTRAIL_LOGGING", "none")
const logger = new Logger()
const execution = makeFakeExecution()

const log = logger.getLogging(execution)

expect(log.request).toBe("")
expect(log.response).toBe("")
})

it("includes headers when TESTRAIL_LOGGING env variable is set to 'headers'", () => {
vi.stubEnv("TESTRAIL_LOGGING", "headers")
const logger = new Logger()
const execution = makeFakeExecution()

const log = logger.getLogging(execution)

expect(log.request).toBe("Headers:\nContent-Type: application/json")
expect(log.response).toBe("")
})

it("includes full request and response when TESTRAIL_LOGGING env variable is set to 'full'", () => {
vi.stubEnv("TESTRAIL_LOGGING", "full")
const logger = new Logger()
const execution = makeFakeExecution({
request: {
method: "GET",
url: {
protocol: "https",
host: ["example", "com"],
path: ["api", "v1"],
query: { members: [] },
},
headers: [{ key: "Content-Type", value: "application/json" }],
body: '{"key": "value"}',
},
response: {
code: 200,
status: "OK",
headers: [{ key: "Content-Type", value: "application/json" }],
stream: Buffer.from(""),
},
})

const log = logger.getLogging(execution)

expect(log.request).toBe(
'Headers:\nContent-Type: application/json\nBody: {"key": "value"}',
)

expect(log.response).toContain(
"Response: 200 OK\nHeaders:\nContent-Type: application/json",
)
})

it("output requestError when responseError is setted", () => {
vi.stubEnv("TESTRAIL_LOGGING", "full")
const logger = new Logger()
const execution = makeFakeExecution({
response: undefined,
requestError: { message: "Network error" },
})

const log = logger.getLogging(execution)

expect(log.response).toContain("A request error has occurred.")
expect(log.response).toContain("Network error")
})
})
})
22 changes: 22 additions & 0 deletions test/utils/fake.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,25 @@ export const makeFakeJsonifyResult = () => {
},
]
}

// モックの execution オブジェクト生成関数
export const makeFakeExecution = (overrides = {}) => ({
request: {
method: "GET",
url: {
protocol: "https",
host: ["example", "com"],
path: ["api", "v1"],
query: { members: [] },
},
headers: [{ key: "Content-Type", value: "application/json" }],
body: "",
},
response: {
code: 200,
status: "OK",
headers: [{ key: "Content-Type", value: "application/json" }],
stream: Buffer.from(""),
},
...overrides,
})