|
| 1 | +// Test that next-on-netlify does not crash with a getServerSideProps index.js file. |
| 2 | +// See: https://github.com/netlify/next-on-netlify/pull/39 |
| 3 | + |
| 4 | +const { parse, join } = require("path"); |
| 5 | +const { |
| 6 | + copySync, |
| 7 | + emptyDirSync, |
| 8 | + existsSync, |
| 9 | + readdirSync, |
| 10 | + readFileSync, |
| 11 | + readJsonSync, |
| 12 | +} = require("fs-extra"); |
| 13 | +const npmRunBuild = require("./helpers/npmRunBuild"); |
| 14 | + |
| 15 | +// The name of this test file (without extension) |
| 16 | +const FILENAME = parse(__filename).name; |
| 17 | + |
| 18 | +// The directory which will be used for testing. |
| 19 | +// We simulate a NextJS app within that directory, with pages, and a |
| 20 | +// package.json file. |
| 21 | +const PROJECT_PATH = join(__dirname, "builds", FILENAME); |
| 22 | + |
| 23 | +// The directory that contains the fixtures, such as NextJS pages, |
| 24 | +// NextJS config, and package.json |
| 25 | +const FIXTURE_PATH = join(__dirname, "fixtures"); |
| 26 | + |
| 27 | +// Capture the output of `npm run build` to verify successful build |
| 28 | +let BUILD_OUTPUT; |
| 29 | + |
| 30 | +beforeAll( |
| 31 | + async () => { |
| 32 | + // Clear project directory |
| 33 | + emptyDirSync(PROJECT_PATH); |
| 34 | + emptyDirSync(join(PROJECT_PATH, "pages")); |
| 35 | + |
| 36 | + // Copy NextJS pages and config |
| 37 | + copySync( |
| 38 | + join(FIXTURE_PATH, "pages-with-gssp-index"), |
| 39 | + join(PROJECT_PATH, "pages") |
| 40 | + ); |
| 41 | + copySync( |
| 42 | + join(FIXTURE_PATH, "next.config.js"), |
| 43 | + join(PROJECT_PATH, "next.config.js") |
| 44 | + ); |
| 45 | + |
| 46 | + // Copy package.json |
| 47 | + copySync( |
| 48 | + join(FIXTURE_PATH, "package.json"), |
| 49 | + join(PROJECT_PATH, "package.json") |
| 50 | + ); |
| 51 | + |
| 52 | + // Invoke `npm run build`: Build Next and run next-on-netlify |
| 53 | + const { stdout } = await npmRunBuild({ directory: PROJECT_PATH }); |
| 54 | + BUILD_OUTPUT = stdout; |
| 55 | + }, |
| 56 | + // time out after 180 seconds |
| 57 | + 180 * 1000 |
| 58 | +); |
| 59 | + |
| 60 | +describe("Next", () => { |
| 61 | + test("builds successfully", () => { |
| 62 | + // NextJS output |
| 63 | + expect(BUILD_OUTPUT).toMatch("Creating an optimized production build..."); |
| 64 | + expect(BUILD_OUTPUT).toMatch("Finalizing page optimization..."); |
| 65 | + expect(BUILD_OUTPUT).toMatch("First Load JS shared by all"); |
| 66 | + |
| 67 | + // Next on Netlify output |
| 68 | + expect(BUILD_OUTPUT).toMatch("Next on Netlify"); |
| 69 | + expect(BUILD_OUTPUT).toMatch("Success! All done!"); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("Routing", () => { |
| 74 | + test("it routes page and data route to Netlify Function", async () => { |
| 75 | + // Read _redirects file |
| 76 | + let contents = readFileSync( |
| 77 | + join(PROJECT_PATH, "out_publish", "_redirects") |
| 78 | + ).toString(); |
| 79 | + |
| 80 | + // Replace non-persistent build ID with placeholder |
| 81 | + contents = contents.replace( |
| 82 | + /\/_next\/data\/[^\/]+\//g, |
| 83 | + "/_next/data/%BUILD_ID%/" |
| 84 | + ); |
| 85 | + |
| 86 | + const redirects = contents.trim().split(/\n/); |
| 87 | + |
| 88 | + expect(redirects[0]).toEqual("# Next-on-Netlify Redirects"); |
| 89 | + expect(redirects[1]).toEqual("/ /.netlify/functions/next_index 200"); |
| 90 | + expect(redirects[2]).toEqual( |
| 91 | + "/_next/data/%BUILD_ID%/index.json /.netlify/functions/next_index 200" |
| 92 | + ); |
| 93 | + |
| 94 | + // Check that no other redirects are present |
| 95 | + expect(redirects).toHaveLength(3); |
| 96 | + }); |
| 97 | +}); |
0 commit comments