From 3cc1a5d818058be17e4a2329e82233a2475debc1 Mon Sep 17 00:00:00 2001 From: Andy Kenward <4893048+andykenward@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:25:16 +0000 Subject: [PATCH] fix: vitest cannot mock --- __tests__/helpers/env.ts | 8 ++++---- __tests__/helpers/inputs.ts | 2 +- input-keys.ts | 16 ++++++++++++++++ src/inputs.ts | 11 ++--------- 4 files changed, 23 insertions(+), 14 deletions(-) create mode 100644 input-keys.ts diff --git a/__tests__/helpers/env.ts b/__tests__/helpers/env.ts index 3fafe2e8..f5f42441 100644 --- a/__tests__/helpers/env.ts +++ b/__tests__/helpers/env.ts @@ -1,7 +1,5 @@ import {vi} from 'vitest' -import {raise} from '@/src/utils.js' - export const TEST_ENV_VARS: NodeJS.ProcessEnv = { GITHUB_HEAD_REF: 'mock-github-head-ref', GITHUB_REF: 'refs/heads/mock-github-ref', @@ -17,8 +15,10 @@ export const TEST_ENV_VARS: NodeJS.ProcessEnv = { export const stubTestEnvVars = () => { for (const key in TEST_ENV_VARS) { - const value = - TEST_ENV_VARS[key] ?? raise(`Missing TEST_ENV_VARS value for key: ${key}`) + const value = TEST_ENV_VARS[key] + if (!value) { + throw new Error(`Missing TEST_ENV_VARS value for key: ${key}`) + } vi.stubEnv(key, value) } } diff --git a/__tests__/helpers/inputs.ts b/__tests__/helpers/inputs.ts index 16dd81cc..98956897 100644 --- a/__tests__/helpers/inputs.ts +++ b/__tests__/helpers/inputs.ts @@ -1,6 +1,6 @@ import {vi} from 'vitest' -import {INPUT_KEYS_REQUIRED} from '@/src/inputs.js' +import {INPUT_KEYS_REQUIRED} from '../../input-keys.js' const INPUT_KEY = `INPUT_` diff --git a/input-keys.ts b/input-keys.ts new file mode 100644 index 00000000..51377dcd --- /dev/null +++ b/input-keys.ts @@ -0,0 +1,16 @@ +export const INPUT_KEY_CLOUDFLARE_ACCOUNT_ID = 'cloudflare-account-id' as const +export const INPUT_KEY_CLOUDFLARE_API_TOKEN = 'cloudflare-api-token' as const +export const INPUT_KEY_CLOUDFLARE_PROJECT_NAME = + 'cloudflare-project-name' as const +export const INPUT_KEY_DIRECTORY = 'directory' as const +export const INPUT_KEY_GITHUB_ENVIRONMENT = 'github-environment' as const +export const INPUT_KEY_GITHUB_TOKEN = 'github-token' as const + +export const INPUT_KEYS_REQUIRED = [ + INPUT_KEY_CLOUDFLARE_ACCOUNT_ID, + INPUT_KEY_CLOUDFLARE_API_TOKEN, + INPUT_KEY_CLOUDFLARE_PROJECT_NAME, + INPUT_KEY_DIRECTORY, + INPUT_KEY_GITHUB_ENVIRONMENT, + INPUT_KEY_GITHUB_TOKEN +] as const diff --git a/src/inputs.ts b/src/inputs.ts index 7798b123..6fe79bc6 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -2,21 +2,14 @@ import type {InputOptions} from '@unlike/github-actions-core' import {getInput} from '@unlike/github-actions-core' -const INPUT_KEY_CLOUDFLARE_ACCOUNT_ID = 'cloudflare-account-id' -const INPUT_KEY_CLOUDFLARE_API_TOKEN = 'cloudflare-api-token' -const INPUT_KEY_CLOUDFLARE_PROJECT_NAME = 'cloudflare-project-name' -const INPUT_KEY_DIRECTORY = 'directory' -const INPUT_KEY_GITHUB_ENVIRONMENT = 'github-environment' -const INPUT_KEY_GITHUB_TOKEN = 'github-token' - -export const INPUT_KEYS_REQUIRED = [ +import { INPUT_KEY_CLOUDFLARE_ACCOUNT_ID, INPUT_KEY_CLOUDFLARE_API_TOKEN, INPUT_KEY_CLOUDFLARE_PROJECT_NAME, INPUT_KEY_DIRECTORY, INPUT_KEY_GITHUB_ENVIRONMENT, INPUT_KEY_GITHUB_TOKEN -] as const +} from '../input-keys.js' const OPTIONS: InputOptions = { required: true