-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathstatic.test.ts
165 lines (141 loc) · 5.92 KB
/
static.test.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { load } from 'cheerio'
import glob from 'fast-glob'
import { getLogger } from 'lambda-local'
import { existsSync } from 'node:fs'
import { join } from 'node:path'
import { v4 } from 'uuid'
import { beforeEach, expect, test, vi } from 'vitest'
import { type FixtureTestContext } from '../utils/contexts.js'
import { createFixture, invokeFunction, runPlugin, runPluginStep } from '../utils/fixture.js'
import {
decodeBlobKey,
generateRandomObjectID,
getBlobEntries,
startMockBlobStore,
} from '../utils/helpers.js'
import { nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
// Disable the verbose logging of the lambda-local runtime
getLogger().level = 'alert'
beforeEach<FixtureTestContext>(async (ctx) => {
// set for each test a new deployID and siteID
ctx.deployID = generateRandomObjectID()
ctx.siteID = v4()
vi.stubEnv('DEPLOY_ID', ctx.deployID)
// hide debug logs in tests
// vi.spyOn(console, 'debug').mockImplementation(() => {})
await startMockBlobStore(ctx)
})
test<FixtureTestContext>('requesting a non existing page route that needs to be fetched from the CDN', async (ctx) => {
await createFixture('page-router', ctx)
await runPlugin(ctx)
const entries = await getBlobEntries(ctx)
expect(entries.map(({ key }) => decodeBlobKey(key.substring(0, 50))).sort()).toEqual([
'/products/an-incredibly-long-product-',
'/products/prerendered',
'/products/事前レンダリング,te',
'/static/revalidate-automatic',
'/static/revalidate-manual',
'/static/revalidate-slow',
'/static/revalidate-slow-data',
'404.html',
'500.html',
'static/fully-static.html',
// the real key is much longer and ends in a hash, but we only assert on the first 50 chars to make it easier
])
// test that it should request the 404.html file
const call1 = await invokeFunction(ctx, { url: 'static/revalidate-not-existing' })
expect(call1.statusCode).toBe(404)
expect(load(call1.body)('h1').text()).toBe('404')
// https://github.com/vercel/next.js/pull/69802 made changes to returned cache-control header,
// after that (14.2.10 and canary.147) 404 pages would have `private` directive, before that it
// would not
const shouldHavePrivateDirective = nextVersionSatisfies('^14.2.10 || >=15.0.0-canary.147')
expect(call1.headers, 'a cache hit on the first invocation of a prerendered page').toEqual(
expect.objectContaining({
'netlify-cdn-cache-control':
(shouldHavePrivateDirective ? 'private, ' : '') +
'no-cache, no-store, max-age=0, must-revalidate, durable',
}),
)
})
test<FixtureTestContext>('linked static resources are placed in correct place in publish directory (no basePath)', async (ctx) => {
await createFixture('simple', ctx)
const {
constants: { PUBLISH_DIR },
} = await runPlugin(ctx)
const publishDirAfterBuild = (
await glob('**/*', { cwd: join(ctx.cwd, PUBLISH_DIR), dot: true, absolute: true })
).sort()
await runPluginStep(ctx, 'onPostBuild')
// fetch index page
const call1 = await invokeFunction(ctx, { url: '/' })
expect(call1.statusCode).toBe(200)
const document = load(call1.body)
// collect linked resources - those should
// contain scripts and an image (can contain duplicates)
const resourcesPaths = [
...Array.from(document('script[src]')).map((elem) => {
return elem.attribs.src
}),
...Array.from(document('link[href]')).map((elem) => {
return elem.attribs.href
}),
...Array.from(document('img[src]')).map((elem) => {
return elem.attribs.src
}),
]
// To make sure test works as expected, we will check if we found
// at least one script and one image
expect(resourcesPaths.find((path) => path.endsWith('.js'))).not.toBeUndefined()
expect(resourcesPaths.find((path) => path.endsWith('.jpg'))).not.toBeUndefined()
// check if linked resources are accessible in publish dir in expected locations
for (const path of resourcesPaths) {
expect(existsSync(join(ctx.cwd, PUBLISH_DIR, path))).toBe(true)
}
// check if we restore publish dir to its original state
await runPluginStep(ctx, 'onEnd')
expect(
(await glob('**/*', { cwd: join(ctx.cwd, PUBLISH_DIR), dot: true, absolute: true })).sort(),
).toEqual(publishDirAfterBuild)
})
test<FixtureTestContext>('linked static resources are placed in correct place in publish directory (with basePath)', async (ctx) => {
await createFixture('base-path', ctx)
const {
constants: { PUBLISH_DIR },
} = await runPlugin(ctx)
console.log(PUBLISH_DIR)
const publishDirAfterBuild = (
await glob('**/*', { cwd: join(ctx.cwd, PUBLISH_DIR), dot: true, absolute: true })
).sort()
await runPluginStep(ctx, 'onPostBuild')
// fetch index page
const call1 = await invokeFunction(ctx, { url: 'base/path' })
expect(call1.statusCode).toBe(200)
const document = load(call1.body)
// collect linked resources - those should
// contain scripts and an image (can contain duplicates because of preload links)
const resourcesPaths = [
...Array.from(document('script[src]')).map((elem) => {
return elem.attribs.src
}),
...Array.from(document('link[href]')).map((elem) => {
return elem.attribs.href
}),
...Array.from(document('img[src]')).map((elem) => {
return elem.attribs.src
}),
]
// To make sure test works as expected, we will check if we found
// at least one script and one image
expect(resourcesPaths.find((path) => path.endsWith('.js'))).not.toBeUndefined()
expect(resourcesPaths.find((path) => path.endsWith('.jpg'))).not.toBeUndefined()
// check if linked resources are accessible in publish dir in expected locations
for (const path of resourcesPaths) {
expect(existsSync(join(ctx.cwd, PUBLISH_DIR, path))).toBe(true)
}
// check if we restore publish dir to its original state
await runPluginStep(ctx, 'onEnd')
expect(
(await glob('**/*', { cwd: join(ctx.cwd, PUBLISH_DIR), dot: true, absolute: true })).sort(),
).toEqual(publishDirAfterBuild)
})