Skip to content

Commit

Permalink
fix: npm script awareness, TS fixes, test env refactors (#659)
Browse files Browse the repository at this point in the history
* refactor: rename internal test env vars

- All internal env values (i.e. for testing CI or testing GitHub Action creation) are prefixed with `TEST_RDME_`

- Renamed our `NODE_ENV` for our test from `test` to `rdme-test`

- Created a little `isTest` helper to check whether we're running tests

* chore(deps-dev): install keyv

This isn't used, but we're seeing TS errors when users try to run `tsc` in a repo that contains `rdme` as a dep. Weirdly enough, installing this package fixes it.

See: https://stackoverflow.com/q/74141618

* ci(prettier): only scan md files

ESLint already covers JS/TS, so the duplicative check is not necessary

* test: reorganize tests so they don't leak console warnings

* feat: don't run createGHA workflow if invoked via npm lifecycle
  • Loading branch information
kanadgupta authored Nov 2, 2022
1 parent 0278aa2 commit 05c34da
Show file tree
Hide file tree
Showing 27 changed files with 143 additions and 173 deletions.
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ describe('rdme categories:create', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(categoriesCreate.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no title provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/categories/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ describe('rdme categories', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(categories.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should return all categories for a single page', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/changelogs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ describe('rdme changelogs', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(changelogs.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no path provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/changelogs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ describe('rdme changelogs (single)', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(changelogs.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no file path provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/custompages/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ describe('rdme custompages', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(custompages.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no path provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/custompages/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ describe('rdme custompages (single)', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(custompages.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no file path provided', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ describe('rdme docs:edit', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should log deprecation notice', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(docsEdit.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
expect(getWarningCommandOutput()).toMatch('is now deprecated');
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('rdme docs', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no path provided', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/prune.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe('rdme docs:prune', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(docsPrune.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no folder provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/docs/single.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ describe('rdme docs (single)', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(docs.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no file path provided', async () => {
Expand Down
12 changes: 6 additions & 6 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ describe('rdme openapi', () => {

describe('CI spec selection', () => {
beforeEach(() => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
});

afterEach(() => {
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error out if multiple possible spec matches were found', () => {
Expand Down Expand Up @@ -737,11 +737,11 @@ describe('rdme openapi', () => {

describe('CI version handling', () => {
beforeEach(() => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
});

afterEach(() => {
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should omit version header in CI environment', async () => {
Expand Down Expand Up @@ -842,11 +842,11 @@ describe('rdme openapi', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(openapi.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if `--create` and `--update` flags are passed simultaneously', () => {
Expand Down
8 changes: 4 additions & 4 deletions __tests__/cmds/openapi/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,18 @@ describe('rdme openapi:validate', () => {

describe('CI tests', () => {
beforeEach(() => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
});

afterEach(() => {
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should successfully validate prompt and not run GHA onboarding', async () => {
process.env.TEST_CREATEGHA = 'true';
process.env.TEST_RDME_CREATEGHA = 'true';
const spec = '__tests__/__fixtures__/petstore-simple-weird-version.json';
await expect(validate.run({ spec })).resolves.toBe(chalk.green(`${spec} is a valid OpenAPI API definition!`));
delete process.env.TEST_CREATEGHA;
delete process.env.TEST_RDME_CREATEGHA;
});

it('should fail if user attempts to pass `--github` flag in CI environment', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('rdme versions:create', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(createVersion.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should error if no version provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('rdme versions:delete', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(deleteVersion.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should delete a specific version', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ describe('rdme versions', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(versions.run({})).rejects.toStrictEqual(new Error('No project API key provided. Please use `--key`.'));
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should make a request to get a list of existing versions', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cmds/versions/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('rdme versions:update', () => {
});

it('should error in CI if no API key provided', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(updateVersion.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
);
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should update a specific version object', async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/helpers/get-gha-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function before(writeFileSyncCb) {
// no need to construct everything
global.Date = jest.fn(() => DATE_TO_USE);

process.env.TEST_CREATEGHA = 'true';
process.env.TEST_RDME_CREATEGHA = 'true';

const spy = jest.spyOn(getPkgVersion, 'getPkgVersion');
spy.mockReturnValue(Promise.resolve('7.8.9'));
Expand All @@ -44,7 +44,7 @@ export function before(writeFileSyncCb) {
*/
export function after() {
configstore.clear();
delete process.env.TEST_CREATEGHA;
delete process.env.TEST_RDME_CREATEGHA;
jest.clearAllMocks();
process.chdir(testWorkingDir);
}
38 changes: 20 additions & 18 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,17 @@ describe('cli', () => {
await expect(cli([])).resolves.toContain('OpenAPI / Swagger');
});

it('should add stored apiKey to opts', async () => {
expect.assertions(1);
const key = '123456';
const version = '1.0.0';
conf.set('apiKey', key);

const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });

await expect(cli(['docs', `--version=${version}`])).rejects.toStrictEqual(
new Error('No path provided. Usage `rdme docs <path> [options]`.')
);

conf.clear();
versionMock.done();
});

describe('logged-in user notifications', () => {
describe('stored API key', () => {
let consoleInfoSpy;
const key = '123456';
const getCommandOutput = () => {
return [consoleInfoSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
};

beforeEach(() => {
conf.set('email', '[email protected]');
conf.set('project', 'owlbert');
conf.set('apiKey', '123456');
conf.set('apiKey', key);
consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
});

Expand All @@ -126,6 +111,23 @@ describe('cli', () => {
conf.clear();
});

it('should add stored apiKey to opts', async () => {
expect.assertions(1);
const version = '1.0.0';

const versionMock = getAPIMock()
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version });

await expect(cli(['docs', `--version=${version}`])).rejects.toStrictEqual(
new Error('No path provided. Usage `rdme docs <path> [options]`.')
);

conf.clear();
versionMock.done();
});

it('should inform a logged in user which project is being updated', async () => {
await expect(cli(['openapi', '--create', '--update'])).rejects.toThrow(
'The `--create` and `--update` options cannot be used simultaneously. Please use one or the other!'
Expand Down
12 changes: 10 additions & 2 deletions __tests__/lib/createGHA.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,19 @@ describe('#createGHA', () => {
});

it('should not run if in a CI environment', async () => {
process.env.TEST_CI = 'true';
process.env.TEST_RDME_CI = 'true';
await expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!');
// asserts that git commands aren't run in CI
expect(git.checkIsRepo).not.toHaveBeenCalled();
delete process.env.TEST_CI;
delete process.env.TEST_RDME_CI;
});

it('should not run if in an npm lifecycle', async () => {
process.env.TEST_RDME_NPM_SCRIPT = 'true';
await expect(createGHA('success!', cmd, command.args, opts)).resolves.toBe('success!');
// asserts that git commands aren't run in CI
expect(git.checkIsRepo).not.toHaveBeenCalled();
delete process.env.TEST_RDME_NPM_SCRIPT;
});

it('should not run if repo only contains non-GitHub remotes', () => {
Expand Down
4 changes: 4 additions & 0 deletions __tests__/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
// colorization here for all tests.
// https://github.com/chalk/supports-color/issues/106
process.env.FORCE_COLOR = 0;

// Sets our test NODE_ENV to a custom value in case of false positives
// if someone is using this tool in a testing environment
process.env.NODE_ENV = 'rdme-test';
File renamed without changes.
Loading

0 comments on commit 05c34da

Please sign in to comment.