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 caching pages and tests #16

Merged
merged 8 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .dockerignore

This file was deleted.

36 changes: 0 additions & 36 deletions .github/workflows/test.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22.11.0
22.12.0
29 changes: 6 additions & 23 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,20 @@

## Code Organization

All code should abide by the following organization rules:

- `fixtures/` is for directories that will be utilized in testing.
- They should be fully executable locally for debugging purposes
- They should be as minimal as possible
- `test/` is for Playwright based test files that are executed by `npm run test`
- Test files should use `import { test, expect } from '../util/test-fixture.js';` so that the correct **Fixture** is managed for the test script
- Test files should execute serially, and relevant to the given Next.js versions
- `util/` is for any non-module source code. This includes scripts (`util/scripts/`), docker configurations (`util/docker/`), or any other utility based code.
- Prime examples include the source code responsible for [_building_](./util/scripts/pretest.js) the **Fixtures**, or the custom Playwright [test fixture](./util/test-fixture.js)

The key source files for the repo are:

- `cli.js`
- `extension.js`
- `config.yaml`
- `schema.graphql`

## Testing
These are what are included in the published module and what HarperDB relies on for the component to work.

Testing for this repo uses containers in order to generate stable, isolated environments containing:
The `fixtures/` directory contains testable examples of using this extension.

- HarperDB
- Node.js
- A HarperDB Base Component (responsible for seeding the database)
- A Next.js application Component (which uses this `@harperdb/nextjs` extension)
The `test-utils/` directory contains some scripts for Playwright that the fixtures use to setup HarperDB for the test run.

To execute tests, run `npm run test`

The first run may take some time as the pretest script is building 12 separate images (3 Node.js ones, 9 Next.js ones). Note, at the moment this operation is parallelized as building is very expensive and can result in the system running out of resources (and crashing the build processes). Subsequent runs utilize the Docker build step cache and are very fast.
## Testing

After the images are built, [Playwright](https://playwright.dev/) will run the tests. These tests each utilize an image, and will manage a container instance relevant to the given Next.js and Node.js pair.
After many, many hours and a plethora of different attempts, setting up HarperDB and running the fixture Playwright tests was too flaky for automation. Review the repository at this [commit](https://github.com/HarperDB/nextjs/tree/b72c05e29bd5afd4b91425ae709effa05bd3c2fd) for some of the prior art.

The tests are configured with generous timeouts and limited to 3 workers at a time to not cause the system to run out of resources.
Instead, for now, fixtures can be tested by running them locally. Make sure to have HarperDB installed globally on your machine. Generally, to run the tests within a fixture, you only have to run `npm install` and `npm run test` from within the specific fixture directory.
3 changes: 2 additions & 1 deletion fixtures/harperdb-base-component/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "harperdb-base-component",
"private": true
"private": true,
"type": "module"
}
9 changes: 1 addition & 8 deletions fixtures/harperdb-base-component/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ const dogs = [
{ id: '1', name: 'Max', breed: 'Cocker Spaniel' },
{ id: '2', name: 'Bella', breed: 'Lab' },
{ id: '3', name: 'Charlie', breed: 'Great Dane' },
{ id: '4', name: 'Lucy', breed: 'Newfoundland' },
{ id: '5', name: 'Cooper', breed: 'Pug' },
{ id: '6', name: 'Daisy', breed: 'Bull Dog' },
{ id: '7', name: 'Rocky', breed: 'Akita' },
{ id: '8', name: 'Luna', breed: 'Wolf' },
{ id: '9', name: 'Buddy', breed: 'Border Collie' },
{ id: '10', name: 'Bailey', breed: 'Golden Retriever' },
{ id: '11', name: 'Sadie', breed: 'Belgian Malinois' },
{ id: '4', name: 'Lucy', breed: 'Newfoundland' }
];

for (const dog of dogs) {
Expand Down
1 change: 1 addition & 0 deletions fixtures/next-14/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.12.0
1 change: 0 additions & 1 deletion fixtures/next-14/.npmrc

This file was deleted.

13 changes: 13 additions & 0 deletions fixtures/next-14/app/components/Dog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { notFound } from 'next/navigation';

export default async function Dog({ dog }) {
return dog ? (
<div>
<h1>{dog.name}</h1>
<p>{dog.breed}</p>
<p>Woof!</p>
</div>
) : (
notFound()
);
}
9 changes: 9 additions & 0 deletions fixtures/next-14/app/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import('harperdb');

export function getDog(id) {
return tables.Dog.get(id);
}

export function listDogs() {
return tables.Dog.search().map(dog => ({ id: dog.id }));
}
19 changes: 19 additions & 0 deletions fixtures/next-14/app/isr-dogs/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Dog from '../../components/Dog';
import { listDogs, getDog } from '../../data';

export const revalidate = 1;

export default async function ISRDog({ params }) {
const dog = await getDog(params.id);

return (
<div>
<p data-testid="when">{new Date().toISOString()}</p>
<Dog dog={dog} />
</div>
);
}

export function generateStaticParams() {
return listDogs();
}
11 changes: 11 additions & 0 deletions fixtures/next-14/app/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Link from 'next/link';

export default function NotFound() {
return (
<div>
<h1>404 Woof!</h1>
<p>Page not found.</p>
<Link href="/">Home</Link>
</div>
);
}
17 changes: 17 additions & 0 deletions fixtures/next-14/app/ssg-dogs/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Dog from '../../components/Dog';
import { getDog, listDogs } from '../../data';

export default async function SSGDog({ params }) {
const dog = getDog(params.id);

return (
<div>
<p data-testid="when">{new Date().toISOString()}</p>
<Dog dog={dog} />
</div>
);
}

export function generateStaticParams() {
return listDogs();
}
13 changes: 13 additions & 0 deletions fixtures/next-14/app/ssr-dogs/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Dog from '../../components/Dog';
import { getDog } from '../../data';

export default async function SSRDog({ params }) {
const dog = await getDog(params.id);

return (
<div>
<p data-testid="when">{new Date().toISOString()}</p>
<Dog dog={dog} />
</div>
);
}
9 changes: 9 additions & 0 deletions fixtures/next-14/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextResponse, NextRequest } from 'next/server';

export function middleware(request) {
return new NextResponse('Hello from middleware', { headers: { 'x-middleware': 'true' } });
}

export const config = {
matcher: '/test-middleware'
}
10 changes: 9 additions & 1 deletion fixtures/next-14/next.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
module.exports = {};
module.exports = {
webpack: (config) => {
config.externals.push({
harperdb: 'commonjs harperdb',
});

return config;
}
};
Loading