|
| 1 | +// Test that next-on-netlify does not crash when pre-rendering index.js file |
| 2 | +// with getStaticProps. |
| 3 | + |
| 4 | +const { parse, join } = require('path') |
| 5 | +const { copySync, emptyDirSync, existsSync, |
| 6 | + readdirSync, readFileSync, readJsonSync } = require('fs-extra') |
| 7 | +const npmRunBuild = require("./helpers/npmRunBuild") |
| 8 | + |
| 9 | +// The name of this test file (without extension) |
| 10 | +const FILENAME = parse(__filename).name |
| 11 | + |
| 12 | +// The directory which will be used for testing. |
| 13 | +// We simulate a NextJS app within that directory, with pages, and a |
| 14 | +// package.json file. |
| 15 | +const PROJECT_PATH = join(__dirname, "builds", FILENAME) |
| 16 | + |
| 17 | +// The directory that contains the fixtures, such as NextJS pages, |
| 18 | +// NextJS config, and package.json |
| 19 | +const FIXTURE_PATH = join(__dirname, "fixtures") |
| 20 | + |
| 21 | +// Capture the output of `npm run build` to verify successful build |
| 22 | +let BUILD_OUTPUT |
| 23 | + |
| 24 | +beforeAll( |
| 25 | + async () => { |
| 26 | + // Clear project directory |
| 27 | + emptyDirSync(PROJECT_PATH) |
| 28 | + emptyDirSync(join(PROJECT_PATH, "pages")) |
| 29 | + |
| 30 | + // Copy NextJS pages and config |
| 31 | + copySync( |
| 32 | + join(FIXTURE_PATH, "pages-with-static-props-index"), |
| 33 | + join(PROJECT_PATH, "pages") |
| 34 | + ) |
| 35 | + copySync( |
| 36 | + join(FIXTURE_PATH, "next.config.js"), |
| 37 | + join(PROJECT_PATH, "next.config.js") |
| 38 | + ) |
| 39 | + |
| 40 | + // Copy package.json |
| 41 | + copySync( |
| 42 | + join(FIXTURE_PATH, "package.json"), |
| 43 | + join(PROJECT_PATH, "package.json") |
| 44 | + ) |
| 45 | + |
| 46 | + // Invoke `npm run build`: Build Next and run next-on-netlify |
| 47 | + const { stdout } = await npmRunBuild({ directory: PROJECT_PATH }) |
| 48 | + BUILD_OUTPUT = stdout |
| 49 | + }, |
| 50 | + // time out after 180 seconds |
| 51 | + 180 * 1000 |
| 52 | +) |
| 53 | + |
| 54 | +describe('Next', () => { |
| 55 | + test('builds successfully', () => { |
| 56 | + // NextJS output |
| 57 | + expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...") |
| 58 | + expect(BUILD_OUTPUT).toMatch("Automatically optimizing pages...") |
| 59 | + expect(BUILD_OUTPUT).toMatch("First Load JS shared by all") |
| 60 | + |
| 61 | + // Next on Netlify output |
| 62 | + expect(BUILD_OUTPUT).toMatch("Next on Netlify") |
| 63 | + expect(BUILD_OUTPUT).toMatch("Success! All done!") |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +describe('Static Pages', () => { |
| 68 | + test('copies static pages to output directory', () => { |
| 69 | + const OUTPUT_PATH = join(PROJECT_PATH, "out_publish") |
| 70 | + |
| 71 | + expect(existsSync(join(OUTPUT_PATH, "index.html"))).toBe(true) |
| 72 | + expect(existsSync(join(OUTPUT_PATH, "static.html"))).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + test('copies static assets to out_publish/_next/ directory', () => { |
| 76 | + const dirs = readdirSync(join(PROJECT_PATH, "out_publish", "_next", "static")) |
| 77 | + |
| 78 | + expect(dirs.length).toBe(3) |
| 79 | + expect(dirs).toContain("chunks") |
| 80 | + expect(dirs).toContain("runtime") |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +describe('404 Page', () => { |
| 85 | + test('copies 404.html to output directory', () => { |
| 86 | + const OUTPUT_PATH = join(PROJECT_PATH, "out_publish") |
| 87 | + |
| 88 | + expect(existsSync(join(OUTPUT_PATH, "404.html"))).toBe(true) |
| 89 | + }) |
| 90 | + |
| 91 | + // This is required for 404.html to work on netlify-dev |
| 92 | + test('copies 404.html to directory root', () => { |
| 93 | + expect(existsSync(join(PROJECT_PATH, "404.html"))).toBe(true) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +describe('Routing',() => { |
| 98 | + test('creates Netlify redirects', async () => { |
| 99 | + // Read _redirects file |
| 100 | + const contents = readFileSync(join(PROJECT_PATH, "out_publish", "_redirects")) |
| 101 | + |
| 102 | + // Convert contents into an array, each line being one element |
| 103 | + const redirects = contents.toString().split("\n") |
| 104 | + |
| 105 | + // Check that routes are present |
| 106 | + expect(redirects).toContain("/ /index.html 200") |
| 107 | + expect(redirects).toContain("/index /index.html 200") |
| 108 | + expect(redirects).toContain("/static /static.html 200") |
| 109 | + }) |
| 110 | +}) |
0 commit comments