forked from shadowwalker/next-pwa
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: added some simple Jest tests (#36)
They simply ensure that we can run dev, build and fetch assets/pages. Hopefully we can introduce more advanced tests in the future.
- Loading branch information
Showing
45 changed files
with
2,376 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
node_modules/ | ||
.pnp/ | ||
.pnp.js/ | ||
|
||
# testing | ||
coverage/ | ||
|
||
# next.js | ||
.next/ | ||
out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env* | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const RootLayout = ({ children }: { children: React.ReactNode }) => ( | ||
<html lang="en"> | ||
<head /> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
|
||
export default RootLayout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Image from "next/image"; | ||
|
||
const Page = () => ( | ||
<main> | ||
<p id="welcome-text">This is a Next.js PWA!</p> | ||
<Image | ||
src="/next.svg" | ||
alt="Next.js Logo" | ||
width={180} | ||
height={37} | ||
priority | ||
/> | ||
</main> | ||
); | ||
|
||
export default Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { createDescribe } from "../../utils/create-describe"; | ||
|
||
createDescribe( | ||
"e2e app-dir", | ||
{ sourceDir: __dirname, skipInstall: false }, | ||
({ next }) => { | ||
it("should render", async () => { | ||
const $ = await next.render("/"); | ||
expect($("#welcome-text").text()).toBe("This is a Next.js PWA!"); | ||
}); | ||
|
||
it("should fetch image", async () => { | ||
const image = await next.fetch("/next.svg"); | ||
expect(image.status).toBe(200); | ||
const favicon = await next.fetch("/favicon.ico"); | ||
expect(favicon.status).toBe(200); | ||
}); | ||
|
||
it("should be able to fetch service worker", async () => { | ||
const sw = await next.fetch("/sw.js"); | ||
expect(sw.status).toBe(200); | ||
expect( | ||
sw.headers.get("Content-Type")?.includes("application/javascript") | ||
).toBe(true); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const withPWA = require("@ducanh2912/next-pwa").default({ | ||
dest: "public", | ||
}); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
module.exports = withPWA(nextConfig); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createDescribe } from "../../utils/create-describe"; | ||
|
||
createDescribe( | ||
"e2e app-dir", | ||
{ sourceDir: __dirname, skipInstall: false }, | ||
({ next }) => { | ||
it("should render", async () => { | ||
const $ = await next.render("/"); | ||
expect($("#welcome-text").text()).toBe("This is a Next.js PWA!"); | ||
expect($("#app-root-text").text()).toBe("This is placed at _app.tsx!"); | ||
}); | ||
|
||
it("should fetch image", async () => { | ||
const image = await next.fetch("/next.svg"); | ||
expect(image.status).toBe(200); | ||
const favicon = await next.fetch("/favicon.ico"); | ||
expect(favicon.status).toBe(200); | ||
}); | ||
|
||
it("should be able to fetch service worker", async () => { | ||
const sw = await next.fetch("/sw.js"); | ||
expect(sw.status).toBe(200); | ||
expect( | ||
sw.headers.get("Content-Type")?.includes("application/javascript") | ||
).toBe(true); | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const withPWA = require("@ducanh2912/next-pwa").default({ | ||
dest: "public", | ||
}); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
module.exports = withPWA(nextConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { AppProps } from "next/app"; | ||
|
||
const App = ({ Component, pageProps }: AppProps) => ( | ||
<> | ||
<p id="app-root-text">This is placed at _app.tsx!</p> | ||
<Component {...pageProps} /> | ||
</> | ||
); | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Image from "next/image"; | ||
|
||
const Page = () => ( | ||
<main> | ||
<p id="welcome-text">This is a Next.js PWA!</p> | ||
<Image | ||
src="/next.svg" | ||
alt="Next.js Logo" | ||
width={180} | ||
height={37} | ||
priority | ||
/> | ||
</main> | ||
); | ||
|
||
export default Page; |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
], | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// a timeout of 5 minutes | ||
jest.setTimeout(300 * 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "tests", | ||
"version": "1.0.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev" | ||
}, | ||
"dependencies": { | ||
"next": "latest", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "18.2.11", | ||
"@types/react-dom": "18.2.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": true | ||
}, | ||
"files": ["../node_modules/jest-chain/types/index.d.ts"], | ||
"include": ["./**/*.ts", "./**/*.tsx"], | ||
"exclude": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { NextInstance, NextInstanceOpts } from "./next-instance-base"; | ||
import { NextInstanceDev } from "./next-instance-dev"; | ||
import { NextInstanceStart } from "./next-instance-start"; | ||
|
||
const validTestModes = ["dev", "start"] as const; | ||
|
||
type NextTestMode = (typeof validTestModes)[number]; | ||
|
||
interface NextTestOpts extends NextInstanceOpts { | ||
sourceDir: string; | ||
} | ||
|
||
const isValidTestMode = (mode: string | undefined): mode is NextTestMode => | ||
typeof mode === "string" && validTestModes.includes(mode as any); | ||
|
||
let testMode: NextTestMode = "start"; | ||
|
||
const envTestMode = process.env.NEXT_TEST_MODE; | ||
|
||
if (isValidTestMode(envTestMode)) { | ||
testMode = envTestMode; | ||
} | ||
|
||
const createNext = async (opts: NextTestOpts) => { | ||
let nextInstance: NextInstance | undefined = undefined; | ||
try { | ||
switch (testMode) { | ||
case "dev": | ||
nextInstance = new NextInstanceDev(opts); | ||
break; | ||
case "start": | ||
nextInstance = new NextInstanceStart(opts); | ||
break; | ||
} | ||
await nextInstance.setup(opts.sourceDir); | ||
await nextInstance.spawn(); | ||
return nextInstance; | ||
} catch (err) { | ||
console.error( | ||
`failed to create next instance: ${JSON.stringify(err, null, 2)}` | ||
); | ||
try { | ||
await nextInstance?.destroy(); | ||
} catch { | ||
// do nothing | ||
} | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export const createDescribe = ( | ||
name: string, | ||
opts: NextTestOpts, | ||
fn: (args: { next: NextInstance; testMode: NextTestMode }) => void | ||
) => { | ||
describe(name, () => { | ||
let next: NextInstance; | ||
beforeAll(async () => { | ||
next = await createNext(opts); | ||
}); | ||
afterAll(async () => { | ||
await next.destroy(); | ||
}); | ||
const nextProxy = new Proxy<NextInstance>({} as NextInstance, { | ||
get(_target, property: keyof NextInstance) { | ||
const prop = next[property]; | ||
return typeof prop === "function" ? prop.bind(next) : prop; | ||
}, | ||
}); | ||
fn({ next: nextProxy, testMode }); | ||
}); | ||
}; |
Oops, something went wrong.