|
1 |
| -import { type getDeployStore } from '@netlify/blobs' |
2 |
| -import { join } from 'node:path' |
3 |
| -import { beforeEach, expect, test, vi } from 'vitest' |
| 1 | +import type { NetlifyPluginOptions } from '@netlify/build' |
| 2 | +import glob from 'fast-glob' |
| 3 | +import { Mock, afterEach, beforeEach, expect, test, vi } from 'vitest' |
4 | 4 | import { mockFileSystem } from '../../../tests/index.js'
|
5 |
| -import { BUILD_DIR } from '../constants.js' |
6 |
| -import { copyStaticContent } from './static.js' |
7 |
| - |
8 |
| -vi.mock('node:fs', async () => { |
9 |
| - const unionFs: any = (await import('unionfs')).default |
10 |
| - const fs = await vi.importActual<typeof import('fs')>('node:fs') |
11 |
| - unionFs.reset = () => { |
12 |
| - unionFs.fss = [fs] |
13 |
| - } |
14 |
| - const united = unionFs.use(fs) |
15 |
| - return { default: united, ...united } |
16 |
| -}) |
| 5 | +import { FixtureTestContext, createFsFixture } from '../../../tests/utils/fixture.js' |
| 6 | +import { getBlobStore } from '../blob.js' |
| 7 | +import { STATIC_DIR } from '../constants.js' |
| 8 | +import { linkStaticAssets, uploadStaticContent } from './static.js' |
17 | 9 |
|
18 |
| -vi.mock('node:fs/promises', async () => { |
19 |
| - const fs = await import('node:fs') |
20 |
| - const { fsCpHelper, rmHelper } = await import('../../../tests/utils/fs-helper.js') |
21 |
| - return { |
22 |
| - ...fs.promises, |
23 |
| - rm: rmHelper, |
24 |
| - cp: fsCpHelper, |
25 |
| - } |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks() |
26 | 12 | })
|
27 | 13 |
|
28 |
| -let fakeBlob: ReturnType<typeof getDeployStore> |
| 14 | +vi.mock('../blob.js', () => ({ |
| 15 | + getBlobStore: vi.fn(), |
| 16 | +})) |
29 | 17 |
|
| 18 | +let mockBlobSet = vi.fn() |
30 | 19 | beforeEach(() => {
|
31 |
| - fakeBlob = { |
32 |
| - set: vi.fn(), |
33 |
| - } as unknown as ReturnType<typeof getDeployStore> |
| 20 | + ;(getBlobStore as Mock).mockReturnValue({ |
| 21 | + set: mockBlobSet, |
| 22 | + }) |
34 | 23 | })
|
35 | 24 |
|
36 |
| -test('should copy the static assets from the build to the publish directory', async () => { |
37 |
| - const { cwd, vol } = mockFileSystem({ |
38 |
| - [`${BUILD_DIR}/.next/static/test.js`]: '', |
39 |
| - [`${BUILD_DIR}/.next/static/sub-dir/test2.js`]: '', |
| 25 | +test('should clear the static directory contents', async () => { |
| 26 | + const PUBLISH_DIR = '.next' |
| 27 | + |
| 28 | + const { vol } = mockFileSystem({ |
| 29 | + [`${STATIC_DIR}/remove-me.js`]: '', |
40 | 30 | })
|
41 | 31 |
|
42 |
| - const PUBLISH_DIR = join(cwd, 'publish') |
43 |
| - await copyStaticContent({ PUBLISH_DIR }, fakeBlob) |
| 32 | + await linkStaticAssets({ |
| 33 | + constants: { PUBLISH_DIR }, |
| 34 | + } as Pick<NetlifyPluginOptions, 'constants'>) |
44 | 35 |
|
45 |
| - expect(fakeBlob.set).toHaveBeenCalledTimes(0) |
46 | 36 | expect(Object.keys(vol.toJSON())).toEqual(
|
| 37 | + expect.not.arrayContaining([`${STATIC_DIR}/remove-me.js`]), |
| 38 | + ) |
| 39 | +}) |
| 40 | + |
| 41 | +test<FixtureTestContext>('should link static content from the publish directory to the static directory', async (ctx) => { |
| 42 | + const PUBLISH_DIR = '.next' |
| 43 | + |
| 44 | + const { cwd } = await createFsFixture( |
| 45 | + { |
| 46 | + [`${PUBLISH_DIR}/static/test.js`]: '', |
| 47 | + [`${PUBLISH_DIR}/static/sub-dir/test2.js`]: '', |
| 48 | + }, |
| 49 | + ctx, |
| 50 | + ) |
| 51 | + |
| 52 | + await linkStaticAssets({ |
| 53 | + constants: { PUBLISH_DIR }, |
| 54 | + } as Pick<NetlifyPluginOptions, 'constants'>) |
| 55 | + |
| 56 | + const files = await glob('**/*', { cwd, dot: true }) |
| 57 | + |
| 58 | + expect(files).toEqual( |
47 | 59 | expect.arrayContaining([
|
48 |
| - `${PUBLISH_DIR}/_next/static/test.js`, |
49 |
| - `${PUBLISH_DIR}/_next/static/sub-dir/test2.js`, |
| 60 | + `${PUBLISH_DIR}/static/test.js`, |
| 61 | + `${PUBLISH_DIR}/static/sub-dir/test2.js`, |
| 62 | + `${STATIC_DIR}/_next/static/test.js`, |
| 63 | + `${STATIC_DIR}/_next/static/sub-dir/test2.js`, |
50 | 64 | ]),
|
51 | 65 | )
|
52 | 66 | })
|
53 | 67 |
|
54 |
| -test('should throw expected error if no static assets directory exists', async () => { |
55 |
| - const { cwd } = mockFileSystem({}) |
| 68 | +test<FixtureTestContext>('should link static content from the public directory to the static directory', async (ctx) => { |
| 69 | + const PUBLISH_DIR = '.next' |
56 | 70 |
|
57 |
| - const PUBLISH_DIR = join(cwd, 'publish') |
58 |
| - const staticDirectory = join(cwd, '.netlify/.next/static') |
| 71 | + const { cwd } = await createFsFixture( |
| 72 | + { |
| 73 | + 'public/fake-image.svg': '', |
| 74 | + 'public/another-asset.json': '', |
| 75 | + }, |
| 76 | + ctx, |
| 77 | + ) |
59 | 78 |
|
60 |
| - await expect(copyStaticContent({ PUBLISH_DIR }, fakeBlob)).rejects.toThrowError( |
61 |
| - `Failed to copy static assets: Error: ENOENT: no such file or directory, readdir '${staticDirectory}'`, |
| 79 | + await linkStaticAssets({ |
| 80 | + constants: { PUBLISH_DIR }, |
| 81 | + } as Pick<NetlifyPluginOptions, 'constants'>) |
| 82 | + |
| 83 | + const files = await glob('**/*', { cwd, dot: true }) |
| 84 | + |
| 85 | + expect(files).toEqual( |
| 86 | + expect.arrayContaining([ |
| 87 | + 'public/another-asset.json', |
| 88 | + 'public/fake-image.svg', |
| 89 | + `${STATIC_DIR}/another-asset.json`, |
| 90 | + `${STATIC_DIR}/fake-image.svg`, |
| 91 | + ]), |
62 | 92 | )
|
63 | 93 | })
|
64 | 94 |
|
65 |
| -test('should copy files from the public directory to the publish directory', async () => { |
66 |
| - const { cwd, vol } = mockFileSystem({ |
67 |
| - [`${BUILD_DIR}/.next/static/test.js`]: '', |
68 |
| - 'public/fake-image.svg': '', |
69 |
| - 'public/another-asset.json': '', |
70 |
| - }) |
| 95 | +test<FixtureTestContext>('should copy the static pages to the publish directory if the routes do not exist in the prerender-manifest', async (ctx) => { |
| 96 | + const PUBLISH_DIR = '.next' |
| 97 | + |
| 98 | + const { cwd } = await createFsFixture( |
| 99 | + { |
| 100 | + [`${PUBLISH_DIR}/prerender-manifest.json`]: JSON.stringify({ |
| 101 | + routes: {}, |
| 102 | + }), |
| 103 | + [`${PUBLISH_DIR}/static/test.js`]: '', |
| 104 | + [`${PUBLISH_DIR}/server/pages/test.html`]: 'test-1', |
| 105 | + [`${PUBLISH_DIR}/server/pages/test2.html`]: 'test-2', |
| 106 | + }, |
| 107 | + ctx, |
| 108 | + ) |
71 | 109 |
|
72 |
| - const PUBLISH_DIR = join(cwd, 'publish') |
73 |
| - await copyStaticContent({ PUBLISH_DIR }, fakeBlob) |
| 110 | + await uploadStaticContent({ |
| 111 | + constants: { PUBLISH_DIR }, |
| 112 | + } as Pick<NetlifyPluginOptions, 'constants'>) |
74 | 113 |
|
75 |
| - expect(Object.keys(vol.toJSON())).toEqual( |
76 |
| - expect.arrayContaining([`${PUBLISH_DIR}/fake-image.svg`, `${PUBLISH_DIR}/another-asset.json`]), |
77 |
| - ) |
| 114 | + expect(mockBlobSet).toHaveBeenCalledTimes(2) |
| 115 | + expect(mockBlobSet).toHaveBeenCalledWith('server/pages/test.html', 'test-1') |
| 116 | + expect(mockBlobSet).toHaveBeenCalledWith('server/pages/test2.html', 'test-2') |
78 | 117 | })
|
79 | 118 |
|
80 |
| -test('should not copy files if the public directory does not exist', async () => { |
81 |
| - const { cwd, vol } = mockFileSystem({ |
82 |
| - [`${BUILD_DIR}/.next/static/test.js`]: '', |
83 |
| - }) |
| 119 | +test<FixtureTestContext>('should not copy the static pages to the publish directory if the routes exist in the prerender-manifest', async (ctx) => { |
| 120 | + const PUBLISH_DIR = '.next' |
| 121 | + |
| 122 | + const { cwd } = await createFsFixture( |
| 123 | + { |
| 124 | + [`${PUBLISH_DIR}/prerender-manifest.json`]: JSON.stringify({ |
| 125 | + routes: { |
| 126 | + '/test': {}, |
| 127 | + '/test2': {}, |
| 128 | + }, |
| 129 | + }), |
| 130 | + [`${PUBLISH_DIR}/static/test.js`]: '', |
| 131 | + [`${PUBLISH_DIR}/server/pages/test.html`]: '', |
| 132 | + [`${PUBLISH_DIR}/server/pages/test2.html`]: '', |
| 133 | + }, |
| 134 | + ctx, |
| 135 | + ) |
84 | 136 |
|
85 |
| - const PUBLISH_DIR = join(cwd, 'publish') |
86 |
| - await expect(copyStaticContent({ PUBLISH_DIR }, fakeBlob)).resolves.toBeUndefined() |
| 137 | + await uploadStaticContent({ |
| 138 | + constants: { PUBLISH_DIR }, |
| 139 | + } as Pick<NetlifyPluginOptions, 'constants'>) |
87 | 140 |
|
88 |
| - expect(vol.toJSON()).toEqual({ |
89 |
| - [join(cwd, `${BUILD_DIR}/.next/static/test.js`)]: '', |
90 |
| - [`${PUBLISH_DIR}/_next/static/test.js`]: '', |
91 |
| - }) |
| 141 | + expect(mockBlobSet).not.toHaveBeenCalled() |
92 | 142 | })
|
0 commit comments