Update test with module wrapper

This commit is contained in:
James M. Greene
2023-03-06 22:08:01 -06:00
parent 6f75b9951c
commit 93872af9c4
+170 -168
View File
@@ -44,7 +44,7 @@ describe('with variables missing', () => {
}) })
}) })
describe('create', () => { describe('Deployment', () => {
beforeAll(() => { beforeAll(() => {
process.env.ACTIONS_RUNTIME_URL = 'http://my-url/' process.env.ACTIONS_RUNTIME_URL = 'http://my-url/'
process.env.GITHUB_RUN_ID = '123' process.env.GITHUB_RUN_ID = '123'
@@ -81,194 +81,196 @@ describe('create', () => {
jest.spyOn(core, 'debug').mockImplementation(jest.fn()) jest.spyOn(core, 'debug').mockImplementation(jest.fn())
}) })
afterEach(() => { describe('#create', () => {
// Remove mock for `core.getInput('preview')` afterEach(() => {
delete process.env.INPUT_PREVIEW // Remove mock for `core.getInput('preview')`
}) delete process.env.INPUT_PREVIEW
})
it('can successfully create a deployment', async () => { it('can successfully create a deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version' process.env.GITHUB_SHA = 'valid-build-version'
const artifactExchangeScope = nock(`http://my-url`) const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview') .get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { .reply(200, {
value: [ value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' }, { url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' } { url: 'https://fake-artifact.com', name: 'github-pages' }
] ]
}) })
const createDeploymentScope = nock('https://api.github.com') const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, { .post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent', artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA, pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt oidc_token: fakeJwt
}) })
.reply(200, { .reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`, status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome' page_url: 'https://actions.github.io/is-awesome'
}) })
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt) core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
// Create the deployment // Create the deployment
const deployment = new Deployment() const deployment = new Deployment()
await deployment.create(fakeJwt) await deployment.create(fakeJwt)
expect(core.setFailed).not.toHaveBeenCalled() expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenLastCalledWith( expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`)) expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('can successfully create a preview deployment', async () => {
process.env.GITHUB_SHA = 'valid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
})
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt,
preview: true
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome',
preview_url: 'https://actions.drafts.github.io/is-awesome'
})
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
// Return `"true"` for `core.getInput("preview")`
process.env.INPUT_PREVIEW = 'true'
// Create the deployment
const deployment = new Deployment()
await deployment.create(fakeJwt)
expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('reports errors with failed deployments', async () => {
process.env.GITHUB_SHA = 'invalid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
artifact_url: 'https://invalid-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA
})
.reply(400, { message: 'Bad request' })
// Create the deployment
const deployment = new Deployment()
await expect(deployment.create()).rejects.toEqual(
new Error(
`Failed to create deployment (status: 400) with build version ${process.env.GITHUB_SHA}. Responded with: Bad request`
) )
)
artifactExchangeScope.done() artifactExchangeScope.done()
createDeploymentScope.done() createDeploymentScope.done()
})
})
describe('check', () => {
beforeAll(() => {
process.env.ACTIONS_RUNTIME_URL = 'http://my-url/'
process.env.GITHUB_RUN_ID = '123'
process.env.ACTIONS_RUNTIME_TOKEN = 'a-token'
process.env.GITHUB_REPOSITORY = 'actions/is-awesome'
process.env.GITHUB_TOKEN = 'gha-token'
process.env.GITHUB_SHA = '123abc'
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'
jest.spyOn(core, 'setOutput').mockImplementation(param => {
return param
}) })
jest.spyOn(core, 'setFailed').mockImplementation(param => { it('can successfully create a preview deployment', async () => {
return param process.env.GITHUB_SHA = 'valid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
})
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt,
preview: true
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome',
preview_url: 'https://actions.drafts.github.io/is-awesome'
})
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
// Return `"true"` for `core.getInput("preview")`
process.env.INPUT_PREVIEW = 'true'
// Create the deployment
const deployment = new Deployment()
await deployment.create(fakeJwt)
expect(core.setFailed).not.toHaveBeenCalled()
expect(core.info).toHaveBeenLastCalledWith(
expect.stringMatching(new RegExp(`^Created deployment for ${process.env.GITHUB_SHA}`))
)
artifactExchangeScope.done()
createDeploymentScope.done()
})
it('reports errors with failed deployments', async () => {
process.env.GITHUB_SHA = 'invalid-build-version'
const artifactExchangeScope = nock(`http://my-url`)
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
.reply(200, { value: [{ url: 'https://invalid-artifact.com', name: 'github-pages' }] })
const createDeploymentScope = nock('https://api.github.com')
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
artifact_url: 'https://invalid-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA
})
.reply(400, { message: 'Bad request' })
// Create the deployment
const deployment = new Deployment()
await expect(deployment.create()).rejects.toEqual(
new Error(
`Failed to create deployment (status: 400) with build version ${process.env.GITHUB_SHA}. Responded with: Bad request`
)
)
artifactExchangeScope.done()
createDeploymentScope.done()
}) })
// Mock error/warning/info/debug
jest.spyOn(core, 'error').mockImplementation(jest.fn())
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
jest.spyOn(core, 'info').mockImplementation(jest.fn())
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
}) })
it('sets output to success when deployment is successful', async () => { describe('#check', () => {
process.env.GITHUB_SHA = 'valid-build-version' beforeAll(() => {
process.env.ACTIONS_RUNTIME_URL = 'http://my-url/'
process.env.GITHUB_RUN_ID = '123'
process.env.ACTIONS_RUNTIME_TOKEN = 'a-token'
process.env.GITHUB_REPOSITORY = 'actions/is-awesome'
process.env.GITHUB_TOKEN = 'gha-token'
process.env.GITHUB_SHA = '123abc'
process.env.GITHUB_ACTOR = 'monalisa'
process.env.GITHUB_ACTION = '__monalisa/octocat'
process.env.GITHUB_ACTION_PATH = 'something'
process.env.ARTIFACT_NAME = 'github-pages'
const artifactExchangeScope = nock(`http://my-url`) jest.spyOn(core, 'setOutput').mockImplementation(param => {
.get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview') return param
.reply(200, {
value: [
{ url: 'https://another-artifact.com', name: 'another-artifact' },
{ url: 'https://fake-artifact.com', name: 'github-pages' }
]
}) })
const createDeploymentScope = nock('https://api.github.com') jest.spyOn(core, 'setFailed').mockImplementation(param => {
.post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, { return param
artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome'
}) })
// Mock error/warning/info/debug
jest.spyOn(core, 'error').mockImplementation(jest.fn())
jest.spyOn(core, 'warning').mockImplementation(jest.fn())
jest.spyOn(core, 'info').mockImplementation(jest.fn())
jest.spyOn(core, 'debug').mockImplementation(jest.fn())
})
const deploymentStatusScope = nock('https://api.github.com') it('sets output to success when deployment is successful', async () => {
.get(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`) process.env.GITHUB_SHA = 'valid-build-version'
.reply(200, {
status: 'succeed'
})
core.getIDToken = jest.fn().mockResolvedValue(fakeJwt) const artifactExchangeScope = nock(`http://my-url`)
core.GetInput = jest.fn(input => { .get('/_apis/pipelines/workflows/123/artifacts?api-version=6.0-preview')
switch (input) { .reply(200, {
case 'timeout': value: [
return 10 * 1000 { url: 'https://another-artifact.com', name: 'another-artifact' },
case 'reporting_interval': { url: 'https://fake-artifact.com', name: 'github-pages' }
return 0 ]
} })
})
// Create the deployment const createDeploymentScope = nock('https://api.github.com')
const deployment = new Deployment() .post(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment`, {
await deployment.create(fakeJwt) artifact_url: 'https://fake-artifact.com&%24expand=SignedContent',
await deployment.check() pages_build_version: process.env.GITHUB_SHA,
oidc_token: fakeJwt
})
.reply(200, {
status_url: `https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`,
page_url: 'https://actions.github.io/is-awesome'
})
expect(core.setOutput).toBeCalledWith('status', 'succeed') const deploymentStatusScope = nock('https://api.github.com')
expect(core.info).toHaveBeenLastCalledWith('Reported success!') .get(`/repos/${process.env.GITHUB_REPOSITORY}/pages/deployment/status/${process.env.GITHUB_SHA}`)
.reply(200, {
status: 'succeed'
})
artifactExchangeScope.done() core.getIDToken = jest.fn().mockResolvedValue(fakeJwt)
createDeploymentScope.done() core.GetInput = jest.fn(input => {
deploymentStatusScope.done() switch (input) {
case 'timeout':
return 10 * 1000
case 'reporting_interval':
return 0
}
})
// Create the deployment
const deployment = new Deployment()
await deployment.create(fakeJwt)
await deployment.check()
expect(core.setOutput).toBeCalledWith('status', 'succeed')
expect(core.info).toHaveBeenLastCalledWith('Reported success!')
artifactExchangeScope.done()
createDeploymentScope.done()
deploymentStatusScope.done()
})
}) })
}) })