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