Skip to content

Commit

Permalink
refactor: ♻️ (web) テスト環境では環境変数のバリデータに、必ずサーバー環境用のものを使うように設定した。 (#4)
Browse files Browse the repository at this point in the history
- グローバル変数`window`が存在するVitest + jsdomのテスト環境で、サーバー側で実行する関数のテストも行うため。
  • Loading branch information
ReoHakase committed May 23, 2024
1 parent 5a2d63d commit fb77b63
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions apps/web/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { createEnv } from '@t3-oss/env-nextjs';
import { z } from 'zod';

/**
* 環境がサーバー側かクライアント側かを判定する式
* クライアントコンポーネント以外のテストも実行するため、Vitest環境では常にtrueを返す
* @default typeof window === "undefined"
* @see https://github.com/t3-oss/t3-env/blob/main/packages/core/src/index.ts
*/
const isServer = typeof window === 'undefined' || process.env.NODE_ENV === 'test';

export const env = createEnv({
isServer,
server: {
BASE_URL: z.string().optional().default('resas.reoiam.dev'),
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
Expand Down

0 comments on commit fb77b63

Please sign in to comment.