|
| 1 | +import { fs, vol } from 'memfs' |
| 2 | +import { Mock, TestContext, afterEach, assert, beforeEach, expect, test, vi } from 'vitest' |
| 3 | + |
| 4 | +import { NetlifyPluginUtils } from '@netlify/build' |
| 5 | +import { join } from 'node:path' |
| 6 | +import { mockFileSystem } from '../../tests/index.js' |
| 7 | +import { moveBuildOutput } from './move-build-output.js' |
| 8 | + |
| 9 | +vi.mock('node:fs', () => fs) |
| 10 | +vi.mock('node:fs/promises', () => fs.promises) |
| 11 | + |
| 12 | +type Context = TestContext & { |
| 13 | + utils: { |
| 14 | + build: { |
| 15 | + failBuild: Mock<any, any> |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +beforeEach<Context>((ctx) => { |
| 21 | + ctx.utils = { |
| 22 | + build: { |
| 23 | + failBuild: vi.fn().mockImplementation((msg, { error } = {}) => { |
| 24 | + assert.fail(`${msg}: ${error || ''}`) |
| 25 | + }), |
| 26 | + }, |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + vol.reset() |
| 32 | + vi.restoreAllMocks() |
| 33 | +}) |
| 34 | + |
| 35 | +test<Context>('should fail the build and throw an error if the PUBLISH_DIR does not exist', async (ctx) => { |
| 36 | + const cwd = mockFileSystem({}) |
| 37 | + ctx.utils.build.failBuild.mockImplementation((msg) => { |
| 38 | + throw new Error(msg) |
| 39 | + }) |
| 40 | + |
| 41 | + try { |
| 42 | + await moveBuildOutput( |
| 43 | + { PUBLISH_DIR: join(cwd, 'does-not-exist') }, |
| 44 | + ctx.utils as unknown as NetlifyPluginUtils, |
| 45 | + ) |
| 46 | + } catch (err) { |
| 47 | + expect(err).toBeInstanceOf(Error) |
| 48 | + if (err instanceof Error) { |
| 49 | + expect(err.message).toEqual( |
| 50 | + `Your publish directory does not exist. Please check your netlify.toml file.`, |
| 51 | + ) |
| 52 | + } |
| 53 | + expect(ctx.utils.build.failBuild).toHaveBeenCalledTimes(1) |
| 54 | + } finally { |
| 55 | + expect.assertions(3) |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +test<Context>('should move the build output to the `.netlify/.next` folder', async (ctx) => { |
| 60 | + const cwd = mockFileSystem({ |
| 61 | + 'out/fake-file.js': '', |
| 62 | + }) |
| 63 | + |
| 64 | + await moveBuildOutput( |
| 65 | + { PUBLISH_DIR: join(cwd, 'out') }, |
| 66 | + ctx.utils as unknown as NetlifyPluginUtils, |
| 67 | + ) |
| 68 | + |
| 69 | + expect(vol.toJSON()).toEqual({ |
| 70 | + [join(cwd, '.netlify/.next/fake-file.js')]: '', |
| 71 | + [join(cwd, 'out')]: null, |
| 72 | + }) |
| 73 | +}) |
0 commit comments