Teste de Autenticação Vitest

TODO: collapasable, copyable, download, etc

For this Typescript code...

export const isValidEmailDomain = (email: string, domainAllowList: string[]): boolean => {
  const parts = email.split('@');
  const emailDomain = parts[1];
  return domainAllowList.includes(emailDomain);
};



export const getDomainAllowList = (): string[] => {
  if (!domainAllowList) {
    domainAllowList = requireEnv(DOMAIN_ALLOW_LIST_KEY)?.split(',').map((domain) => domain.trim()) || [];
  }
  return domainAllowList;
}

Please generate a test file that starts with


import { isValidEmailDomain, getDomainAllowList } from "@/app/lib/auth";```Here's a test file for the given TypeScript code using Vitest. This test file includes tests for both `isValidEmailDomain` and `getDomainAllowList` functions.

```typescript
import { describe, it, expect, beforeEach } from "vitest";
import { isValidEmailDomain, getDomainAllowList } from "@/app/lib/auth";

// Mock environment variable and requireEnv function
const DOMAIN_ALLOW_LIST_KEY = 'DOMAIN_ALLOW_LIST';
const originalEnv = process.env;

beforeEach(() => {
  process.env = { ...originalEnv }; // Restore original environment variables before each test
});

describe("isValidEmailDomain", () => {
  it("should return true if the email domain is in the allow list", () => {
    const email = "[email protected]";
    const domainAllowList = ["example.com", "test.com"];
    expect(isValidEmailDomain(email, domainAllowList)).toBe(true);
  });

  it("should return false if the email domain is not in the allow list", () => {
    const email = "[email protected]";
    const domainAllowList = ["example.com", "test.com"];
    expect(isValidEmailDomain(email, domainAllowList)).toBe(false);
  });

  it("should handle emails with multiple @ symbols", () => {
    const email = "user@[email protected]";
    const domainAllowList = ["example.com", "[email protected]"];
    expect(isValidEmailDomain(email, domainAllowList)).toBe(true);
  });
});

describe("getDomainAllowList", () => {
  it("should return an array of allowed domains from the environment variable", () => {
    process.env[DOMAIN_ALLOW_LIST_KEY] = "example.com,test.com";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual(["example.com", "test.com"]);
  });

  it("should return an empty array if the environment variable is not set", () => {
    process.env[DOMAIN_ALLOW_LIST_KEY] = "";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual([]);
  });

  it("should trim whitespace from domain names", () => {
    process.env[DOMAIN_ALLOW_LIST_KEY] = "  example.com , test.com ";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual(["example.com", "test.com"]);
  });

  it("should handle cases where requireEnv function is not available", () => {
    const requireEnv = (key: string) => process.env[key];
    global.requireEnv = requireEnv;
    
    process.env[DOMAIN_ALLOW_LIST_KEY] = "example.com,test.com";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual(["example.com", "test.com"]);
  });
});

In this test file:

  1. The isValidEmailDomain function is tested with various cases to check if it correctly identifies valid and invalid email domains.
  2. The getDomainAllowList function is tested to ensure it correctly retrieves and parses the allowed domain list from an environment variable.

Make sure to replace the placeholder requireEnv implementation with your actual environment variable retrieval logic if it's different from what's shown.```const originalEnv = process.env;

beforeEach(() => { process.env = { ...originalEnv }; // Restore original environment variables before each test });

describe("isValidEmailDomain", () => { it("should return true if the email domain is in the allow list", () => { const email = "[email protected]"; const domainAllowList = ["example.com", "test.com"]; expect(isValidEmailDomain(email, domainAllowList)).toBe(true); });

it("should return false if the email domain is not in the allow list", () => { const email = "[email protected]"; const domainAllowList = ["example.com", "test.com"]; expect(isValidEmailDomain(email, domainAllowList)).toBe(false); });

});

describe("getDomainAllowList", () => { it("should return an array of allowed domains from the environment variable", () => { process.env[DOMAIN_ALLOW_LIST_KEY] = "example.com,test.com"; const domainAllowList = getDomainAllowList(); expect(domainAllowList).toEqual(["example.com", "test.com"]); });

it("should return an empty array if the environment variable is not set", () => { process.env[DOMAIN_ALLOW_LIST_KEY] = ""; const domainAllowList = getDomainAllowList(); expect(domainAllowList).toEqual([]); });

it("should trim whitespace from domain names", () => { process.env[DOMAIN_ALLOW_LIST_KEY] = " example.com , test.com "; const domainAllowList = getDomainAllowList(); expect(domainAllowList).toEqual(["example.com", "test.com"]); }); });```

This fails with ```AssertionError: expected [ 'example.com', 'test.com' ] to deeply equal []

  • Expected
  • Received
  • Array []
  • Array [
  • "example.com",
  • "test.com",
  • ]```The error indicates that getDomainAllowList is returning a non-empty array when the environment variable is not set. This suggests that the domainAllowList variable is not being correctly reset between tests, causing a state leak.

To ensure the domainAllowList variable is properly reset, you can clear it within the beforeEach block. Here’s an updated version of your test file:

import { describe, it, expect, beforeEach } from "vitest";
import { isValidEmailDomain, getDomainAllowList } from "@/app/lib/auth";

// Mock environment variable and requireEnv function
const DOMAIN_ALLOW_LIST_KEY = 'DOMAIN_ALLOW_LIST';
let domainAllowList: string[] = [];
const originalEnv = process.env;

beforeEach(() => {
  process.env = { ...originalEnv }; // Restore original environment variables before each test
  domainAllowList = []; // Clear the domainAllowList between tests
});

describe("isValidEmailDomain", () => {
  it("should return true if the email domain is in the allow list", () => {
    const email = "[email protected]";
    const domainAllowList = ["example.com", "test.com"];
    expect(isValidEmailDomain(email, domainAllowList)).toBe(true);
  });

  it("should return false if the email domain is not in the allow list", () => {
    const email = "[email protected]";
    const domainAllowList = ["example.com", "test.com"];
    expect(isValidEmailDomain(email, domainAllowList)).toBe(false);
  });
});

describe("getDomainAllowList", () => {
  it("should return an array of allowed domains from the environment variable", () => {
    process.env[DOMAIN_ALLOW_LIST_KEY] = "example.com,test.com";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual(["example.com", "test.com"]);
  });

  it("should return an empty array if the environment variable is not set", () => {
    delete process.env[DOMAIN_ALLOW_LIST_KEY];
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual([]);
  });

  it("should trim whitespace from domain names", () => {
    process.env[DOMAIN_ALLOW_LIST_KEY] = "  example.com , test.com ";
    const domainAllowList = getDomainAllowList();
    expect(domainAllowList).toEqual(["example.com", "test.com"]);
  });
});

In this updated version:

  1. The domainAllowList is explicitly reset in the beforeEach block to avoid state leakage between tests.
  2. The test that checks for an empty array now correctly deletes the environment variable instead of setting it to an empty string. This ensures that getDomainAllowList returns an empty array when the environment variable is not set.

Teste de Autenticação Vitest created at Tue Jan 20 1970 20:34:02 GMT+0000 (Coordinated Universal Time)

Related Chats

Tests untuk initializeNewThread 0.506

Tests Funktion sortByMessageId 0.500

Conditional UserProvider Wrapper 0.405

Teste de Unidade Vite 0.389

New chat 0.384

Rust Enum for Strings 0.383

Fix TS Error: useAtom 0.381

Config Environment Variables Override 0.374

Async Handler Type Declaration 0.372