Skip to content

Commit 0f3e35d

Browse files
committed
test: fix sitemap tests
Signed-off-by: ivelin <[email protected]>
1 parent 904c24b commit 0f3e35d

File tree

4 files changed

+74
-51
lines changed

4 files changed

+74
-51
lines changed

next-sitemap.config.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// next-sitemap.config.js
22
// File path: next-sitemap.config.js
3-
// Configures sitemap for K12Beast, including home, public, and documentation pages
4-
// Scans directories for page.tsx and page.mdx files, excluding dynamic routes
3+
// Configures sitemap for K12Beast, including only root and public pages
4+
// Scans directories for page.tsx and page.mdx files, excluding dynamic routes and non-public paths
55

66
const fs = require('fs');
77
const path = require('path');
@@ -17,10 +17,13 @@ function addPaths(dir, prefix, fileName) {
1717
paths.push(...addPaths(filePath, `${prefix}/${file}`, fileName));
1818
} else if (file === fileName) {
1919
const relativePath = prefix.endsWith('/public') ? '/public' : prefix;
20-
if (!relativePath.includes('[')) {
20+
// Exclude dynamic routes and non-public paths
21+
if (!relativePath.includes('[') && (relativePath === '/public' || relativePath.startsWith('/public/'))) {
2122
paths.push({
2223
loc: relativePath,
2324
lastmod: new Date().toISOString(),
25+
changefreq: 'daily',
26+
priority: relativePath === '/public' ? 0.8 : 0.7,
2427
});
2528
}
2629
}
@@ -37,6 +40,8 @@ module.exports = {
3740
{
3841
loc: '/',
3942
lastmod: new Date().toISOString(),
43+
changefreq: 'daily',
44+
priority: 1.0,
4045
},
4146
];
4247
paths.push(...addPaths(PUBLIC_DIR, '/public', 'page.tsx'));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"postinstall": "echo 'Please set up your environment variables in .env.local. See README.md for instructions.'",
1111
"test:e2e": "playwright test",
1212
"test:e2e:ui": "playwright test --ui",
13-
"test:server": "jest --config jest.config.ts",
13+
"test:server": "npm run sitemap && jest --config jest.config.ts",
1414
"test": "npm run test:e2e && npm run test:server",
1515
"sitemap": "next-sitemap"
1616
},

public/sitemap.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
3+
<url><loc>https://k12beast.com</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>1</priority></url>
4+
<url><loc>https://k12beast.com/public/about</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
5+
<url><loc>https://k12beast.com/public/confirm-success</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
6+
<url><loc>https://k12beast.com/public/login</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
7+
<url><loc>https://k12beast.com/public/privacy</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
8+
<url><loc>https://k12beast.com/public/reset-password</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
9+
<url><loc>https://k12beast.com/public/signup</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
10+
<url><loc>https://k12beast.com/public/terms</loc><lastmod>2025-05-30T21:49:00.027Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
11+
<url><loc>https://k12beast.com/public/docs/getting-started</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
12+
<url><loc>https://k12beast.com/public/docs</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
13+
<url><loc>https://k12beast.com/public/docs/parents/introduction</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
14+
<url><loc>https://k12beast.com/public/docs/parents</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
15+
<url><loc>https://k12beast.com/public/docs/students</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
16+
<url><loc>https://k12beast.com/public/docs/subjects</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
17+
<url><loc>https://k12beast.com/public/docs/teachers/classroom</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
18+
<url><loc>https://k12beast.com/public/docs/teachers</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
19+
<url><loc>https://k12beast.com/public/docs/tutors/guide</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
20+
<url><loc>https://k12beast.com/public/docs/tutors</loc><lastmod>2025-05-30T21:49:00.028Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
21+
</urlset>

tests/server/sitemap.spec.ts

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,77 @@
11
// tests/server/sitemap.spec.ts
2-
// File path: tests/server/sitemap.spec.ts
3-
// Verifies sitemap.xml generation includes all expected URLs
4-
// Handles missing sitemap file and generation errors
5-
6-
import { execSync } from 'child_process';
7-
import { readFileSync, unlinkSync, existsSync } from 'fs';
2+
// Tests sitemap generation for K12Beast, ensuring only root and public pages are included
3+
import { readFile, existsSync } from 'fs';
84
import { parseStringPromise } from 'xml2js';
5+
import { resolve } from 'path';
6+
import { promisify } from 'util';
97

10-
describe('Sitemap Generation', () => {
11-
const sitemapPath = './public/sitemap.xml';
8+
const readFileAsync = promisify(readFile);
129

13-
beforeAll(() => {
14-
// Run next-sitemap CLI to generate sitemap.xml
15-
try {
16-
execSync('npm run sitemap', { stdio: 'inherit' });
17-
} catch (error) {
18-
throw new Error(`Failed to generate sitemap: ${error.message}`);
19-
}
20-
});
21-
22-
afterAll(() => {
23-
// Clean up sitemap.xml
24-
if (existsSync(sitemapPath)) {
25-
unlinkSync(sitemapPath);
26-
}
27-
});
10+
describe('Sitemap Generation', () => {
11+
test('includes only root and public pages in sitemap.xml', async () => {
12+
// Resolve path to sitemap
13+
const sitemapPath = resolve('public/sitemap.xml');
14+
console.log(`[Sitemap Test] Checking for sitemap at: ${sitemapPath}`);
2815

29-
it('includes all expected pages in sitemap.xml', async () => {
30-
// Check if sitemap.xml exists
16+
// Check if sitemap file exists
3117
if (!existsSync(sitemapPath)) {
32-
throw new Error('Sitemap file not found: ./public/sitemap.xml. Ensure `npm run build` is run before `npm run sitemap`.');
18+
throw new Error(
19+
'Sitemap file not found at public/sitemap.xml. Ensure `npm run build` and `npm run sitemap` are run before tests. Check `next-sitemap.config.js` and `npm run sitemap` output for errors.'
20+
);
3321
}
3422

35-
// Read sitemap.xml
36-
const sitemapContent = readFileSync(sitemapPath, 'utf8');
23+
// Read the generated sitemap
24+
const sitemapContent = await readFileAsync(sitemapPath, 'utf-8');
3725

38-
// Parse XML to JSON
26+
// Parse XML to extract URLs
3927
const parsed = await parseStringPromise(sitemapContent);
40-
const urls = parsed.urlset.url.map((u) => u.loc[0]).sort();
28+
const urls = parsed.urlset.url.map((entry: { loc: string[] }) => entry.loc[0]);
29+
console.log(`[Sitemap Test] Parsed URLs: ${urls.join(', ')}`);
4130

42-
// Expected pages
31+
// Define expected URLs based on site structure and documentation plan
4332
const expectedUrls = [
4433
'https://k12beast.com',
4534
'https://k12beast.com/public/about',
4635
'https://k12beast.com/public/confirm-success',
47-
'https://k12beast.com/public/login',
48-
'https://k12beast.com/public/privacy',
49-
'https://k12beast.com/public/reset-password',
50-
'https://k12beast.com/public/signup',
51-
'https://k12beast.com/public/terms',
5236
'https://k12beast.com/public/docs',
5337
'https://k12beast.com/public/docs/getting-started',
54-
'https://k12beast.com/public/docs/students',
5538
'https://k12beast.com/public/docs/parents',
5639
'https://k12beast.com/public/docs/parents/introduction',
40+
'https://k12beast.com/public/docs/students',
41+
'https://k12beast.com/public/docs/subjects',
5742
'https://k12beast.com/public/docs/teachers',
5843
'https://k12beast.com/public/docs/teachers/classroom',
5944
'https://k12beast.com/public/docs/tutors',
6045
'https://k12beast.com/public/docs/tutors/guide',
61-
'https://k12beast.com/public/docs/subjects',
62-
].sort();
46+
'https://k12beast.com/public/login',
47+
'https://k12beast.com/public/privacy',
48+
'https://k12beast.com/public/reset-password',
49+
'https://k12beast.com/public/signup',
50+
'https://k12beast.com/public/terms',
51+
];
6352

6453
// Verify total number of URLs
6554
if (urls.length !== expectedUrls.length) {
55+
console.log(`[Sitemap Test] URL count mismatch. Expected: ${expectedUrls.length}, Received: ${urls.length}`);
6656
throw new Error(`URL count mismatch. Expected: ${expectedUrls.length}, Received: ${urls.length}, URLs: ${urls.join(', ')}`);
6757
}
6858
expect(urls.length).toBe(expectedUrls.length);
6959

70-
// Compare each expected URL individually
71-
expectedUrls.forEach((expectedUrl, index) => {
72-
console.log(`Checking URL ${index}: ${expectedUrl}`);
73-
const found = urls.includes(expectedUrl);
74-
if (!found) {
75-
throw new Error(`URL at index ${index} not found: expected "${expectedUrl}", received: ${urls.join(', ')}`);
60+
// Verify all URLs are either root or start with /public/
61+
for (const url of urls) {
62+
if (!url.match(/^https:\/\/k12beast\.com(\/?|\/public\/.*)$/)) {
63+
console.log(`[Sitemap Test] Invalid URL format: ${url}`);
64+
}
65+
expect(url).toMatch(/^https:\/\/k12beast\.com(\/?|\/public\/.*)$/);
66+
expect(expectedUrls).toContain(url);
67+
}
68+
69+
// Verify all expected URLs are present
70+
for (const expectedUrl of expectedUrls) {
71+
if (!urls.includes(expectedUrl)) {
72+
console.log(`[Sitemap Test] Missing expected URL: ${expectedUrl}`);
7673
}
77-
expect(found).toBe(true);
78-
});
74+
expect(urls).toContain(expectedUrl);
75+
}
7976
});
8077
});

0 commit comments

Comments
 (0)