Skip to content

Commit

Permalink
test: fix tests in GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Dec 10, 2024
1 parent f9fc29c commit 8ebc8dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
16 changes: 6 additions & 10 deletions __tests__/commands/openapi/upload.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import nock from 'nock';
import prompts from 'prompts';
import slugify from 'slugify';
import { describe, beforeAll, beforeEach, afterEach, it, expect, vi } from 'vitest';
import { describe, beforeAll, beforeEach, afterEach, it, expect } from 'vitest';

import Command from '../../../src/commands/openapi/upload.js';
import petstore from '../../__fixtures__/petstore-simple-weird-version.json' with { type: 'json' };
import { getAPIv2Mock } from '../../helpers/get-api-mock.js';
import { getAPIv2Mock, getAPIv2MockForGHA } from '../../helpers/get-api-mock.js';
import { runCommand, type OclifOutput } from '../../helpers/oclif.js';
import { after, before } from '../../helpers/setup-gha-env.js';

const key = 'rdme_123';
const version = '1.0.0';
Expand Down Expand Up @@ -226,16 +227,12 @@ describe('rdme openapi upload', () => {
});

describe('and the command is being run in a CI environment', () => {
beforeEach(() => {
vi.stubEnv('TEST_RDME_CI', 'true');
});
beforeEach(before);

afterEach(() => {
vi.unstubAllEnvs();
});
afterEach(after);

it('should overwrite an existing API definition without asking for confirmation', async () => {
const mock = getAPIv2Mock({ authorization: `Bearer ${key}` })
const mock = getAPIv2MockForGHA({ authorization: `Bearer ${key}` })
.get(`/versions/${version}/apis`)
.reply(200, { data: [{ filename: slugifiedFilename }] })
.put(`/versions/1.0.0/apis/${slugifiedFilename}`, body =>
Expand All @@ -249,7 +246,6 @@ describe('rdme openapi upload', () => {
});

const result = await run(['--version', version, filename, '--key', key]);
expect(result).toStrictEqual({});
expect(result.stdout).toContain('was successfully updated in ReadMe!');

mock.done();
Expand Down
11 changes: 11 additions & 0 deletions __tests__/helpers/get-api-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ export function getAPIv2Mock(reqHeaders: nock.Options['reqheaders'] = {}) {
},
});
}

/**
* Variant of `getAPIv2Mock` for mocking a GitHub Actions environment.
*/
export function getAPIv2MockForGHA(reqHeaders: nock.Options['reqheaders'] = {}) {
return getAPIv2Mock({
'User-Agent': ua => ua.startsWith(`rdme-github/${mockVersion}`),
'x-readme-source': 'cli-gh',
...reqHeaders,
});
}

0 comments on commit 8ebc8dd

Please sign in to comment.