Skip to content
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
4 changes: 2 additions & 2 deletions apps/www/components/page/copy-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { CopyButton } from "./copy-button";

// Mock the useToast hook
Expand Down
4 changes: 2 additions & 2 deletions apps/www/components/page/sail-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, render, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { SailButton } from "./sail-button";

// Mock Next.js router
Expand Down
4 changes: 2 additions & 2 deletions apps/www/components/page/star-us-button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { StarUsButton } from "./star-us-button";

// Mock fetch to avoid real API calls
Expand Down
4 changes: 2 additions & 2 deletions apps/www/components/page/video-demo.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { VideoDemo } from "./video-demo";

// Mock window.open
Expand Down
4 changes: 2 additions & 2 deletions apps/www/components/ui/heading.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, render, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { Heading } from "./heading";

// Mock the useIsMobile hook
Expand Down
2 changes: 1 addition & 1 deletion apps/www/hooks/use-is-mobile.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";
import { useIsMobile } from "./use-is-mobile";

// Mock window.matchMedia
Expand Down
31 changes: 15 additions & 16 deletions apps/www/lib/utils/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { breakDownGithubUrl, getLinkTitleAndHref } from "./github";

describe("github utilities", () => {
const originalEnv = process.env;

beforeEach(() => {
vi.resetModules();
process.env = { ...originalEnv };
});

afterEach(() => {
process.env = originalEnv;
});

describe("breakDownGithubUrl", () => {
Expand All @@ -35,7 +28,7 @@ describe("github utilities", () => {
});

it("should use environment variable when no URL provided", () => {
process.env.NEXT_PUBLIC_GITHUB_URL = "https://github.com/example/repo";
vi.stubEnv("NEXT_PUBLIC_GITHUB_URL", "https://github.com/example/repo");

const result = breakDownGithubUrl();

Expand All @@ -47,8 +40,11 @@ describe("github utilities", () => {
});

it("should use custom branch from environment variable", () => {
process.env.NEXT_PUBLIC_GITHUB_URL = "https://github.com/yamcodes/arkenv";
process.env.NEXT_PUBLIC_GITHUB_BRANCH = "develop";
vi.stubEnv(
"NEXT_PUBLIC_GITHUB_URL",
"https://github.com/yamcodes/arkenv",
);
vi.stubEnv("NEXT_PUBLIC_GITHUB_BRANCH", "develop");

const result = breakDownGithubUrl();

Expand All @@ -60,7 +56,7 @@ describe("github utilities", () => {
});

it("should throw error when no URL is configured", () => {
delete process.env.NEXT_PUBLIC_GITHUB_URL;
vi.unstubAllEnvs();

expect(() => breakDownGithubUrl()).toThrow(
"NEXT_PUBLIC_GITHUB_URL is not configured",
Expand Down Expand Up @@ -121,7 +117,7 @@ describe("github utilities", () => {
});

it("should use environment variable when no URL provided", () => {
process.env.NEXT_PUBLIC_GITHUB_URL = "https://github.com/example/repo";
vi.stubEnv("NEXT_PUBLIC_GITHUB_URL", "https://github.com/example/repo");

const result = getLinkTitleAndHref("test.md");

Expand All @@ -132,8 +128,11 @@ describe("github utilities", () => {
});

it("should use custom branch from environment variable", () => {
process.env.NEXT_PUBLIC_GITHUB_URL = "https://github.com/yamcodes/arkenv";
process.env.NEXT_PUBLIC_GITHUB_BRANCH = "develop";
vi.stubEnv(
"NEXT_PUBLIC_GITHUB_URL",
"https://github.com/yamcodes/arkenv",
);
vi.stubEnv("NEXT_PUBLIC_GITHUB_BRANCH", "develop");

const result = getLinkTitleAndHref("package.json");

Expand All @@ -144,7 +143,7 @@ describe("github utilities", () => {
});

it("should throw error when no URL is configured", () => {
delete process.env.NEXT_PUBLIC_GITHUB_URL;
vi.unstubAllEnvs();

expect(() => getLinkTitleAndHref("test.md")).toThrow(
"NEXT_PUBLIC_GITHUB_URL is not configured",
Expand Down
1 change: 1 addition & 0 deletions apps/www/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export default defineProject({
environment: "jsdom",
setupFiles: ["./tests/setup.ts"],
restoreMocks: true,
unstubEnvs: true,
},
});
28 changes: 6 additions & 22 deletions packages/arkenv/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import {
afterEach,
beforeEach,
describe,
expect,
expectTypeOf,
it,
vi,
} from "vitest";
import { afterEach, describe, expect, expectTypeOf, it, vi } from "vitest";
import arkenv, { createEnv } from "./index";

// Capture snapshot of process.env at module level
const originalEnv = { ...process.env };

describe("index.ts exports", () => {
beforeEach(() => {
// Replace with a clean environment for each test
process.env = {};
});

afterEach(() => {
// Restore mocks and reset process.env to captured snapshot
// Restore mocks and unstub all environment variables
vi.restoreAllMocks();
process.env = { ...originalEnv };
vi.unstubAllEnvs();
});

it("should export createEnv as default export", () => {
Expand All @@ -40,7 +24,7 @@ describe("index.ts exports", () => {

it("should work with default import", () => {
// Set test environment variable
process.env.TEST_DEFAULT_IMPORT = "test-value";
vi.stubEnv("TEST_DEFAULT_IMPORT", "test-value");

const env = arkenv({
TEST_DEFAULT_IMPORT: "string",
Expand All @@ -52,7 +36,7 @@ describe("index.ts exports", () => {

it("should work with named import", () => {
// Set test environment variable
process.env.TEST_NAMED_IMPORT = "test-value";
vi.stubEnv("TEST_NAMED_IMPORT", "test-value");

const env = createEnv({
TEST_NAMED_IMPORT: "string",
Expand Down Expand Up @@ -80,7 +64,7 @@ describe("index.ts exports", () => {

it("should have same behavior for both default and named imports", () => {
// Set test environment variable
process.env.COMPARISON_TEST = "same-value";
vi.stubEnv("COMPARISON_TEST", "same-value");

const envFromDefault = arkenv({
COMPARISON_TEST: "string",
Expand Down
7 changes: 7 additions & 0 deletions packages/arkenv/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineProject } from "vitest/config";

export default defineProject({
test: {
name: "arkenv",
},
});
1 change: 1 addition & 0 deletions packages/vite-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"tsup": "^8.5.0",
"typescript": "^5.9.3",
"vite": "^7.1.9",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.2.4"
},
"peerDependencies": {
Expand Down
17 changes: 8 additions & 9 deletions packages/vite-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ vi.mock("arkenv", () => ({

import arkenvPlugin from "./index.js";

// Capture snapshot of process.env at module level
const ORIGINAL_ENV = { ...process.env };

const fixturesDir = join(__dirname, "__fixtures__");

// Get the mocked functions
Expand All @@ -28,14 +25,14 @@ for (const name of readdirSync(fixturesDir)) {

describe(`Fixture: ${name}`, () => {
beforeEach(() => {
// Clean environment start and mock cleanup
process.env = { ...ORIGINAL_ENV };
// Clear environment variables and mock cleanup
vi.unstubAllEnvs();
mockCreateEnv.mockClear();
});

afterEach(() => {
// Complete cleanup: restore environment and reset mocks
process.env = { ...ORIGINAL_ENV };
vi.unstubAllEnvs();
mockCreateEnv.mockReset();
});

Expand All @@ -44,7 +41,9 @@ for (const name of readdirSync(fixturesDir)) {

// Set up environment variables from the fixture
if (config.envVars) {
Object.assign(process.env, config.envVars);
for (const [key, value] of Object.entries(config.envVars)) {
vi.stubEnv(key, value);
}
}

await expect(
Expand Down Expand Up @@ -77,12 +76,12 @@ for (const name of readdirSync(fixturesDir)) {
// Unit tests for plugin functionality
describe("Plugin Unit Tests", () => {
beforeEach(() => {
process.env = { ...ORIGINAL_ENV };
vi.unstubAllEnvs();
mockCreateEnv.mockClear();
});

afterEach(() => {
process.env = { ...ORIGINAL_ENV };
vi.unstubAllEnvs();
mockCreateEnv.mockReset();
});

Expand Down
10 changes: 2 additions & 8 deletions packages/vite-plugin/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import path from "node:path";
import { defineConfig } from "vitest/config";
import { defineProject } from "vitest/config";

export default defineConfig({
export default defineProject({
test: {
name: "@arkenv/vite-plugin",
environment: "node",
coverage: {
provider: "v8",
reporter: ["text", "json", "html"],
exclude: ["node_modules/", "dist/", "**/*.test.ts", "**/*.config.ts"],
},
},
resolve: {
alias: {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export default defineConfig({
"**/public/**",
],
},
unstubEnvs: true,
},
});
Loading