Skip to content

Commit 9c1df3a

Browse files
Add tests for matchRoutes middleware in auto-routing.js (#79)
Adding tests for the matchRoutes middleware in auto-routing.js - [x] Create test file for auto-routing middleware - [x] Add test setup with Express and Nunjucks configuration - [x] Add tests for root path routing - [x] Add tests for simple path routing - [x] Add test for simple path with .html extension - [x] Add tests for nested path routing (folder with index) - [x] Add tests for non-existent paths (404 scenarios) - [x] Add tests for template errors - [x] Run tests to verify they pass (55 tests total, all passing) - [x] Run linter to ensure code style is correct (all checks pass) - [x] Refactor to match existing test patterns exactly - [x] Use underscore prefix for unused parameters per code review feedback <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Add tests for the matchRoutes middleware</issue_title> > <issue_description>The `lib/express-middleware/auto-routing.js` file needs some tests to be added within `tests/express-middleware/`, following the existing test patterns.</issue_description> > > <agent_instructions>Use the built-in node:test and node:assert libraries.</agent_instructions> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #78 <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/nhsuk/nhsuk-prototype-kit-package/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: frankieroberto <30665+frankieroberto@users.noreply.github.com>
1 parent 36df985 commit 9c1df3a

File tree

6 files changed

+120
-5
lines changed

6 files changed

+120
-5
lines changed

package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
const assert = require('node:assert')
2+
const http = require('node:http')
3+
const { join } = require('node:path')
4+
const { describe, it } = require('node:test')
5+
6+
const express = require('express')
7+
const nunjucks = require('nunjucks')
8+
const request = require('supertest')
9+
10+
const { matchRoutes } = require('../../lib/express-middleware/auto-routing')
11+
12+
describe('matchRoutes', () => {
13+
// Create test templates directory
14+
const templatesDir = join(__dirname, 'test-templates')
15+
16+
// Setup Express app with Nunjucks
17+
const app = express()
18+
app.set('view engine', 'html')
19+
nunjucks.configure([templatesDir], {
20+
express: app,
21+
noCache: true
22+
})
23+
24+
// Add the matchRoutes middleware
25+
app.use(matchRoutes)
26+
27+
// Add 404 handler
28+
app.use((req, res) => {
29+
res.status(404).send('Not found')
30+
})
31+
32+
// Add error handler
33+
app.use((_err, _req, res, _next) => {
34+
res.status(500).send('Template error')
35+
})
36+
37+
const server = http.createServer(app)
38+
39+
describe('root path', () => {
40+
it('should render index.html for root path', async () => {
41+
const response = await request(server).get('/')
42+
assert.strictEqual(response.status, 200)
43+
assert.strictEqual(response.text, 'Test index page\n')
44+
assert.strictEqual(
45+
response.headers['content-type'],
46+
'text/html; charset=utf-8'
47+
)
48+
})
49+
})
50+
51+
describe('simple path', () => {
52+
it('should render simple.html for /simple path', async () => {
53+
const response = await request(server).get('/simple')
54+
assert.strictEqual(response.status, 200)
55+
assert.strictEqual(response.text, 'Simple test page\n')
56+
assert.strictEqual(
57+
response.headers['content-type'],
58+
'text/html; charset=utf-8'
59+
)
60+
})
61+
62+
it('should render simple.html for /simple.html path', async () => {
63+
const response = await request(server).get('/simple.html')
64+
assert.strictEqual(response.status, 200)
65+
assert.strictEqual(response.text, 'Simple test page\n')
66+
assert.strictEqual(
67+
response.headers['content-type'],
68+
'text/html; charset=utf-8'
69+
)
70+
})
71+
})
72+
73+
describe('nested path with index', () => {
74+
it('should render nested/index.html for /nested path', async () => {
75+
const response = await request(server).get('/nested')
76+
assert.strictEqual(response.status, 200)
77+
assert.strictEqual(response.text, 'Nested index page\n')
78+
assert.strictEqual(
79+
response.headers['content-type'],
80+
'text/html; charset=utf-8'
81+
)
82+
})
83+
84+
it('should render nested/index.html for /nested/ path with trailing slash', async () => {
85+
const response = await request(server).get('/nested/')
86+
assert.strictEqual(response.status, 200)
87+
assert.strictEqual(response.text, 'Nested index page\n')
88+
assert.strictEqual(
89+
response.headers['content-type'],
90+
'text/html; charset=utf-8'
91+
)
92+
})
93+
})
94+
95+
describe('non-existent paths', () => {
96+
it('should call next() for non-existent template', async () => {
97+
const response = await request(server).get('/does-not-exist')
98+
assert.strictEqual(response.status, 404)
99+
assert.strictEqual(response.text, 'Not found')
100+
})
101+
102+
it('should call next() for non-existent nested path', async () => {
103+
const response = await request(server).get('/path/does/not/exist')
104+
assert.strictEqual(response.status, 404)
105+
assert.strictEqual(response.text, 'Not found')
106+
})
107+
})
108+
109+
describe('template errors', () => {
110+
it('should call next(error) for template rendering errors', async () => {
111+
const response = await request(server).get('/error')
112+
assert.strictEqual(response.status, 500)
113+
assert.strictEqual(response.text, 'Template error')
114+
})
115+
})
116+
})
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% unknown_tag %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test index page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Nested index page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simple test page

0 commit comments

Comments
 (0)