Skip to content

Add unit test suite for app components and Amplify configuration#9

Open
bigweavergammainternal[bot] wants to merge 1 commit intomainfrom
add-unit-tests-20260216-222601
Open

Add unit test suite for app components and Amplify configuration#9
bigweavergammainternal[bot] wants to merge 1 commit intomainfrom
add-unit-tests-20260216-222601

Conversation

@bigweavergammainternal
Copy link
Copy Markdown

This pull request was generated by @kiro-agent 👻

Comment with /kiro fix to address specific feedback or /kiro all to address everything.
Learn about Kiro autonomous agent


Problem

The project had no test framework or test files configured, leaving all source code without automated test coverage. This created a gap in quality assurance for the Next.js App Router components and AWS Amplify Gen 2 backend configuration.

Solution

Set up a zero-dependency testing framework using Node.js built-in test runner (node:test) with a custom TypeScript loader, and added comprehensive unit tests across all key source files.

Testing Framework Setup

  • Test runner: Node.js built-in node:test module — no additional test dependencies required
  • TypeScript support: Custom __tests__/helpers/ts-loader.js that strips TypeScript syntax, converts ESM imports/exports to CJS, and resolves @/ path aliases
  • Mock system: Built-in mock module registration via Module._load override, enabling isolation of AWS Amplify dependencies
  • Test script: Added npm test command to package.json using node --test with auto-discovery of *.test.js files

Test Coverage (40 tests total)

app/page.tsx — App component (20 tests)

  • Verifies client component directive and default export
  • Validates todo list UI rendering (heading, button, list element, success message, tutorial link)
  • Tests createTodo function interaction (prompt call, button wiring, Todo.create invocation)
  • Validates data subscription flow (useState, observeQuery, useEffect mount, list rendering, state updates)
  • Confirms AWS Amplify integration (generateClient import, Schema type, Amplify.configure, typed client)

app/layout.tsx — RootLayout component (9 tests)

  • Validates metadata exports (title, description, Metadata type)
  • Tests component structure (default export, children prop, body rendering)
  • Verifies HTML attributes (lang="en") and Inter font usage

amplify/auth/resource.ts — Auth configuration (3 tests)

  • Verifies defineAuth is called to configure authentication
  • Confirms email is set as the login mechanism
  • Validates the auth configuration is exported

amplify/data/resource.ts — Data schema (8 tests)

  • Verifies schema defines a Todo model with a content string field
  • Tests authorization setup on the Todo model
  • Confirms defineData is called with apiKey as default authorization mode
  • Validates API key expiration is set to 30 days

Files Added/Modified

File Description
__tests__/helpers/ts-loader.js TypeScript loader with mock registration for node:test
__tests__/app/page.test.js Unit tests for the main App component
__tests__/app/layout.test.js Unit tests for the RootLayout component
__tests__/amplify/auth/resource.test.js Unit tests for Amplify auth configuration
__tests__/amplify/data/resource.test.js Unit tests for Amplify data schema
package.json Added test script

Testing

  • All 40 tests pass when running npm test
  • Tests execute using Node.js built-in test runner with no additional dependencies
  • AWS Amplify modules (@aws-amplify/backend, aws-amplify/data) are fully mocked to isolate component behavior from cloud services

- Set up testing framework using Node.js built-in test runner (node:test)
  with a custom TypeScript loader for .ts file support
- Add test helper (ts-loader.js) that strips TypeScript syntax, converts
  ESM to CJS, and provides mock module registration
- Add unit tests for amplify/auth/resource.ts (3 tests):
  - Verifies defineAuth is called to configure authentication
  - Verifies email is configured as the login mechanism
  - Verifies the auth configuration is exported
- Add unit tests for amplify/data/resource.ts (8 tests):
  - Verifies schema defines a Todo model with content string field
  - Verifies authorization is set on the Todo model
  - Verifies defineData is called with apiKey authorization mode
  - Verifies API key expiration is set to 30 days
- Add unit tests for app/layout.tsx (9 tests):
  - Verifies metadata title and description are properly defined
  - Verifies RootLayout renders children inside body element
  - Verifies html lang attribute and Inter font usage
- Add unit tests for app/page.tsx (20 tests):
  - Verifies todo list UI rendering (heading, button, list)
  - Verifies createTodo function interaction (prompt, create call)
  - Verifies data subscription via observeQuery
  - Verifies AWS Amplify integration (generateClient, configure)
- Add test script to package.json: npm test runs all 40 tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant