Skip to content

Commit

Permalink
test: refactor need for process.env 'test' (#383)
Browse files Browse the repository at this point in the history
* test: refactor need for process.env 'test'

* test: create use faketimers

* chore: build

* chore: changeset

* chore: changeset
  • Loading branch information
andykenward authored Jun 23, 2024
1 parent 7de9ee1 commit 4a04751
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-cups-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'github-actions-cloudflare-pages': patch
---

test: remove need for checking process.env in test environment.
6 changes: 6 additions & 0 deletions __tests__/common/cloudflare/deployment/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ describe('createCloudflareDeployment', () => {
let mockApi: MockApi

beforeEach(() => {
vi.useFakeTimers({
shouldAdvanceTime: true
})
mockApi = setMockApi()
})
afterEach(async () => {
mockApi.mockAgent.assertNoPendingInterceptors()
await mockApi.mockAgent.close()
vi.mocked(execAsync).mockReset()
vi.runOnlyPendingTimers()
vi.useRealTimers()
})

test('handles thrown error from wrangler deploy', async () => {
Expand Down Expand Up @@ -130,6 +135,7 @@ describe('createCloudflareDeployment', () => {
directory: 'mock-directory',
workingDirectory: 'mock-working-directory'
})
// vi.advanceTimersByTime(2000)

expect(execAsync).toHaveBeenCalledWith(
`npx wrangler@${process.env.npm_package_dependencies_wrangler} pages deploy mock-directory --project-name=mock-cloudflare-project-name --branch=mock-github-head-ref --commit-dirty=true --commit-hash=mock-github-sha`,
Expand Down
12 changes: 8 additions & 4 deletions __tests__/common/github/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {describe, expect, test} from 'vitest'

import {useContext} from '@/common/github/context.js'
import {beforeEach, describe, expect, test, vi} from 'vitest'

describe('getGitHubContext', () => {
test('returns eventName ', () => {
beforeEach(() => {
vi.resetModules()
})

test('returns eventName ', async () => {
expect.assertions(8)

const {useContext} = await import('@/common/github/context.js')

const {repo, event, branch, sha, graphqlEndpoint, ref} = useContext()

/** Repo */
Expand Down
45 changes: 22 additions & 23 deletions __tests__/common/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@ import {beforeEach, describe, expect, test, vi} from 'vitest'

import {stubInputEnv} from '@/tests/helpers/inputs.js'

import {useCommonInputs} from '@/common/inputs.js'
import {
INPUT_KEY_CLOUDFLARE_API_TOKEN,
INPUT_KEY_GITHUB_ENVIRONMENT,
INPUT_KEY_GITHUB_TOKEN
} from '@/input-keys'

const setup = async () => {
return await import('@/common/inputs.js')
}

describe('common', () => {
describe('inputs', () => {
beforeEach(() => {
vi.resetModules()
vi.unstubAllEnvs()
})

test('should error when missing inputs', () => {
test('should error when missing inputs', async () => {
expect.assertions(3)
const {useCommonInputs} = await setup()

expect(() => useCommonInputs()).toThrow(
/input required and not supplied: cloudflare-api-token/i
Expand All @@ -31,40 +36,34 @@ describe('common', () => {
expect(() => useCommonInputs()).not.toThrow()
})

test('returns correct values', () => {
test('returns correct values', async () => {
expect.assertions(1)

stubInputEnv(INPUT_KEY_CLOUDFLARE_API_TOKEN)
stubInputEnv(INPUT_KEY_GITHUB_TOKEN)
stubInputEnv(INPUT_KEY_GITHUB_ENVIRONMENT)

const inputs = useCommonInputs()
const {useCommonInputs} = await setup()

expect(inputs).toMatchInlineSnapshot(`
{
"cloudflareApiToken": "mock-cloudflare-api-token",
"gitHubApiToken": "mock-github-token",
"gitHubEnvironment": "mock-github-environment",
}
`)
expect(useCommonInputs()).toStrictEqual({
cloudflareApiToken: 'mock-cloudflare-api-token',
gitHubApiToken: 'mock-github-token',
gitHubEnvironment: 'mock-github-environment'
})
})

test(`returns undefined for optional ${INPUT_KEY_GITHUB_ENVIRONMENT}`, () => {
expect.assertions(2)
test(`returns undefined for optional ${INPUT_KEY_GITHUB_ENVIRONMENT}`, async () => {
expect.assertions(1)
stubInputEnv(INPUT_KEY_CLOUDFLARE_API_TOKEN)
stubInputEnv(INPUT_KEY_GITHUB_TOKEN)

const inputs = useCommonInputs()

expect(inputs.gitHubEnvironment).toBeUndefined()
const {useCommonInputs} = await setup()

expect(inputs).toMatchInlineSnapshot(`
{
"cloudflareApiToken": "mock-cloudflare-api-token",
"gitHubApiToken": "mock-github-token",
"gitHubEnvironment": undefined,
}
`)
expect(useCommonInputs()).toStrictEqual({
cloudflareApiToken: 'mock-cloudflare-api-token',
gitHubApiToken: 'mock-github-token',
gitHubEnvironment: undefined
})
})
})
})
32 changes: 20 additions & 12 deletions __tests__/deploy/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,36 @@ import {beforeEach, describe, expect, test, vi} from 'vitest'

import {stubRequiredInputEnv} from '@/tests/helpers/inputs.js'

import {useInputs} from '@/deploy/inputs.js'

const setup = async () => {
return await import('@/deploy/inputs.js')
}
describe('deploy', () => {
describe('inputs', () => {
beforeEach(() => {
vi.resetModules()
vi.unstubAllEnvs()
})

test('returns correct values', () => {
test('returns correct values', async () => {
expect.assertions(1)

stubRequiredInputEnv()
const inputs = useInputs()
const {useInputs} = await setup()

expect(useInputs()).toStrictEqual({
cloudflareAccountId: 'mock-cloudflare-account-id',
cloudflareProjectName: 'mock-cloudflare-project-name',
directory: 'mock-directory',
workingDirectory: '.'
})
})

expect(inputs).toMatchInlineSnapshot(`
{
"cloudflareAccountId": "mock-cloudflare-account-id",
"cloudflareProjectName": "mock-cloudflare-project-name",
"directory": "mock-directory",
"workingDirectory": ".",
}
`)
test('throws error', async () => {
expect.assertions(1)
const {useInputs} = await setup()
expect(() => useInputs()).toThrow(
'Input required and not supplied: cloudflare-account-id'
)
})
})
})
Loading

0 comments on commit 4a04751

Please sign in to comment.