|
| 1 | +// Test next-on-netlify when config is set from a function in next.config.js |
| 2 | +// See: https://github.com/netlify/next-on-netlify/issues/25 |
| 3 | + |
| 4 | +const { parse, join } = require("path"); |
| 5 | +const { existsSync, readdirSync, readFileSync } = require("fs-extra"); |
| 6 | +const buildNextApp = require("./helpers/buildNextApp"); |
| 7 | + |
| 8 | +// The name of this test file (without extension) |
| 9 | +const FILENAME = parse(__filename).name; |
| 10 | + |
| 11 | +// The directory which will be used for testing. |
| 12 | +// We simulate a NextJS app within that directory, with pages, and a |
| 13 | +// package.json file. |
| 14 | +const PROJECT_PATH = join(__dirname, "builds", FILENAME); |
| 15 | +const FUNCTIONS_DIR = "my-functions"; |
| 16 | +const PUBLISH_DIR = "my-publish"; |
| 17 | + |
| 18 | +// Capture the output to verify successful build |
| 19 | +let buildOutput; |
| 20 | + |
| 21 | +beforeAll( |
| 22 | + async () => { |
| 23 | + runOutput = await buildNextApp() |
| 24 | + .forTest(__filename) |
| 25 | + .withPages("pages") |
| 26 | + .withNextConfig("next.config.js") |
| 27 | + .withPackageJson("package.json") |
| 28 | + .runWithRequire({ functionsDir: FUNCTIONS_DIR, publishDir: PUBLISH_DIR }); |
| 29 | + }, |
| 30 | + // time out after 180 seconds |
| 31 | + 180 * 1000 |
| 32 | +); |
| 33 | + |
| 34 | +describe("next-on-netlify", () => { |
| 35 | + const functionsDir = join(PROJECT_PATH, FUNCTIONS_DIR); |
| 36 | + |
| 37 | + test("builds successfully", () => { |
| 38 | + expect(runOutput).toMatch("Built successfully!"); |
| 39 | + }); |
| 40 | + |
| 41 | + test("creates a Netlify Function for each SSR page", () => { |
| 42 | + expect(existsSync(join(functionsDir, "next_index", "next_index.js"))).toBe( |
| 43 | + true |
| 44 | + ); |
| 45 | + expect( |
| 46 | + existsSync(join(functionsDir, "next_shows_id", "next_shows_id.js")) |
| 47 | + ).toBe(true); |
| 48 | + expect( |
| 49 | + existsSync( |
| 50 | + join(functionsDir, "next_shows_params", "next_shows_params.js") |
| 51 | + ) |
| 52 | + ).toBe(true); |
| 53 | + expect( |
| 54 | + existsSync( |
| 55 | + join( |
| 56 | + functionsDir, |
| 57 | + "next_getServerSideProps_static", |
| 58 | + "next_getServerSideProps_static.js" |
| 59 | + ) |
| 60 | + ) |
| 61 | + ).toBe(true); |
| 62 | + expect( |
| 63 | + existsSync( |
| 64 | + join( |
| 65 | + functionsDir, |
| 66 | + "next_getServerSideProps_id", |
| 67 | + "next_getServerSideProps_id.js" |
| 68 | + ) |
| 69 | + ) |
| 70 | + ).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + test("copies static pages to output directory", () => { |
| 74 | + const OUTPUT_PATH = join(PROJECT_PATH, PUBLISH_DIR); |
| 75 | + |
| 76 | + expect(existsSync(join(OUTPUT_PATH, "static.html"))).toBe(true); |
| 77 | + expect(existsSync(join(OUTPUT_PATH, "static/[id].html"))).toBe(true); |
| 78 | + }); |
| 79 | + |
| 80 | + test("copies static assets to out_publish/_next/ directory", () => { |
| 81 | + const dirs = readdirSync( |
| 82 | + join(PROJECT_PATH, PUBLISH_DIR, "_next", "static") |
| 83 | + ); |
| 84 | + |
| 85 | + expect(dirs.length).toBe(2); |
| 86 | + expect(dirs).toContain("chunks"); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("clean up of NoN files", () => { |
| 91 | + test("creates a .nonfiletracking to audit NoN-specific files between builds", () => { |
| 92 | + const cacheDir = join(PROJECT_PATH, "/node_modules/.cache/next-on-netlify"); |
| 93 | + const dirs = readdirSync(cacheDir); |
| 94 | + expect(dirs[0]).toEqual(".nonfiletracking"); |
| 95 | + }); |
| 96 | + |
| 97 | + test(".nonfiletracking contains NoN-specific files", () => { |
| 98 | + const cacheDir = join(PROJECT_PATH, "/node_modules/.cache/next-on-netlify"); |
| 99 | + const fileList = readFileSync(join(cacheDir, ".nonfiletracking"), "utf8"); |
| 100 | + // had to test equality this way because of windows :) |
| 101 | + const isSameList = (arr1, arr2) => |
| 102 | + arr1.reduce((isSame, func) => { |
| 103 | + if (arr2.includes(func)) { |
| 104 | + isSame = true; |
| 105 | + } else { |
| 106 | + isSame = false; |
| 107 | + } |
| 108 | + return isSame; |
| 109 | + }, true); |
| 110 | + const nextFunctions = [ |
| 111 | + "next_api_shows_id", |
| 112 | + "next_api_shows_params", |
| 113 | + "next_api_static", |
| 114 | + "next_getServerSideProps_all_slug", |
| 115 | + "next_getServerSideProps_id", |
| 116 | + "next_getServerSideProps_static", |
| 117 | + "next_getStaticProps_id", |
| 118 | + "next_getStaticProps_static", |
| 119 | + "next_getStaticProps_withFallback_id", |
| 120 | + "next_getStaticProps_withFallback_slug", |
| 121 | + "next_getStaticProps_withRevalidate_id", |
| 122 | + "next_getStaticProps_withRevalidate_withFallback_id", |
| 123 | + "next_getStaticProps_withrevalidate", |
| 124 | + "next_index", |
| 125 | + "next_shows_id", |
| 126 | + "next_shows_params", |
| 127 | + ]; |
| 128 | + const fileListFunctions = fileList.split("---")[0].trim().split("\n"); |
| 129 | + expect(isSameList(nextFunctions, fileListFunctions)).toBe(true); |
| 130 | + const publishFiles = [ |
| 131 | + "404.html", |
| 132 | + "_next", |
| 133 | + "_redirects", |
| 134 | + "getStaticProps", |
| 135 | + "static", |
| 136 | + "static.html", |
| 137 | + ]; |
| 138 | + const fileListPublish = fileList.split("---")[1].trim().split("\n"); |
| 139 | + expect(isSameList(publishFiles, fileListPublish)).toBe(true); |
| 140 | + }); |
| 141 | +}); |
0 commit comments