Integration Testing Guide ๐งช โ
Learn how to use get-cookie in your testing workflows.
Basic Testing Setup โ
Installation โ
bash
# Add as dev dependency
pnpm add -D @mherod/get-cookie
# Or with npm
npm install --save-dev @mherod/get-cookie
# Or with yarn
yarn add --dev @mherod/get-cookieTest Helper Function โ
typescript
// test/helpers/cookies.ts
import { getCookie } from "@mherod/get-cookie";
export async function getAuthCookie(domain: string): Promise<string> {
  try {
    const cookies = await getCookie({
      name: "auth",
      domain,
      removeExpired: true,
    });
    return cookies[0]?.value ?? "";
  } catch (error) {
    console.error("Failed to get auth cookie:", error);
    return "";
  }
}Testing Frameworks โ
Jest โ
typescript
// test/api.test.ts
import { getAuthCookie } from "./helpers/cookies";
describe("API Tests", () => {
  let authCookie: string;
  beforeAll(async () => {
    authCookie = await getAuthCookie("api.example.com");
  });
  test("authenticated request", async () => {
    const response = await fetch("https://api.example.com/me", {
      headers: {
        Cookie: `auth=${authCookie}`,
      },
    });
    expect(response.status).toBe(200);
  });
});Mocha โ
typescript
// test/api.spec.ts
import { getAuthCookie } from "./helpers/cookies";
import { expect } from "chai";
describe("API Tests", () => {
  let authCookie: string;
  before(async () => {
    authCookie = await getAuthCookie("api.example.com");
  });
  it("should make authenticated request", async () => {
    const response = await fetch("https://api.example.com/me", {
      headers: {
        Cookie: `auth=${authCookie}`,
      },
    });
    expect(response.status).to.equal(200);
  });
});Testing Strategies โ
E2E Testing โ
typescript
// cypress/support/commands.ts
import { getCookie } from "@mherod/get-cookie";
Cypress.Commands.add("loginWithCookie", async (domain) => {
  const cookies = await getCookie({
    name: "auth",
    domain,
  });
  cookies.forEach((cookie) => {
    cy.setCookie(cookie.name, cookie.value, {
      domain: cookie.domain,
      path: "/",
    });
  });
});
// cypress/e2e/dashboard.cy.ts
describe("Dashboard", () => {
  beforeEach(() => {
    cy.loginWithCookie("example.com");
    cy.visit("/dashboard");
  });
  it("shows user data", () => {
    cy.get("[data-test=user-name]").should("be.visible");
  });
});API Testing โ
typescript
// test/helpers/api.ts
import { getCookie } from "@mherod/get-cookie";
export class ApiClient {
  private baseUrl: string;
  private cookies: string[];
  constructor(baseUrl: string) {
    this.baseUrl = baseUrl;
    this.cookies = [];
  }
  async authenticate() {
    const cookies = await getCookie({
      name: "%",
      domain: new URL(this.baseUrl).hostname,
    });
    this.cookies = cookies.map((c) => `${c.name}=${c.value}`);
  }
  async request(path: string) {
    return fetch(`${this.baseUrl}${path}`, {
      headers: {
        Cookie: this.cookies.join("; "),
      },
    });
  }
}
// test/api/users.test.ts
const api = new ApiClient("https://api.example.com");
beforeAll(async () => {
  await api.authenticate();
});
test("get user profile", async () => {
  const response = await api.request("/me");
  expect(response.status).toBe(200);
});CI/CD Integration โ
GitHub Actions โ
yaml
# .github/workflows/test.yml
name: Integration Tests
on: [push]
jobs:
  test:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v2
      - name: Setup Chrome
        uses: browser-actions/setup-chrome@v1
      - name: Login to Test Account
        run: |
          # Login to test account in Chrome
          ./scripts/login.sh
      - name: Run Tests
        run: |
          pnpm install
          pnpm testAdvanced Testing Workflow โ
yaml
# .github/workflows/comprehensive-tests.yml
name: Comprehensive Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20.x
      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9.15.2
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Run comprehensive tests
        run: |
          pnpm test
          pnpm run test:integration
          pnpm run test:e2eBest Practices โ
Cookie Management โ
- Clear Cookies Between Tests typescript- afterEach(async () => { // Clear browser cookies await clearBrowserCookies(); });
- Handle Missing Cookies typescript- function assertCookie(cookie?: string) { if (!cookie) { throw new Error("Required cookie not found"); } return cookie; }
- Timeout Handling typescript- const COOKIE_TIMEOUT = 5000; async function waitForCookie(domain: string) { const startTime = Date.now(); while (Date.now() - startTime < COOKIE_TIMEOUT) { const cookie = await getAuthCookie(domain); if (cookie) return cookie; await new Promise((r) => setTimeout(r, 100)); } throw new Error("Cookie timeout"); }
Security โ
- Test Account Isolation - Use dedicated test accounts
- Never use production cookies
- Rotate test credentials
 
- CI Environment - Use ephemeral browsers
- Clear state between runs
- Secure cookie storage
 
- Error Handling typescript- process.on("uncaughtException", (error) => { // Clear sensitive data clearCookies(); process.exit(1); });
Debugging โ
Logging โ
typescript
const DEBUG = process.env.DEBUG === "1";
function debugLog(message: string, data?: any) {
  if (DEBUG) {
    console.log(`[get-cookie] ${message}`, data);
  }
}Cookie Inspection โ
typescript
function inspectCookie(cookie: any) {
  const { value, ...safeProps } = cookie;
  debugLog("Cookie found:", {
    ...safeProps,
    valueLength: value?.length,
  });
}Error Tracking โ
typescript
class CookieError extends Error {
  constructor(
    message: string,
    public readonly context: any,
  ) {
    super(message);
    this.name = "CookieError";
  }
}
try {
  // Test code
} catch (error) {
  throw new CookieError("Test failed", {
    browser: process.env.BROWSER,
    timestamp: new Date().toISOString(),
  });
}