Skip to content

Commit

Permalink
feat(test): moving unit tests over to vitest (#857)
Browse files Browse the repository at this point in the history
* feat(test): moving unit tests over to vitest

* fix: prettier issues

* ci: stop testing node 14

* fix: schema updater for prettier 3

* fix: install missing vitest coverage dep

* chore: better link

* ci: let other node versions run if one fails

* ci: try skipping some tests

* chore(deps): bump eslint config

* test: don't return in before* hooks

if you return a function within before* hooks, they'll run on teardown. so these functions are currently doing the opposite of what we're intending 😮‍💨

see: https://vitest.dev/api/#beforeall

* test: more before* hook fixes

* test: try using stubEnv in a few places

* chore: typo

* chore: rebuild lockfile using npm i

* chore: wait this happened after i ran "npm i" again?

* Revert "Merge branch 'next' into feat/vitest"

This reverts commit 22f7904, reversing
changes made to 3298545.

... weird. so i tried merging from next and reinstalling using `npm i`, which removed these packages and will cause node 14 tests to fail.... i hate it here

* fix(vitest): split out tests (#870)

* test: try splitting out tests

* ci: knip fix

* ci: oops

* chore: remove unused snapshots

* refactor: try using MSW instead of nock in single-threaded tests

* chore: temporarily don't worry about coverage 🤐

* chore: backfill headers-polyfill

* ci: re-enable tests on node 14

* test: add JSON error body

* ci: try matrix for threads

* ci: oops

* chore: copy edits

* chore: run build before tests

* fix(ci): split out ci and test steps

* chore: fix lockfile

* Revert "fix(ci): split out ci and test steps"

This reverts commit 112ec66.

* chore: clean out some comment junk

* chore: try fixing lockfile (again)

@esbuild/android-arm wasn't showing up in here, i think it got accidentally removed which is causing node 14 tests to break

* feat: better header checking

* fix: don't nest timers

* chore: remove unnecessary globals file

* chore: bumping eslint-config

---------

Co-authored-by: Kanad Gupta <git@kanad.dev>
Co-authored-by: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 3d9265a commit 36d561b
Show file tree
Hide file tree
Showing 68 changed files with 32,625 additions and 22,357 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
name: Test Suite
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
Expand All @@ -28,14 +29,8 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: npm

- name: Install deps
run: npm ci

- name: Build dist
run: npm run build

- name: Run tests
run: npm test
- name: Build and run tests
run: npm cit

lint:
runs-on: ubuntu-latest
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.*
2 changes: 1 addition & 1 deletion __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "@readme/eslint-config/testing/jest"
"extends": "@readme/eslint-config/testing/vitest"
}
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: 2 additions & 2 deletions __tests__/cmds/__snapshots__/whoami.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// 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@example.com to the subdomain project."`;
exports[`rdme whoami > should return the authenticated user 1`] = `"You are currently logged in as email@example.com to the subdomain project."`;
7 changes: 5 additions & 2 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 @@ -10,12 +11,14 @@ const key = 'API_KEY';
const version = '1.0.0';

describe('rdme categories:create', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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 Down
7 changes: 5 additions & 2 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 @@ -10,12 +11,14 @@ const key = 'API_KEY';
const version = '1.0.0';

describe('rdme categories', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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 Down
7 changes: 5 additions & 2 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 @@ -18,12 +19,14 @@ const fullFixturesDir = `${__dirname}./../../${fixturesBaseDir}`;
const key = 'API_KEY';

describe('rdme changelogs', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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 Down
7 changes: 5 additions & 2 deletions __tests__/cmds/changelogs/single.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 @@ -18,12 +19,14 @@ const fullFixturesDir = `${__dirname}./../../${fixturesBaseDir}`;
const key = 'API_KEY';

describe('rdme changelogs (single)', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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 Down
7 changes: 5 additions & 2 deletions __tests__/cmds/custompages/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 CustomPagesCommand from '../../../src/cmds/custompages';
import APIError from '../../../src/lib/apiError';
Expand All @@ -18,12 +19,14 @@ const fullFixturesDir = `${__dirname}./../../${fixturesBaseDir}`;
const key = 'API_KEY';

describe('rdme custompages', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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(custompages.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand Down
7 changes: 5 additions & 2 deletions __tests__/cmds/custompages/single.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 CustomPagesCommand from '../../../src/cmds/custompages';
import APIError from '../../../src/lib/apiError';
Expand All @@ -18,12 +19,14 @@ const fullFixturesDir = `${__dirname}./../../${fixturesBaseDir}`;
const key = 'API_KEY';

describe('rdme custompages (single)', () => {
beforeAll(() => nock.disableNetConnect());
beforeAll(() => {
nock.disableNetConnect();
});

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(custompages.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand Down
14 changes: 7 additions & 7 deletions __tests__/cmds/docs/__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[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed as opt (github flag enabled) 1`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed as opt (github flag enabled) 1`] = `
"
Your GitHub Actions workflow file has been created! ✨
Expand All @@ -14,7 +14,7 @@ Almost done! Just a couple more steps:
"
`;

exports[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed as opt (github flag enabled) 2`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed as opt (github flag enabled) 2`] = `
"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z
# You can view our full documentation here: https://docs.readme.com/docs/rdme
name: ReadMe GitHub Action 🦉
Expand All @@ -41,7 +41,7 @@ jobs:
"
`;

exports[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed in via opt 1`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed in via opt 1`] = `
"
Your GitHub Actions workflow file has been created! ✨
Expand All @@ -55,7 +55,7 @@ Almost done! Just a couple more steps:
"
`;

exports[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed in via opt 2`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed in via opt 2`] = `
"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z
# You can view our full documentation here: https://docs.readme.com/docs/rdme
name: ReadMe GitHub Action 🦉
Expand All @@ -82,7 +82,7 @@ jobs:
"
`;

exports[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed in via prompt 1`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed in via prompt 1`] = `
"
Your GitHub Actions workflow file has been created! ✨
Expand All @@ -96,7 +96,7 @@ Almost done! Just a couple more steps:
"
`;

exports[`rdme docs GHA onboarding E2E tests should create GHA workflow with version passed in via prompt 2`] = `
exports[`rdme docs > GHA onboarding E2E tests > should create GHA workflow with version passed in via prompt 2`] = `
"# This GitHub Actions workflow was auto-generated by the \`rdme\` cli on 2022-01-01T00:00:00.000Z
# You can view our full documentation here: https://docs.readme.com/docs/rdme
name: ReadMe GitHub Action 🦉
Expand Down
13 changes: 8 additions & 5 deletions __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';

import nock from 'nock';
import prompts from 'prompts';
import { describe, beforeAll, afterAll, beforeEach, afterEach, it, expect, vi } from 'vitest';

import DocsEditCommand from '../../../src/cmds/docs/edit';
import APIError from '../../../src/lib/apiError';
Expand All @@ -20,20 +21,22 @@ describe('rdme docs:edit', () => {
return [consoleWarnSpy.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
}

beforeAll(() => {
nock.disableNetConnect();
});

beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
});

afterEach(() => {
consoleWarnSpy.mockRestore();
});

beforeAll(() => nock.disableNetConnect());

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(docsEdit.run({})).rejects.toStrictEqual(new Error('You must provide a valid email address.'));
consoleInfoSpy.mockRestore();
Expand All @@ -60,7 +63,7 @@ describe('rdme docs:edit', () => {

it('should fetch the doc from the api', async () => {
expect.assertions(5);
const consoleSpy = jest.spyOn(console, 'info').mockImplementation();
const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
const slug = 'getting-started';
const body = 'abcdef';
const edits = 'ghijkl';
Expand Down
Loading

0 comments on commit 36d561b

Please sign in to comment.