|
| 1 | +// Test next-on-netlify when a custom distDir is set in next.config.js |
| 2 | +const { parse, join } = require('path') |
| 3 | +const { copySync, emptyDirSync, readFileSync, writeFileSync } = require('fs-extra') |
| 4 | +const npmRunBuild = require("./helpers/npmRunBuild") |
| 5 | + |
| 6 | +// The name of this test file (without extension) |
| 7 | +const FILENAME = parse(__filename).name |
| 8 | + |
| 9 | +// The directory which will be used for testing. |
| 10 | +// We simulate a NextJS app within that directory, with pages, and a |
| 11 | +// package.json file. |
| 12 | +const PROJECT_PATH = join(__dirname, "builds", FILENAME) |
| 13 | + |
| 14 | +// The directory that contains the fixtures, such as NextJS pages, |
| 15 | +// NextJS config, and package.json |
| 16 | +const FIXTURE_PATH = join(__dirname, "fixtures") |
| 17 | + |
| 18 | +// Capture the output of `npm run build` to verify successful build |
| 19 | +let BUILD_OUTPUT |
| 20 | + |
| 21 | +beforeAll( |
| 22 | + async () => { |
| 23 | + // Clear project directory |
| 24 | + emptyDirSync(PROJECT_PATH) |
| 25 | + emptyDirSync(join(PROJECT_PATH, "pages")) |
| 26 | + |
| 27 | + // Copy NextJS pages and config |
| 28 | + copySync( |
| 29 | + join(FIXTURE_PATH, "pages-with-static-props-index"), |
| 30 | + join(PROJECT_PATH, "pages") |
| 31 | + ) |
| 32 | + copySync( |
| 33 | + join(FIXTURE_PATH, "next.config.js"), |
| 34 | + join(PROJECT_PATH, "next.config.js") |
| 35 | + ) |
| 36 | + |
| 37 | + // Copy package.json |
| 38 | + copySync( |
| 39 | + join(FIXTURE_PATH, "package.json"), |
| 40 | + join(PROJECT_PATH, "package.json") |
| 41 | + ) |
| 42 | + |
| 43 | + // Create a _redirects file |
| 44 | + writeFileSync( |
| 45 | + join(PROJECT_PATH, "_redirects"), |
| 46 | + "# Custom Redirect Rules\n" + |
| 47 | + "https://old.example.com/* https://new.example.com/:splat 301!\n" |
| 48 | + ) |
| 49 | + |
| 50 | + // Invoke `npm run build`: Build Next and run next-on-netlify |
| 51 | + const { stdout } = await npmRunBuild({ directory: PROJECT_PATH }) |
| 52 | + BUILD_OUTPUT = stdout |
| 53 | + }, |
| 54 | + // time out after 180 seconds |
| 55 | + 180 * 1000 |
| 56 | +) |
| 57 | + |
| 58 | +describe('Next', () => { |
| 59 | + test('builds successfully', () => { |
| 60 | + // NextJS output |
| 61 | + expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...") |
| 62 | + expect(BUILD_OUTPUT).toMatch("Automatically optimizing pages...") |
| 63 | + expect(BUILD_OUTPUT).toMatch("First Load JS shared by all") |
| 64 | + |
| 65 | + // Next on Netlify output |
| 66 | + expect(BUILD_OUTPUT).toMatch("Next on Netlify") |
| 67 | + expect(BUILD_OUTPUT).toMatch("Success! All done!") |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +describe('Routing',() => { |
| 72 | + test('includes custom redirect rules', async () => { |
| 73 | + // Read _redirects file |
| 74 | + const contents = readFileSync(join(PROJECT_PATH, "out_publish", "_redirects")) |
| 75 | + |
| 76 | + const redirects = contents.toString().split(/\n/) |
| 77 | + expect(redirects[0]).toEqual("# Custom Redirect Rules") |
| 78 | + expect(redirects[1]).toEqual("https://old.example.com/* https://new.example.com/:splat 301!") |
| 79 | + |
| 80 | + // Check that other routes are present |
| 81 | + expect(redirects).toContain("/ /index.html 200") |
| 82 | + expect(redirects).toContain("/index /index.html 200") |
| 83 | + }) |
| 84 | +}) |
0 commit comments