Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(test): moving unit tests over to vitest #857

Merged
merged 25 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f8a4e55
feat(test): moving unit tests over to vitest
erunion Aug 10, 2023
46c7731
fix: prettier issues
erunion Aug 10, 2023
51c71c3
ci: stop testing node 14
erunion Aug 10, 2023
27b7781
fix: schema updater for prettier 3
erunion Aug 10, 2023
1a4c78c
fix: install missing vitest coverage dep
erunion Aug 10, 2023
cf1aef9
chore: better link
kanadgupta Aug 10, 2023
5664dd9
ci: let other node versions run if one fails
kanadgupta Aug 10, 2023
0376bee
ci: try skipping some tests
kanadgupta Aug 11, 2023
b658139
chore(deps): bump eslint config
kanadgupta Aug 14, 2023
0283788
test: don't return in before* hooks
kanadgupta Aug 14, 2023
9c823fe
test: more before* hook fixes
kanadgupta Aug 14, 2023
029d4d4
test: try using stubEnv in a few places
kanadgupta Aug 14, 2023
b1d9175
chore: typo
kanadgupta Aug 14, 2023
dad18a5
Merge branch 'next' into feat/vitest
kanadgupta Aug 21, 2023
f3b9a5d
Merge branch 'next' into feat/vitest
kanadgupta Aug 23, 2023
0071e90
Merge branch 'next' into feat/vitest
kanadgupta Sep 5, 2023
13249fe
Merge branch 'next' into feat/vitest
kanadgupta Sep 5, 2023
1e573ed
chore: rebuild lockfile using npm i
kanadgupta Sep 5, 2023
3298545
chore: wait this happened after i ran "npm i" again?
kanadgupta Sep 5, 2023
22f7904
Merge branch 'next' into feat/vitest
kanadgupta Sep 5, 2023
f3dc0ac
Revert "Merge branch 'next' into feat/vitest"
kanadgupta Sep 5, 2023
ed480ed
fix(vitest): split out tests (#870)
kanadgupta Sep 5, 2023
104c00b
fix: don't nest timers
kanadgupta Sep 5, 2023
bcbe0af
chore: remove unnecessary globals file
kanadgupta Sep 5, 2023
f2ef333
chore: bumping eslint-config
erunion Sep 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
strategy:
matrix:
node-version:
- 14
- 16
- 18
- 20
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ __tests__/
.prettier*
bin/*.js
coverage/
jest.*.js
packages/
vitest.*
5 changes: 4 additions & 1 deletion __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": ["@readme/eslint-config/testing"]
"extends": "@readme/eslint-config/testing/vitest",
"rules": {
"@typescript-eslint/no-empty-function": "off"
}
}
10 changes: 5 additions & 5 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`cli --help should not surface args that are designated as hidden 1`] = `
exports[`cli > --help > should not surface args that are designated as hidden 1`] = `
"
Upload, or resync, your OpenAPI/Swagger definition to ReadMe.

Expand Down Expand Up @@ -47,7 +47,7 @@ Related commands
"
`;

exports[`cli --help should print usage for a given command 1`] = `
exports[`cli > --help > should print usage for a given command 1`] = `
"
Upload, or resync, your OpenAPI/Swagger definition to ReadMe.

Expand Down Expand Up @@ -94,7 +94,7 @@ Related commands
"
`;

exports[`cli --help should print usage for a given command if supplied as \`help <command>\` 1`] = `
exports[`cli > --help > should print usage for a given command if supplied as \`help <command>\` 1`] = `
"
Upload, or resync, your OpenAPI/Swagger definition to ReadMe.

Expand Down Expand Up @@ -141,7 +141,7 @@ Related commands
"
`;

exports[`cli --help should show related commands for a subcommands help menu 1`] = `
exports[`cli > --help > should show related commands for a subcommands help menu 1`] = `
"
List versions available in your project or get a version by SemVer (https://semver.org/).

Expand Down
2 changes: 2 additions & 0 deletions __tests__/bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { exec } from 'child_process';

import { describe, it, expect } from 'vitest';

describe('bin', () => {
it('should show our help screen', async () => {
expect.assertions(1);
Expand Down
4 changes: 3 additions & 1 deletion __tests__/cmds/__snapshots__/whoami.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`rdme whoami > should return the authenticated user 1`] = `"You are currently logged in as [email protected] to the subdomain project."`;

exports[`rdme whoami should return the authenticated user 1`] = `"You are currently logged in as [email protected] to the subdomain project."`;
29 changes: 15 additions & 14 deletions __tests__/cmds/categories/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest';

import CategoriesCreateCommand from '../../../src/cmds/categories/create';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
Expand All @@ -15,7 +16,7 @@ describe('rdme categories:create', () => {
afterEach(() => nock.cleanAll());

it('should prompt for login if no API key provided', async () => {
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
prompts.inject(['this-is-not-an-email', 'password', 'subdomain']);
await expect(categoriesCreate.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand All @@ -24,27 +25,27 @@ describe('rdme categories:create', () => {
it('should error in CI if no API key provided', async () => {
process.env.TEST_RDME_CI = 'true';
await expect(categoriesCreate.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
new Error('No project API key provided. Please use `--key`.'),
);
delete process.env.TEST_RDME_CI;
});

it('should error if no title provided', () => {
return expect(categoriesCreate.run({ key: '123' })).rejects.toStrictEqual(
new Error('No title provided. Usage `rdme categories:create <title> [options]`.')
new Error('No title provided. Usage `rdme categories:create <title> [options]`.'),
);
});

it('should error if categoryType is blank', () => {
return expect(categoriesCreate.run({ key: '123', title: 'Test Title' })).rejects.toStrictEqual(
new Error('`categoryType` must be `guide` or `reference`.')
new Error('`categoryType` must be `guide` or `reference`.'),
);
});

it('should error if categoryType is not `guide` or `reference`', () => {
return expect(
// @ts-expect-error Testing a CLI arg failure case.
categoriesCreate.run({ key: '123', title: 'Test Title', categoryType: 'test' })
categoriesCreate.run({ key: '123', title: 'Test Title', categoryType: 'test' }),
).rejects.toStrictEqual(new Error('`categoryType` must be `guide` or `reference`.'));
});

Expand All @@ -71,7 +72,7 @@ describe('rdme categories:create', () => {
key,
version: '1.0.0',
preventDuplicates: true,
})
}),
).resolves.toBe("🌱 successfully created 'New Category' with a type of 'guide' and an id of '123'");

getMock.done();
Expand Down Expand Up @@ -102,7 +103,7 @@ describe('rdme categories:create', () => {
key,
version: '1.0.0',
preventDuplicates: true,
})
}),
).resolves.toBe("🌱 successfully created 'Category' with a type of 'reference' and an id of '123'");

getMock.done();
Expand All @@ -124,7 +125,7 @@ describe('rdme categories:create', () => {
categoryType: 'guide',
key,
version: '1.0.0',
})
}),
).resolves.toBe("🌱 successfully created 'Category' with a type of 'reference' and an id of '123'");

postMock.done();
Expand All @@ -149,11 +150,11 @@ describe('rdme categories:create', () => {
key,
version: '1.0.0',
preventDuplicates: true,
})
}),
).rejects.toStrictEqual(
new Error(
"The 'Category' category with a type of 'guide' already exists with an id of '123'. A new category was not created."
)
"The 'Category' category with a type of 'guide' already exists with an id of '123'. A new category was not created.",
),
);

getMock.done();
Expand All @@ -178,11 +179,11 @@ describe('rdme categories:create', () => {
key,
version: '1.0.0',
preventDuplicates: true,
})
}),
).rejects.toStrictEqual(
new Error(
"The 'Category' category with a type of 'guide' already exists with an id of '123'. A new category was not created."
)
"The 'Category' category with a type of 'guide' already exists with an id of '123'. A new category was not created.",
),
);

getMock.done();
Expand Down
11 changes: 6 additions & 5 deletions __tests__/cmds/categories/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest';

import CategoriesCommand from '../../../src/cmds/categories';
import getAPIMock, { getAPIMockWithVersionHeader } from '../../helpers/get-api-mock';
Expand All @@ -15,7 +16,7 @@ describe('rdme categories', () => {
afterEach(() => nock.cleanAll());

it('should prompt for login if no API key provided', async () => {
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
prompts.inject(['this-is-not-an-email', 'password', 'subdomain']);
await expect(categories.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand All @@ -24,7 +25,7 @@ describe('rdme categories', () => {
it('should error in CI if no API key provided', async () => {
process.env.TEST_RDME_CI = 'true';
await expect(categories.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
new Error('No project API key provided. Please use `--key`.'),
);
delete process.env.TEST_RDME_CI;
});
Expand All @@ -41,7 +42,7 @@ describe('rdme categories', () => {
const versionMock = getAPIMock().get(`/api/v1/version/${version}`).basicAuth({ user: key }).reply(200, { version });

await expect(categories.run({ key, version: '1.0.0' })).resolves.toBe(
JSON.stringify([{ title: 'One Category', slug: 'one-category', type: 'guide' }], null, 2)
JSON.stringify([{ title: 'One Category', slug: 'one-category', type: 'guide' }], null, 2),
);

getMock.done();
Expand Down Expand Up @@ -71,8 +72,8 @@ describe('rdme categories', () => {
{ title: 'Another Category', slug: 'another-category', type: 'guide' },
],
null,
2
)
2,
),
);

getMock.done();
Expand Down
37 changes: 19 additions & 18 deletions __tests__/cmds/changelogs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from 'chalk';
import frontMatter from 'gray-matter';
import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, it, expect, vi } from 'vitest';

import ChangelogsCommand from '../../../src/cmds/changelogs';
import APIError from '../../../src/lib/apiError';
Expand All @@ -23,7 +24,7 @@ describe('rdme changelogs', () => {
afterAll(() => nock.cleanAll());

it('should prompt for login if no API key provided', async () => {
const consoleInfoSpy = jest.spyOn(console, 'info').mockImplementation();
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
prompts.inject(['this-is-not-an-email', 'password', 'subdomain']);
await expect(changelogs.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand All @@ -32,28 +33,28 @@ describe('rdme changelogs', () => {
it('should error in CI if no API key provided', async () => {
process.env.TEST_RDME_CI = 'true';
await expect(changelogs.run({})).rejects.toStrictEqual(
new Error('No project API key provided. Please use `--key`.')
new Error('No project API key provided. Please use `--key`.'),
);
delete process.env.TEST_RDME_CI;
});

it('should error if no path provided', () => {
return expect(changelogs.run({ key })).rejects.toStrictEqual(
new Error('No path provided. Usage `rdme changelogs <path> [options]`.')
new Error('No path provided. Usage `rdme changelogs <path> [options]`.'),
);
});

it('should error if the argument is not a folder', () => {
return expect(changelogs.run({ key, filePath: 'not-a-folder' })).rejects.toStrictEqual(
new Error("Oops! We couldn't locate a file or directory at the path you provided.")
new Error("Oops! We couldn't locate a file or directory at the path you provided."),
);
});

it('should error if the folder contains no markdown files', () => {
return expect(changelogs.run({ key, filePath: '.github/workflows' })).rejects.toStrictEqual(
new Error(
"The directory you provided (.github/workflows) doesn't contain any of the following required files: .markdown, .md."
)
"The directory you provided (.github/workflows) doesn't contain any of the following required files: .markdown, .md.",
),
);
});

Expand Down Expand Up @@ -114,7 +115,7 @@ describe('rdme changelogs', () => {
[
`✏️ successfully updated 'simple-doc' with contents from __tests__/${fixturesBaseDir}/existing-docs/simple-doc.md`,
`✏️ successfully updated 'another-doc' with contents from __tests__/${fixturesBaseDir}/existing-docs/subdir/another-doc.md`,
].join('\n')
].join('\n'),
);

getMocks.done();
Expand All @@ -141,12 +142,12 @@ describe('rdme changelogs', () => {
expect(updatedDocs).toBe(
[
`🎭 dry run! This will update 'simple-doc' with contents from __tests__/${fixturesBaseDir}/existing-docs/simple-doc.md with the following metadata: ${JSON.stringify(
simpleDoc.doc.data
simpleDoc.doc.data,
)}`,
`🎭 dry run! This will update 'another-doc' with contents from __tests__/${fixturesBaseDir}/existing-docs/subdir/another-doc.md with the following metadata: ${JSON.stringify(
anotherDoc.doc.data
anotherDoc.doc.data,
)}`,
].join('\n')
].join('\n'),
);

getMocks.done();
Expand All @@ -169,7 +170,7 @@ describe('rdme changelogs', () => {
[
'`simple-doc` was not updated because there were no changes.',
'`another-doc` was not updated because there were no changes.',
].join('\n')
].join('\n'),
);

getMocks.done();
Expand All @@ -194,7 +195,7 @@ describe('rdme changelogs', () => {
[
'🎭 dry run! `simple-doc` will not be updated because there were no changes.',
'🎭 dry run! `another-doc` will not be updated because there were no changes.',
].join('\n')
].join('\n'),
);

getMocks.done();
Expand Down Expand Up @@ -225,7 +226,7 @@ describe('rdme changelogs', () => {
.reply(201, { slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash });

await expect(changelogs.run({ filePath: `./__tests__/${fixturesBaseDir}/new-docs`, key })).resolves.toBe(
`🌱 successfully created 'new-doc' (ID: 1234) with contents from __tests__/${fixturesBaseDir}/new-docs/new-doc.md`
`🌱 successfully created 'new-doc' (ID: 1234) with contents from __tests__/${fixturesBaseDir}/new-docs/new-doc.md`,
);

getMock.done();
Expand All @@ -247,11 +248,11 @@ describe('rdme changelogs', () => {
});

await expect(
changelogs.run({ dryRun: true, filePath: `./__tests__/${fixturesBaseDir}/new-docs`, key })
changelogs.run({ dryRun: true, filePath: `./__tests__/${fixturesBaseDir}/new-docs`, key }),
).resolves.toBe(
`🎭 dry run! This will create 'new-doc' with contents from __tests__/${fixturesBaseDir}/new-docs/new-doc.md with the following metadata: ${JSON.stringify(
doc.data
)}`
doc.data,
)}`,
);

getMock.done();
Expand Down Expand Up @@ -296,7 +297,7 @@ describe('rdme changelogs', () => {
};

await expect(changelogs.run({ filePath: `./${fullDirectory}`, key })).rejects.toStrictEqual(
new APIError(formattedErrorObject)
new APIError(formattedErrorObject),
);

getMocks.done();
Expand Down Expand Up @@ -327,7 +328,7 @@ describe('rdme changelogs', () => {
.reply(201, { slug: doc.data.slug, _id: id, body: doc.content, ...doc.data, lastUpdatedHash: hash });

await expect(changelogs.run({ filePath: `./__tests__/${fixturesBaseDir}/slug-docs`, key })).resolves.toBe(
`🌱 successfully created 'marc-actually-wrote-a-test' (ID: 1234) with contents from __tests__/${fixturesBaseDir}/slug-docs/new-doc-slug.md`
`🌱 successfully created 'marc-actually-wrote-a-test' (ID: 1234) with contents from __tests__/${fixturesBaseDir}/slug-docs/new-doc-slug.md`,
);

getMock.done();
Expand Down
Loading