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

Add vitest unit tests #2122

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ jobs:

- name: Run Check
run: pnpm run lint:linkcheck
unitTest:
name: Check for code issues with Vitest
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Tools & Dependencies
uses: ./.github/actions/install

- name: Run Check
run: pnpm run test
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"test": "vitest",
"preview": "astro preview",
"format": "pnpm run format:code",
"format:ci": "pnpm run format:imports && pnpm run format:code",
Expand Down Expand Up @@ -77,7 +78,8 @@
"typescript": "^4.7.4",
"unified": "^10.1.2",
"unist-util-remove": "^3.1.0",
"unist-util-visit": "^4.1.0"
"unist-util-visit": "^4.1.0",
"vitest": "^0.25.3"
},
"dependencies": {
"@fontsource/ibm-plex-mono": "^4.5.10",
Expand Down
134 changes: 134 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions src/util/generateToc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import generateToc from './generateToc';
import { describe, expect, test } from 'vitest';
const overview = { depth: 2, slug: 'overview', text: 'Overview', children: [] };
describe('generateToc', () => {
test('should include a default overview heading', () => {
expect(generateToc([])).toEqual([overview]);
});
test('should pass when heading 2 comes before heading 3', () => {
const toc = generateToc([
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
},
{
slug: 'heading 3',
text: 'heading 3',
depth: 3,
},
]);
expect(toc).toEqual([
overview,
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
children: [
{
slug: 'heading 3',
text: 'heading 3',
depth: 3,
children: [],
},
],
},
]);
});

test('should pass when heading 2 comes before heading 2', () => {
const toc = generateToc([
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
},
{
slug: 'heading 3',
text: 'heading 3',
depth: 3,
},
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
},
]);
expect(toc).toEqual([
overview,
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
children: [
{
slug: 'heading 3',
text: 'heading 3',
depth: 3,
children: [],
},
],
},
{
slug: 'heading 2',
text: 'heading 2',
depth: 2,
children: [],
},
]);
});
});
1 change: 0 additions & 1 deletion src/util/generateToc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export default function generateToc(headings: MarkdownHeading[], title = 'Overvi
} else {
const lastItemInToc = toc[toc.length - 1];
if (heading.depth < lastItemInToc.depth) {
console.log(headings);
throw new Error(`Orphan heading found: ${heading.text}.`);
}
if (heading.depth === lastItemInToc.depth) {
Expand Down