|
| 1 | +import { checkNextSiteHasBuilt } from '../../plugin/src/helpers/verification' |
| 2 | +import { outdent } from 'outdent' |
| 3 | + |
| 4 | +import type { NetlifyPluginUtils } from '@netlify/build' |
| 5 | +type FailBuild = NetlifyPluginUtils['build']['failBuild'] |
| 6 | + |
| 7 | +jest.mock('fs', () => { |
| 8 | + return { |
| 9 | + existsSync: jest.fn() |
| 10 | + } |
| 11 | +}) |
| 12 | + |
| 13 | +describe('checkNextSiteHasBuilt', () => { |
| 14 | + let failBuildMock |
| 15 | + const { existsSync } = require('fs') |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + failBuildMock = (jest.fn() as unknown) as FailBuild |
| 19 | + }) |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + jest.clearAllMocks() |
| 23 | + jest.resetAllMocks() |
| 24 | + }) |
| 25 | + |
| 26 | + it('returns error message about incorrectly publishing the ".next" directory when "next export" was run', () => { |
| 27 | + existsSync.mockReturnValue(true) |
| 28 | + |
| 29 | + const expectedFailureMessage = outdent` |
| 30 | + Detected that "next export" was run, but site is incorrectly publishing the ".next" directory. |
| 31 | + The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". |
| 32 | + ` |
| 33 | + |
| 34 | + checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock }) |
| 35 | + |
| 36 | + expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) |
| 37 | + }) |
| 38 | + |
| 39 | + it('returns error message prompt to change publish directory to ".next"', () => { |
| 40 | + // False for not initially finding the specified 'publish' directory, |
| 41 | + // True for successfully finding a '.next' directory with a production build |
| 42 | + existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true) |
| 43 | + |
| 44 | + const expectedFailureMessage = outdent` |
| 45 | + The directory "someCustomDir" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. |
| 46 | + However, a '.next' directory was found with a production build. |
| 47 | + Consider changing your 'publish' directory to '.next' |
| 48 | + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". |
| 49 | + ` |
| 50 | + |
| 51 | + checkNextSiteHasBuilt({ publish: 'someCustomDir', failBuild: failBuildMock }) |
| 52 | + |
| 53 | + expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) |
| 54 | + }) |
| 55 | + |
| 56 | + it('returns error message prompt when publish directory is set to "out"', () => { |
| 57 | + existsSync.mockReturnValue(false) |
| 58 | + |
| 59 | + const expectedFailureMessage = outdent` |
| 60 | + The directory "out" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. |
| 61 | + Your publish directory is set to "out", but in most cases it should be ".next". |
| 62 | + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". |
| 63 | + ` |
| 64 | + checkNextSiteHasBuilt({ publish: 'out', failBuild: failBuildMock }) |
| 65 | + |
| 66 | + expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) |
| 67 | + }) |
| 68 | + |
| 69 | + it('returns default error message when production build was not found', () => { |
| 70 | + existsSync.mockReturnValue(false) |
| 71 | + const expectedFailureMessage = outdent` |
| 72 | + The directory ".next" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. |
| 73 | + In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config. |
| 74 | + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". |
| 75 | + ` |
| 76 | + checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock }) |
| 77 | + |
| 78 | + expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) |
| 79 | + }) |
| 80 | +}) |
0 commit comments