Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit e97b64a

Browse files
committed
Tests: Cache NextJS builds
Cache the NextJS apps built for testing. The cache is based on the current NextJS version and a hash of the folder contents. This guarantees that the app will be rebuilt if the fixture files change (e.g., modifying one of the pages or the config). Because the building of NextJS is rather slow, the tests are several times faster when a cache has been established. The cache can be busted by running -npm run cleanup-.
1 parent a11be6a commit e97b64a

18 files changed

+299
-388
lines changed

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
],
2727
"scripts": {
2828
"format": "prettier --write .",
29+
"cleanup": "node ./tests/helpers/clearCache.js",
2930
"test": "jest --config tests/jest.config.js",
3031
"cypress:local": "env CYPRESS_DEPLOY=local cypress run --config-file false --config video=false",
3132
"cypress:netlify": "env CYPRESS_DEPLOY=netlify cypress run --config-file false --config video=false",
@@ -37,6 +38,7 @@
3738
},
3839
"devDependencies": {
3940
"cypress": "^5.1.0",
41+
"folder-hash": "^3.3.3",
4042
"jest": "^26.4.2",
4143
"netlify-cli": "^2.61.0",
4244
"next": "^9.5.3",

tests/customNextDistDir.test.js

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
// Test next-on-netlify when a custom distDir is set in next.config.js
2+
23
const { parse, join } = require("path");
3-
const {
4-
copySync,
5-
emptyDirSync,
6-
existsSync,
7-
readdirSync,
8-
readFileSync,
9-
readJsonSync,
10-
} = require("fs-extra");
11-
const npmRunBuild = require("./helpers/npmRunBuild");
4+
const buildNextApp = require("./helpers/buildNextApp");
125

136
// The name of this test file (without extension)
147
const FILENAME = parse(__filename).name;
@@ -18,49 +11,25 @@ const FILENAME = parse(__filename).name;
1811
// package.json file.
1912
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
2013

21-
// The directory that contains the fixtures, such as NextJS pages,
22-
// NextJS config, and package.json
23-
const FIXTURE_PATH = join(__dirname, "fixtures");
24-
25-
// Capture the output of `npm run build` to verify successful build
26-
let BUILD_OUTPUT;
14+
// Capture the output to verify successful build
15+
let buildOutput;
2716

2817
beforeAll(
2918
async () => {
30-
// Clear project directory
31-
emptyDirSync(PROJECT_PATH);
32-
emptyDirSync(join(PROJECT_PATH, "pages"));
33-
34-
// Copy NextJS pages and config
35-
copySync(join(FIXTURE_PATH, "pages"), join(PROJECT_PATH, "pages"));
36-
copySync(
37-
join(FIXTURE_PATH, "next.config.js-with-distDir.js"),
38-
join(PROJECT_PATH, "next.config.js")
39-
);
40-
41-
// Copy package.json
42-
copySync(
43-
join(FIXTURE_PATH, "package.json"),
44-
join(PROJECT_PATH, "package.json")
45-
);
46-
47-
// Invoke `npm run build`: Build Next and run next-on-netlify
48-
const { stdout } = await npmRunBuild({ directory: PROJECT_PATH });
49-
BUILD_OUTPUT = stdout;
19+
buildOutput = await buildNextApp()
20+
.forTest(__filename)
21+
.withPages("pages-with-gssp-index")
22+
.withNextConfig("next.config.js")
23+
.withPackageJson("package.json")
24+
.build();
5025
},
5126
// time out after 180 seconds
5227
180 * 1000
5328
);
5429

55-
describe("Next", () => {
30+
describe("next-on-netlify", () => {
5631
test("builds successfully", () => {
57-
// NextJS output
58-
expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...");
59-
expect(BUILD_OUTPUT).toMatch("Finalizing page optimization...");
60-
expect(BUILD_OUTPUT).toMatch("First Load JS shared by all");
61-
62-
// Next on Netlify output
63-
expect(BUILD_OUTPUT).toMatch("Next on Netlify");
64-
expect(BUILD_OUTPUT).toMatch("Success! All done!");
32+
expect(buildOutput).toMatch("Next on Netlify");
33+
expect(buildOutput).toMatch("Success! All done!");
6534
});
6635
});

tests/customRedirects.test.js

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// Test next-on-netlify when a custom distDir is set in next.config.js
2+
23
const { parse, join } = require("path");
3-
const {
4-
copySync,
5-
emptyDirSync,
6-
readFileSync,
7-
writeFileSync,
8-
} = require("fs-extra");
9-
const npmRunBuild = require("./helpers/npmRunBuild");
4+
const { readFileSync } = require("fs-extra");
5+
const buildNextApp = require("./helpers/buildNextApp");
106

117
// The name of this test file (without extension)
128
const FILENAME = parse(__filename).name;
@@ -16,60 +12,27 @@ const FILENAME = parse(__filename).name;
1612
// package.json file.
1713
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
1814

19-
// The directory that contains the fixtures, such as NextJS pages,
20-
// NextJS config, and package.json
21-
const FIXTURE_PATH = join(__dirname, "fixtures");
22-
23-
// Capture the output of `npm run build` to verify successful build
24-
let BUILD_OUTPUT;
15+
// Capture the output to verify successful build
16+
let buildOutput;
2517

2618
beforeAll(
2719
async () => {
28-
// Clear project directory
29-
emptyDirSync(PROJECT_PATH);
30-
emptyDirSync(join(PROJECT_PATH, "pages"));
31-
32-
// Copy NextJS pages and config
33-
copySync(
34-
join(FIXTURE_PATH, "pages-with-static-props-index"),
35-
join(PROJECT_PATH, "pages")
36-
);
37-
copySync(
38-
join(FIXTURE_PATH, "next.config.js"),
39-
join(PROJECT_PATH, "next.config.js")
40-
);
41-
42-
// Copy package.json
43-
copySync(
44-
join(FIXTURE_PATH, "package.json"),
45-
join(PROJECT_PATH, "package.json")
46-
);
47-
48-
// Create a _redirects file
49-
writeFileSync(
50-
join(PROJECT_PATH, "_redirects"),
51-
"# Custom Redirect Rules\n" +
52-
"https://old.example.com/* https://new.example.com/:splat 301!\n"
53-
);
54-
55-
// Invoke `npm run build`: Build Next and run next-on-netlify
56-
const { stdout } = await npmRunBuild({ directory: PROJECT_PATH });
57-
BUILD_OUTPUT = stdout;
20+
buildOutput = await buildNextApp()
21+
.forTest(__filename)
22+
.withPages("pages-with-static-props-index")
23+
.withNextConfig("next.config.js")
24+
.withPackageJson("package.json")
25+
.withFile("_redirects")
26+
.build();
5827
},
5928
// time out after 180 seconds
6029
180 * 1000
6130
);
6231

63-
describe("Next", () => {
32+
describe("next-on-netlify", () => {
6433
test("builds successfully", () => {
65-
// NextJS output
66-
expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...");
67-
expect(BUILD_OUTPUT).toMatch("Finalizing page optimization...");
68-
expect(BUILD_OUTPUT).toMatch("First Load JS shared by all");
69-
70-
// Next on Netlify output
71-
expect(BUILD_OUTPUT).toMatch("Next on Netlify");
72-
expect(BUILD_OUTPUT).toMatch("Success! All done!");
34+
expect(buildOutput).toMatch("Next on Netlify");
35+
expect(buildOutput).toMatch("Success! All done!");
7336
});
7437
});
7538

tests/defaults.test.js

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Test default next-on-netlify configuration
2+
23
const { parse, join } = require("path");
34
const {
4-
copySync,
5-
emptyDirSync,
65
existsSync,
76
readdirSync,
87
readFileSync,
98
readJsonSync,
109
} = require("fs-extra");
11-
const npmRunBuild = require("./helpers/npmRunBuild");
10+
const buildNextApp = require("./helpers/buildNextApp");
1211

1312
// The name of this test file (without extension)
1413
const FILENAME = parse(__filename).name;
@@ -18,70 +17,46 @@ const FILENAME = parse(__filename).name;
1817
// package.json file.
1918
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
2019

21-
// The directory that contains the fixtures, such as NextJS pages,
22-
// NextJS config, and package.json
23-
const FIXTURE_PATH = join(__dirname, "fixtures");
24-
25-
// Capture the output of `npm run build` to verify successful build
26-
let BUILD_OUTPUT;
20+
// Capture the output to verify successful build
21+
let buildOutput;
2722

2823
beforeAll(
2924
async () => {
30-
// Clear project directory
31-
emptyDirSync(PROJECT_PATH);
32-
emptyDirSync(join(PROJECT_PATH, "pages"));
33-
34-
// Copy NextJS pages and config
35-
copySync(join(FIXTURE_PATH, "pages"), join(PROJECT_PATH, "pages"));
36-
copySync(
37-
join(FIXTURE_PATH, "next.config.js"),
38-
join(PROJECT_PATH, "next.config.js")
39-
);
40-
41-
// Copy package.json
42-
copySync(
43-
join(FIXTURE_PATH, "package.json"),
44-
join(PROJECT_PATH, "package.json")
45-
);
46-
47-
// Copy image.png to public folder
48-
copySync(
49-
join(FIXTURE_PATH, "image.png"),
50-
join(PROJECT_PATH, "public", "image.png")
51-
);
52-
53-
// Invoke `npm run build`: Build Next and run next-on-netlify
54-
const { stdout } = await npmRunBuild({ directory: PROJECT_PATH });
55-
BUILD_OUTPUT = stdout;
25+
buildOutput = await buildNextApp()
26+
.forTest(__filename)
27+
.withPages("pages")
28+
.withNextConfig("next.config.js")
29+
.withPackageJson("package.json")
30+
.withFile("image.png", join("public", "image.png"))
31+
.build();
5632
},
5733
// time out after 180 seconds
5834
180 * 1000
5935
);
6036

61-
describe("Next", () => {
37+
describe("next-on-netlify", () => {
6238
test("builds successfully", () => {
63-
// NextJS output
64-
expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...");
65-
expect(BUILD_OUTPUT).toMatch("Finalizing page optimization...");
66-
expect(BUILD_OUTPUT).toMatch("First Load JS shared by all");
39+
expect(buildOutput).toMatch("Next on Netlify");
40+
expect(buildOutput).toMatch("Success! All done!");
41+
});
42+
});
6743

68-
// Next on Netlify output
69-
expect(BUILD_OUTPUT).toMatch("Next on Netlify");
70-
expect(BUILD_OUTPUT).toMatch("Copying public/ folder to out_publish/");
71-
expect(BUILD_OUTPUT).toMatch(
72-
"Copying static NextJS assets to out_publish/"
73-
);
74-
expect(BUILD_OUTPUT).toMatch(
44+
describe("next-on-netlify", () => {
45+
test("builds successfully", () => {
46+
expect(buildOutput).toMatch("Next on Netlify");
47+
expect(buildOutput).toMatch("Copying public/ folder to out_publish/");
48+
expect(buildOutput).toMatch("Copying static NextJS assets to out_publish/");
49+
expect(buildOutput).toMatch(
7550
"Setting up SSR pages and SSG pages with fallback as Netlify Functions in out_functions/"
7651
);
77-
expect(BUILD_OUTPUT).toMatch(
52+
expect(buildOutput).toMatch(
7853
"Copying pre-rendered SSG pages to out_publish/ and JSON data to out_publish/_next/data/"
7954
);
80-
expect(BUILD_OUTPUT).toMatch(
55+
expect(buildOutput).toMatch(
8156
"Writing pre-rendered HTML pages to out_publish/"
8257
);
83-
expect(BUILD_OUTPUT).toMatch("Setting up redirects");
84-
expect(BUILD_OUTPUT).toMatch("Success! All done!");
58+
expect(buildOutput).toMatch("Setting up redirects");
59+
expect(buildOutput).toMatch("Success! All done!");
8560
});
8661
});
8762

tests/fixtures/_redirects

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Custom Redirect Rules
2+
https://old.example.com/* https://new.example.com/:splat 301!

tests/fixtures/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "next-on-netlify-test",
33
"scripts": {
4-
"build": "../../../node_modules/.bin/next build",
5-
"postbuild": "node ../../../next-on-netlify"
4+
"next-build": "../../../../node_modules/.bin/next build",
5+
"next-on-netlify": "node ../../../next-on-netlify"
66
}
77
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const Index = () => <p>This page is pre-rendered.</p>;
2+
3+
export default Index;

0 commit comments

Comments
 (0)