From 16886123db9027b0dc57e7b1032c7e2e18c2eb0c Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:03:45 +0200 Subject: [PATCH 01/10] chore: type fix --- test/helpers/functions.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers/functions.spec.ts b/test/helpers/functions.spec.ts index 66abefae0a..937cc24bff 100644 --- a/test/helpers/functions.spec.ts +++ b/test/helpers/functions.spec.ts @@ -4,7 +4,7 @@ import { describeCwdTmpDir, moveNextDist } from "../test-utils" describeCwdTmpDir('api route file analysis', () => { it('extracts correct route configs from source files', async () => { await moveNextDist() - const configs = await getExtendedApiRouteConfigs('.next', process.cwd()) + const configs = await getExtendedApiRouteConfigs('.next', process.cwd(), ['js', 'jsx', 'ts', 'tsx']) // Using a Set means the order doesn't matter expect(new Set(configs)).toEqual( new Set([ From e57b07ff36b2f40fa68df912244fdecd572e9a0e Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:21:47 +0200 Subject: [PATCH 02/10] chore: add plugin and first min config --- .eslintignore | 1 - .eslintrc.js | 13 +++++++++++ package-lock.json | 59 ++++++++++++++++++++++++++++++++++++++++++----- package.json | 6 +++-- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/.eslintignore b/.eslintignore index bc70e6ae4a..f4c9bf841a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,6 +1,5 @@ .next node_modules -test lib demos packages/runtime/src/templates/edge diff --git a/.eslintrc.js b/.eslintrc.js index 69ae1605ad..93bfd34cf4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -59,5 +59,18 @@ module.exports = { 'promise/catch-or-return': 0, }, }, + { + files: ['test/**'], + plugins: ['jest'], + extends: ['plugin:jest/recommended'], + rules: { + // Disable global rules + 'max-nested-callbacks': 0, + '@typescript-eslint/no-empty-function': 0, + 'max-lines-per-function': 0, + // esling-plugin-jest specific rules + 'jest/consistent-test-it': ['error', { fn: 'it', withinDescribe: 'it' }], + } + } ], } diff --git a/package-lock.json b/package-lock.json index 0a8432e482..cc3c994995 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,6 +43,7 @@ "cypress": "^12.10.0", "escape-string-regexp": "^2.0.0", "eslint-config-next": "^12.0.0", + "eslint-plugin-jest": "^27.2.1", "eslint-plugin-promise": "^6.0.0", "eslint-plugin-unicorn": "^43.0.2", "execa": "^5.1.1", @@ -54,6 +55,7 @@ "mock-fs": "^5.2.0", "netlify-plugin-cypress": "^2.2.1", "npm-run-all": "^4.1.5", + "pathe": "^1.1.0", "playwright-chromium": "1.28.1", "prettier": "^2.1.2", "react": "^18.2.0", @@ -11626,6 +11628,30 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/eslint-plugin-jest": { + "version": "27.2.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-27.2.1.tgz", + "integrity": "sha512-l067Uxx7ZT8cO9NJuf+eJHvt6bqJyz2Z29wykyEdz/OtmcELQl2MQGQLX8J94O1cSJWAwUSEvCjwjA7KEK3Hmg==", + "dev": true, + "dependencies": { + "@typescript-eslint/utils": "^5.10.0" + }, + "engines": { + "node": "^14.15.0 || ^16.10.0 || >=18.0.0" + }, + "peerDependencies": { + "@typescript-eslint/eslint-plugin": "^5.0.0", + "eslint": "^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "@typescript-eslint/eslint-plugin": { + "optional": true + }, + "jest": { + "optional": true + } + } + }, "node_modules/eslint-plugin-jsx-a11y": { "version": "6.6.1", "resolved": "https://registry.npmjs.org/eslint-plugin-jsx-a11y/-/eslint-plugin-jsx-a11y-6.6.1.tgz", @@ -19843,9 +19869,10 @@ } }, "node_modules/pathe": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", - "integrity": "sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.0.tgz", + "integrity": "sha512-ODbEPR0KKHqECXW1GoxdDb+AZvULmXjVPy4rt+pGo2+TnjJTIPJQSVS6N63n8T2Ip+syHhbn52OewKicV0373w==", + "dev": true }, "node_modules/pause-stream": { "version": "0.0.11", @@ -24370,6 +24397,11 @@ "node": ">=12.0.0" } }, + "packages/runtime/node_modules/pathe": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", + "integrity": "sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==" + }, "packages/runtime/node_modules/semver": { "version": "7.3.8", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", @@ -27700,6 +27732,11 @@ "typescript": "^4.6.3" }, "dependencies": { + "pathe": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", + "integrity": "sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==" + }, "semver": { "version": "7.3.8", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz", @@ -32909,6 +32946,15 @@ } } }, + "eslint-plugin-jest": { + "version": "27.2.1", + "resolved": "https://registry.npmjs.org/eslint-plugin-jest/-/eslint-plugin-jest-27.2.1.tgz", + "integrity": "sha512-l067Uxx7ZT8cO9NJuf+eJHvt6bqJyz2Z29wykyEdz/OtmcELQl2MQGQLX8J94O1cSJWAwUSEvCjwjA7KEK3Hmg==", + "dev": true, + "requires": { + "@typescript-eslint/utils": "^5.10.0" + } + }, "eslint-plugin-jsx-a11y": { "version": "6.6.1", "resolved": "https://registry.npmjs.org/eslint-plugin-jsx-a11y/-/eslint-plugin-jsx-a11y-6.6.1.tgz", @@ -39014,9 +39060,10 @@ "dev": true }, "pathe": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", - "integrity": "sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==" + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.0.tgz", + "integrity": "sha512-ODbEPR0KKHqECXW1GoxdDb+AZvULmXjVPy4rt+pGo2+TnjJTIPJQSVS6N63n8T2Ip+syHhbn52OewKicV0373w==", + "dev": true }, "pause-stream": { "version": "0.0.11", diff --git a/package.json b/package.json index f100c951b7..a5750450f4 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,8 @@ "test:update": "run-s build build:demo test:jest:update" }, "config": { - "eslint": "--cache --format=codeframe --max-warnings=0 \"{packages,src,scripts,tests,.github}/**/*.{ts,js,md,html}\" \"*.{ts,js,md,html}\" \".*.{ts,js,md,html}\"", - "prettier": "--loglevel=warn \"{packages,src,scripts,tests,.github}/**/*.{ts,js,md,yml,json,html}\" \"*.{ts,js,yml,json,html}\" \".*.{ts,js,yml,json,html}\" \"!package-lock.json\"" + "eslint": "--cache --format=codeframe --max-warnings=0 \"{packages,test,.github}/**/*.{ts,js,md,html}\" \"*.{ts,js,md,html}\" \".*.{ts,js,md,html}\"", + "prettier": "--loglevel=warn \"{packages,test,.github}/**/*.{ts,js,md,yml,json,html}\" \"*.{ts,js,yml,json,html}\" \".*.{ts,js,yml,json,html}\" \"!package-lock.json\"" }, "repository": { "type": "git", @@ -67,6 +67,7 @@ "cypress": "^12.10.0", "escape-string-regexp": "^2.0.0", "eslint-config-next": "^12.0.0", + "eslint-plugin-jest": "^27.2.1", "eslint-plugin-promise": "^6.0.0", "eslint-plugin-unicorn": "^43.0.2", "execa": "^5.1.1", @@ -78,6 +79,7 @@ "mock-fs": "^5.2.0", "netlify-plugin-cypress": "^2.2.1", "npm-run-all": "^4.1.5", + "pathe": "^1.1.0", "playwright-chromium": "1.28.1", "prettier": "^2.1.2", "react": "^18.2.0", From 95abfca8867207e27b93cbf8186d50a48bedce73 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:24:27 +0200 Subject: [PATCH 03/10] chore: ignore copied tests from next for now --- .eslintignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index f4c9bf841a..90ae8e5d11 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,4 +6,5 @@ packages/runtime/src/templates/edge packages/runtime/src/templates/edge-shared packages/runtime/lib packages/runtime/dist-types -jestSetup.js \ No newline at end of file +jestSetup.js +test/e2e \ No newline at end of file From d4231e58751dde94b5286bb7a328dee4d846c40a Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:26:44 +0200 Subject: [PATCH 04/10] chore: ignore more stuff --- .eslintignore | 3 ++- .prettierignore | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index 90ae8e5d11..cc151edb48 100644 --- a/.eslintignore +++ b/.eslintignore @@ -7,4 +7,5 @@ packages/runtime/src/templates/edge-shared packages/runtime/lib packages/runtime/dist-types jestSetup.js -test/e2e \ No newline at end of file +test/e2e +test/fixtures/broken_next_config/next.config.js \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 52c69c7596..d65b076d46 100644 --- a/.prettierignore +++ b/.prettierignore @@ -21,6 +21,8 @@ node_modules lib tsconfig.json demos/nx-next-monorepo-demo +test/fixtures/broken_next_config/next.config.js +test/e2e **/CHANGELOG.md packages/runtime/lib From eb54c753320e9fd8d150de200d61cbe37d54a80f Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:28:03 +0200 Subject: [PATCH 05/10] chore: prettier --- .eslintrc.js | 5 +- test/fixtures/analysis/background.js.nft.json | 2 +- test/fixtures/not-static.json | 1 - .../custom/pages/api/custom.api.js | 2 +- .../default/pages/api/default.js | 2 +- .../serverless_next_config/next.config.js | 2 +- test/helpers/analysis.spec.ts | 6 +- test/helpers/config.spec.ts | 14 ++-- test/helpers/edge.spec.ts | 8 +-- test/helpers/files.spec.ts | 20 +++--- test/helpers/functions.spec.ts | 6 +- test/helpers/functionsMetaData.spec.ts | 2 +- test/helpers/verification.spec.ts | 21 ++++-- test/index.spec.ts | 67 ++++++++----------- test/templates/handlerUtils.spec.ts | 13 ++-- test/templates/server.spec.ts | 35 ++++++++-- test/test-utils.ts | 18 ++--- 17 files changed, 117 insertions(+), 107 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 93bfd34cf4..8636f3d9d9 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -68,9 +68,10 @@ module.exports = { 'max-nested-callbacks': 0, '@typescript-eslint/no-empty-function': 0, 'max-lines-per-function': 0, + 'unicorn/no-empty-file': 0, // esling-plugin-jest specific rules 'jest/consistent-test-it': ['error', { fn: 'it', withinDescribe: 'it' }], - } - } + }, + }, ], } diff --git a/test/fixtures/analysis/background.js.nft.json b/test/fixtures/analysis/background.js.nft.json index e212381029..8872301a94 100644 --- a/test/fixtures/analysis/background.js.nft.json +++ b/test/fixtures/analysis/background.js.nft.json @@ -1 +1 @@ -{"version":1,"files":["../../webpack-api-runtime.js","../../../package.json"]} \ No newline at end of file +{ "version": 1, "files": ["../../webpack-api-runtime.js", "../../../package.json"] } diff --git a/test/fixtures/not-static.json b/test/fixtures/not-static.json index 2cb68341e3..cdb2db3ada 100644 --- a/test/fixtures/not-static.json +++ b/test/fixtures/not-static.json @@ -82,5 +82,4 @@ "build:ci:frontend": "npm ci && npm run build && yarn workspace frontend build" } } - ] diff --git a/test/fixtures/page-extensions/custom/pages/api/custom.api.js b/test/fixtures/page-extensions/custom/pages/api/custom.api.js index 625c0891b2..172f1ae6a4 100644 --- a/test/fixtures/page-extensions/custom/pages/api/custom.api.js +++ b/test/fixtures/page-extensions/custom/pages/api/custom.api.js @@ -1 +1 @@ -// noop \ No newline at end of file +// noop diff --git a/test/fixtures/page-extensions/default/pages/api/default.js b/test/fixtures/page-extensions/default/pages/api/default.js index 625c0891b2..172f1ae6a4 100644 --- a/test/fixtures/page-extensions/default/pages/api/default.js +++ b/test/fixtures/page-extensions/default/pages/api/default.js @@ -1 +1 @@ -// noop \ No newline at end of file +// noop diff --git a/test/fixtures/serverless_next_config/next.config.js b/test/fixtures/serverless_next_config/next.config.js index 0fbd0e535c..f0b0ae0241 100644 --- a/test/fixtures/serverless_next_config/next.config.js +++ b/test/fixtures/serverless_next_config/next.config.js @@ -1,3 +1,3 @@ module.exports = { - target: 'serverless' + target: 'serverless', } diff --git a/test/helpers/analysis.spec.ts b/test/helpers/analysis.spec.ts index a066f56508..7beb9df7c5 100644 --- a/test/helpers/analysis.spec.ts +++ b/test/helpers/analysis.spec.ts @@ -40,9 +40,9 @@ describe('static source analysis', () => { }) }) it('should throw if schedule is provided when type is background', async () => { - await expect(extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-schedule.ts'))).rejects.toThrow( - 'Unsupported config value in test/fixtures/analysis/background-schedule.ts', - ) + await expect( + extractConfigFromFile(resolve(__dirname, '../fixtures/analysis/background-schedule.ts')), + ).rejects.toThrow('Unsupported config value in test/fixtures/analysis/background-schedule.ts') expect(console.error).toHaveBeenCalledWith( `Invalid config value in test/fixtures/analysis/background-schedule.ts: schedule is not allowed unless type is "experimental-scheduled"`, ) diff --git a/test/helpers/config.spec.ts b/test/helpers/config.spec.ts index 2373e2dc9c..763e289620 100644 --- a/test/helpers/config.spec.ts +++ b/test/helpers/config.spec.ts @@ -1,10 +1,12 @@ -import { - generateCustomHeaders, - NextConfig -} from "../../packages/runtime/src/helpers/config" +import { generateCustomHeaders, NextConfig } from '../../packages/runtime/src/helpers/config' import type { NetlifyPluginOptions } from '@netlify/build' -const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [], headers: [] } as NetlifyPluginOptions["netlifyConfig"] +const netlifyConfig = { + build: { command: 'npm run build' }, + functions: {}, + redirects: [], + headers: [], +} as NetlifyPluginOptions['netlifyConfig'] describe('generateCustomHeaders', () => { // The routesManifest is the contents of the routes-manifest.json file which will already contain the generated @@ -426,4 +428,4 @@ describe('generateCustomHeaders', () => { }, ]) }) -}) \ No newline at end of file +}) diff --git a/test/helpers/edge.spec.ts b/test/helpers/edge.spec.ts index f4ee91f28e..d0d55648bf 100644 --- a/test/helpers/edge.spec.ts +++ b/test/helpers/edge.spec.ts @@ -41,13 +41,13 @@ describe('generateRscDataEdgeManifest', () => { expect(edgeManifest).toEqual([ { function: 'rsc-data', - generator: "@netlify/next-runtime@1.0.0", + generator: '@netlify/next-runtime@1.0.0', name: 'RSC data routing', path: '/', }, { function: 'rsc-data', - generator: "@netlify/next-runtime@1.0.0", + generator: '@netlify/next-runtime@1.0.0', name: 'RSC data routing', path: '/index.rsc', }, @@ -94,13 +94,13 @@ describe('generateRscDataEdgeManifest', () => { expect(edgeManifest).toEqual([ { function: 'rsc-data', - generator: "@netlify/next-runtime@1.0.0", + generator: '@netlify/next-runtime@1.0.0', name: 'RSC data routing', pattern: '^/blog/([^/]+?)(?:/)?$', }, { function: 'rsc-data', - generator: "@netlify/next-runtime@1.0.0", + generator: '@netlify/next-runtime@1.0.0', name: 'RSC data routing', pattern: '^/blog/([^/]+?)\\.rsc$', }, diff --git a/test/helpers/files.spec.ts b/test/helpers/files.spec.ts index 02e77e3e5b..ecb80ecc67 100644 --- a/test/helpers/files.spec.ts +++ b/test/helpers/files.spec.ts @@ -7,18 +7,14 @@ import { unpatchNextFiles, getDependenciesOfFile, getSourceFileForPage, -} from "../../packages/runtime/src/helpers/files" -import { - readFileSync, - copy, - ensureDir, -} from "fs-extra" -import path from "path" -import { dirname } from "path" +} from '../../packages/runtime/src/helpers/files' +import { readFileSync, copy, ensureDir } from 'fs-extra' +import path from 'path' +import { dirname } from 'path' import { resolve } from 'pathe' -import { join } from "pathe" -import { Rewrites } from "../../packages/runtime/src/helpers/types" -import { describeCwdTmpDir, moveNextDist } from "../test-utils" +import { join } from 'pathe' +import { Rewrites } from '../../packages/runtime/src/helpers/types' +import { describeCwdTmpDir, moveNextDist } from '../test-utils' const TEST_DIR = resolve(__dirname, '..') @@ -241,4 +237,4 @@ describe('getSourceFileForPage', () => { expect(filePath.replace(TEST_DIR, '')).toBe('/fixtures/page-extensions/custom/pages/api/custom.api.js') }) -}) \ No newline at end of file +}) diff --git a/test/helpers/functions.spec.ts b/test/helpers/functions.spec.ts index 937cc24bff..965339b8b1 100644 --- a/test/helpers/functions.spec.ts +++ b/test/helpers/functions.spec.ts @@ -1,5 +1,5 @@ -import { getExtendedApiRouteConfigs } from "../../packages/runtime/src/helpers/functions" -import { describeCwdTmpDir, moveNextDist } from "../test-utils" +import { getExtendedApiRouteConfigs } from '../../packages/runtime/src/helpers/functions' +import { describeCwdTmpDir, moveNextDist } from '../test-utils' describeCwdTmpDir('api route file analysis', () => { it('extracts correct route configs from source files', async () => { @@ -21,4 +21,4 @@ describeCwdTmpDir('api route file analysis', () => { ]), ) }) -}) \ No newline at end of file +}) diff --git a/test/helpers/functionsMetaData.spec.ts b/test/helpers/functionsMetaData.spec.ts index 81f51efc69..e445b39a5b 100644 --- a/test/helpers/functionsMetaData.spec.ts +++ b/test/helpers/functionsMetaData.spec.ts @@ -102,4 +102,4 @@ describe('writeFunctionConfiguration', () => { expect(actual).toEqual(expected) }) -}) \ No newline at end of file +}) diff --git a/test/helpers/verification.spec.ts b/test/helpers/verification.spec.ts index 83e028c660..d2cd34d858 100644 --- a/test/helpers/verification.spec.ts +++ b/test/helpers/verification.spec.ts @@ -1,10 +1,19 @@ import Chance from 'chance' -import { checkNextSiteHasBuilt, checkZipSize, getProblematicUserRewrites } from '../../packages/runtime/src/helpers/verification' +import { + checkNextSiteHasBuilt, + checkZipSize, + getProblematicUserRewrites, +} from '../../packages/runtime/src/helpers/verification' import { outdent } from 'outdent' import type { NetlifyPluginOptions } from '@netlify/build' -import { describeCwdTmpDir, moveNextDist } from "../test-utils" +import { describeCwdTmpDir, moveNextDist } from '../test-utils' -const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [], headers: [] } as NetlifyPluginOptions["netlifyConfig"] +const netlifyConfig = { + build: { command: 'npm run build' }, + functions: {}, + redirects: [], + headers: [], +} as NetlifyPluginOptions['netlifyConfig'] import type { NetlifyPluginUtils } from '@netlify/build' type FailBuild = NetlifyPluginUtils['build']['failBuild'] @@ -101,11 +110,13 @@ describe('checkZipSize', () => { it('emits a warning that DISABLE_BUNDLE_ZIP_SIZE_CHECK was enabled', async () => { process.env.DISABLE_BUNDLE_ZIP_SIZE_CHECK = 'true' await checkZipSize(chance.string()) - expect(consoleSpy).toHaveBeenCalledWith('Function bundle size check was DISABLED with the DISABLE_BUNDLE_ZIP_SIZE_CHECK environment variable. Your deployment will break if it exceeds the maximum supported size of function zip files in your account.') + expect(consoleSpy).toHaveBeenCalledWith( + 'Function bundle size check was DISABLED with the DISABLE_BUNDLE_ZIP_SIZE_CHECK environment variable. Your deployment will break if it exceeds the maximum supported size of function zip files in your account.', + ) }) }) -describeCwdTmpDir("getProblematicUserRewrites", () => { +describeCwdTmpDir('getProblematicUserRewrites', () => { it('finds problematic user rewrites', async () => { await moveNextDist() const rewrites = getProblematicUserRewrites({ diff --git a/test/index.spec.ts b/test/index.spec.ts index 376de21c69..60a0b72817 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -16,42 +16,29 @@ jest.mock('../packages/runtime/src/helpers/functionsMetaData', () => { } }) -import Chance from "chance" -import { - writeJSON, - unlink, - existsSync, - readFileSync, - ensureDir, - readJson, - pathExists, - writeFile, - move, -} from "fs-extra" -import path from "path" -import process from "process" -import os from "os" -import { dir as getTmpDir } from "tmp-promise" +import Chance from 'chance' +import { writeJSON, unlink, existsSync, readFileSync, ensureDir, readJson, pathExists, writeFile, move } from 'fs-extra' +import path from 'path' +import process from 'process' +import os from 'os' +import { dir as getTmpDir } from 'tmp-promise' // @ts-expect-error - TODO: Convert runtime export to ES6 -import nextRuntimeFactory from "../packages/runtime/src" +import nextRuntimeFactory from '../packages/runtime/src' const nextRuntime = nextRuntimeFactory({}) -import { watchForMiddlewareChanges } from "../packages/runtime/src/helpers/compiler" -import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } from "../packages/runtime/src/constants" -import { join } from "pathe" -import { - getRequiredServerFiles, - updateRequiredServerFiles, -} from "../packages/runtime/src/helpers/config" -import { resolve } from "path" +import { watchForMiddlewareChanges } from '../packages/runtime/src/helpers/compiler' +import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } from '../packages/runtime/src/constants' +import { join } from 'pathe' +import { getRequiredServerFiles, updateRequiredServerFiles } from '../packages/runtime/src/helpers/config' +import { resolve } from 'path' import type { NetlifyPluginOptions } from '@netlify/build' -import { changeCwd, useFixture, moveNextDist } from "./test-utils" +import { changeCwd, useFixture, moveNextDist } from './test-utils' const chance = new Chance() const constants = { INTERNAL_FUNCTIONS_SRC: '.netlify/functions-internal', PUBLISH_DIR: '.next', FUNCTIONS_DIST: '.netlify/functions', -} as unknown as NetlifyPluginOptions["constants"] +} as unknown as NetlifyPluginOptions['constants'] const utils = { build: { failBuild(message) { @@ -63,14 +50,19 @@ const utils = { save: jest.fn(), restore: jest.fn(), }, -} as unknown as NetlifyPluginOptions["utils"] +} as unknown as NetlifyPluginOptions['utils'] const normalizeChunkNames = (source) => source.replaceAll(/\/chunks\/\d+\.js/g, '/chunks/CHUNK_ID.js') const onBuildHasRun = (netlifyConfig) => Boolean(netlifyConfig.functions[HANDLER_FUNCTION_NAME]?.included_files?.some((file) => file.includes('BUILD_ID'))) -const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, redirects: [], headers: [] } as NetlifyPluginOptions["netlifyConfig"] +const netlifyConfig = { + build: { command: 'npm run build' }, + functions: {}, + redirects: [], + headers: [], +} as NetlifyPluginOptions['netlifyConfig'] const defaultArgs = { netlifyConfig, utils, @@ -637,30 +629,29 @@ describe('onBuild()', () => { await nextRuntime.onBuild(defaultArgs) const manifestPath = await readJson(path.resolve('.netlify/edge-functions/manifest.json')) const manifest = manifestPath.functions - + expect(manifest).toEqual( expect.arrayContaining([ expect.objectContaining({ - generator: '@netlify/next-runtime@1.0.0' - }) - ]) + generator: '@netlify/next-runtime@1.0.0', + }), + ]), ) }) - test('generates generator field within the edge-functions manifest includes IPX', async () => { process.env.NEXT_FORCE_EDGE_IMAGES = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) const manifestPath = await readJson(path.resolve('.netlify/edge-functions/manifest.json')) const manifest = manifestPath.functions - + expect(manifest).toEqual( expect.arrayContaining([ expect.objectContaining({ - generator: '@netlify/next-runtime@1.0.0' - }) - ]) + generator: '@netlify/next-runtime@1.0.0', + }), + ]), ) }) diff --git a/test/templates/handlerUtils.spec.ts b/test/templates/handlerUtils.spec.ts index 795c48dd36..d5cc361943 100644 --- a/test/templates/handlerUtils.spec.ts +++ b/test/templates/handlerUtils.spec.ts @@ -5,15 +5,10 @@ import { localizeDataRoute, downloadFile, } from '../../packages/runtime/src/templates/handlerUtils' -import { join } from "pathe" -import os from "os" -import path from "path" -import { - unlink, - existsSync, - readFileSync, - ensureDir, -} from "fs-extra" +import { join } from 'pathe' +import os from 'os' +import path from 'path' +import { unlink, existsSync, readFileSync, ensureDir } from 'fs-extra' describe('normalizeRoute', () => { it('removes a trailing slash from a route', () => { diff --git a/test/templates/server.spec.ts b/test/templates/server.spec.ts index a8870761fd..0ee660df8e 100644 --- a/test/templates/server.spec.ts +++ b/test/templates/server.spec.ts @@ -82,7 +82,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: {} }, { ...mockTokenConfig }) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/non-i18n/with-revalidate/', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/non-i18n/with-revalidate/', + headers: { 'x-prerender-revalidate': 'test' }, + }) // @ts-expect-error - Types are incorrect for `MockedResponse` await requestHandler(mockReq, mockRes) @@ -99,7 +102,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: { ...mocki18nConfig } }, { ...mockTokenConfig }) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/i18n/with-revalidate/', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/i18n/with-revalidate/', + headers: { 'x-prerender-revalidate': 'test' }, + }) // @ts-expect-error - Types are incorrect for `MockedResponse` await requestHandler(mockReq, mockRes) @@ -116,7 +122,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: {} }, { ...mockTokenConfig }) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/blog/rob/hello', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/blog/rob/hello', + headers: { 'x-prerender-revalidate': 'test' }, + }) // @ts-expect-error - Types are incorrect for `MockedResponse` await requestHandler(mockReq, mockRes) @@ -133,7 +142,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: { ...mocki18nConfig } }, { ...mockTokenConfig }) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/fr/posts/hello', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/fr/posts/hello', + headers: { 'x-prerender-revalidate': 'test' }, + }) // @ts-expect-error - Types are incorrect for `MockedResponse` await requestHandler(mockReq, mockRes) @@ -150,7 +162,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/not-a-valid-path/', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/not-a-valid-path/', + headers: { 'x-prerender-revalidate': 'test' }, + }) // @ts-expect-error - Types are incorrect for `MockedResponse` await expect(requestHandler(mockReq, mockRes)).rejects.toThrow('not an ISR route') @@ -160,7 +175,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/posts/hello/', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/posts/hello/', + headers: { 'x-prerender-revalidate': 'test' }, + }) mockedApiFetch.mockResolvedValueOnce({ code: 500, message: 'Failed to revalidate' }) // @ts-expect-error - Types are incorrect for `MockedResponse` @@ -171,7 +189,10 @@ describe('the netlify next server', () => { const netlifyNextServer = new NetlifyNextServer({ conf: {} }, mockTokenConfig) const requestHandler = netlifyNextServer.getRequestHandler() - const { req: mockReq, res: mockRes } = createRequestResponseMocks({ url: '/posts/hello', headers: { 'x-prerender-revalidate': 'test' }}) + const { req: mockReq, res: mockRes } = createRequestResponseMocks({ + url: '/posts/hello', + headers: { 'x-prerender-revalidate': 'test' }, + }) mockedApiFetch.mockRejectedValueOnce(new Error('Unable to connect')) // @ts-expect-error - Types are incorrect for `MockedResponse` diff --git a/test/test-utils.ts b/test/test-utils.ts index 0d5ac9f23b..3010514d10 100644 --- a/test/test-utils.ts +++ b/test/test-utils.ts @@ -1,14 +1,8 @@ -import path from "path" -import { dirname } from "path" -import cpy from "cpy" -import { - writeJSON, - existsSync, - ensureDir, - readJson, - copy, -} from "fs-extra" -import { dir as getTmpDir } from "tmp-promise" +import path from 'path' +import { dirname } from 'path' +import cpy from 'cpy' +import { writeJSON, existsSync, ensureDir, readJson, copy } from 'fs-extra' +import { dir as getTmpDir } from 'tmp-promise' const FIXTURES_DIR = `${__dirname}/fixtures` const SAMPLE_PROJECT_DIR = `${__dirname}/../demos/default` @@ -91,4 +85,4 @@ export const describeCwdTmpDir = (name: string, fn: () => void): void => { fn() }) -} \ No newline at end of file +} From bcce91fad918a7ec5013d98445b896270ab41a44 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:39:16 +0200 Subject: [PATCH 06/10] chore: more fixes --- .eslintrc.js | 9 +- package-lock.json | 90 ++++++++++----- package.json | 1 + packages/next/package.json | 1 + packages/next/test/request.spec.ts | 7 +- test/helpers/analysis.spec.ts | 3 +- test/helpers/config.spec.ts | 3 +- test/helpers/edge.spec.ts | 3 +- test/helpers/files.spec.ts | 28 ++--- test/helpers/functionsMetaData.spec.ts | 1 + test/helpers/matchers.spec.ts | 3 +- test/helpers/utils.spec.ts | 1 + test/helpers/verification.spec.ts | 43 ++++--- test/index.spec.ts | 151 ++++++++++++------------- test/templates/handlerUtils.spec.ts | 12 +- test/test-utils.ts | 11 +- 16 files changed, 202 insertions(+), 165 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8636f3d9d9..0a6b965381 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -60,17 +60,22 @@ module.exports = { }, }, { - files: ['test/**'], + files: ['test/**', 'packages/**/test/**'], plugins: ['jest'], extends: ['plugin:jest/recommended'], rules: { // Disable global rules - 'max-nested-callbacks': 0, + 'max-nested-callbacks': 'off', '@typescript-eslint/no-empty-function': 0, 'max-lines-per-function': 0, 'unicorn/no-empty-file': 0, + 'prefer-destructuring': 0, + '@typescript-eslint/no-unused-vars': 0, + 'unicorn/no-await-expression-member': 0, // esling-plugin-jest specific rules 'jest/consistent-test-it': ['error', { fn: 'it', withinDescribe: 'it' }], + 'jest/no-disabled-tests': 0, + 'jest/no-conditional-expect': 0, }, }, ], diff --git a/package-lock.json b/package-lock.json index cc3c994995..d1634fd947 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,6 +47,7 @@ "eslint-plugin-promise": "^6.0.0", "eslint-plugin-unicorn": "^43.0.2", "execa": "^5.1.1", + "fs-extra": "^11.1.1", "husky": "^7.0.4", "jest": "^27.0.0", "jest-extended": "^3.2.0", @@ -2365,6 +2366,20 @@ "node": ">=v14" } }, + "node_modules/@commitlint/read/node_modules/fs-extra": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/@commitlint/resolve-extends": { "version": "17.1.0", "resolved": "https://registry.npmjs.org/@commitlint/resolve-extends/-/resolve-extends-17.1.0.tgz", @@ -4856,19 +4871,6 @@ "unstorage": "^1.0.0" } }, - "node_modules/@netlify/ipx/node_modules/fs-extra": { - "version": "11.1.1", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.1.1.tgz", - "integrity": "sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==", - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=14.14" - } - }, "node_modules/@netlify/ipx/node_modules/ufo": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.1.1.tgz", @@ -13136,16 +13138,16 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" }, "node_modules/fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.1.1.tgz", + "integrity": "sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==", "dependencies": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", "universalify": "^2.0.0" }, "engines": { - "node": ">=12" + "node": ">=14.14" } }, "node_modules/fs-memo": { @@ -24342,6 +24344,7 @@ "devDependencies": { "@netlify/edge-functions": "^2.0.0", "@types/node": "^17.0.25", + "chance": "^1.1.11", "next": "^13.3.0", "npm-run-all": "^4.1.5", "typescript": "^4.6.3" @@ -24397,6 +24400,19 @@ "node": ">=12.0.0" } }, + "packages/runtime/node_modules/fs-extra": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, "packages/runtime/node_modules/pathe": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", @@ -25929,6 +25945,19 @@ "fs-extra": "^10.0.0", "git-raw-commits": "^2.0.0", "minimist": "^1.2.6" + }, + "dependencies": { + "fs-extra": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "dev": true, + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + } } }, "@commitlint/resolve-extends": { @@ -27662,16 +27691,6 @@ "unstorage": "^1.0.0" }, "dependencies": { - "fs-extra": { - "version": "11.1.1", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.1.1.tgz", - "integrity": "sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==", - "requires": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - } - }, "ufo": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ufo/-/ufo-1.1.1.tgz", @@ -27684,6 +27703,7 @@ "requires": { "@netlify/edge-functions": "^2.0.0", "@types/node": "^17.0.25", + "chance": "*", "next": "^13.3.0", "npm-run-all": "^4.1.5", "typescript": "^4.6.3" @@ -27732,6 +27752,16 @@ "typescript": "^4.6.3" }, "dependencies": { + "fs-extra": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, "pathe": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/pathe/-/pathe-0.2.0.tgz", @@ -33945,9 +33975,9 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==" }, "fs-extra": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", - "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "version": "11.1.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.1.1.tgz", + "integrity": "sha512-MGIE4HOvQCeUCzmlHs0vXpih4ysz4wg9qiSAu6cd42lVwPbTM1TjV7RusoyQqMmk/95gdQZX72u+YW+c3eEpFQ==", "requires": { "graceful-fs": "^4.2.0", "jsonfile": "^6.0.1", diff --git a/package.json b/package.json index a5750450f4..e55528a57b 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "eslint-plugin-promise": "^6.0.0", "eslint-plugin-unicorn": "^43.0.2", "execa": "^5.1.1", + "fs-extra": "^11.1.1", "husky": "^7.0.4", "jest": "^27.0.0", "jest-extended": "^3.2.0", diff --git a/packages/next/package.json b/packages/next/package.json index cb1f5d164c..7c5c250844 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -9,6 +9,7 @@ "devDependencies": { "@netlify/edge-functions": "^2.0.0", "@types/node": "^17.0.25", + "chance": "^1.1.11", "next": "^13.3.0", "npm-run-all": "^4.1.5", "typescript": "^4.6.3" diff --git a/packages/next/test/request.spec.ts b/packages/next/test/request.spec.ts index a421de9544..f772af6f4c 100644 --- a/packages/next/test/request.spec.ts +++ b/packages/next/test/request.spec.ts @@ -2,6 +2,7 @@ import Chance from 'chance' import { NextURL } from 'next/dist/server/web/next-url' import { RequestCookies } from 'next/dist/server/web/spec-extension/cookies' import { NextRequest } from 'next/server' + import { MiddlewareRequest } from '../src/middleware/request' const chance = new Chance() @@ -75,19 +76,19 @@ describe('MiddlewareRequest', () => { it('throws an error when MiddlewareRequest is run outside of edge environment', () => { delete globalThis.Deno - expect(() => new MiddlewareRequest(nextRequest)).toThrowError( + expect(() => new MiddlewareRequest(nextRequest)).toThrow( 'MiddlewareRequest only works in a Netlify Edge Function environment', ) }) it('throws an error when x-nf-request-id header is missing', () => { nextRequest.headers.delete('x-nf-request-id') - expect(() => new MiddlewareRequest(nextRequest)).toThrowError('Missing x-nf-request-id header') + expect(() => new MiddlewareRequest(nextRequest)).toThrow('Missing x-nf-request-id header') }) it('throws an error when request context is missing', () => { globalThis.NFRequestContextMap.delete(requestId) - expect(() => new MiddlewareRequest(nextRequest)).toThrowError( + expect(() => new MiddlewareRequest(nextRequest)).toThrow( `Could not find request context for request id ${requestId}`, ) }) diff --git a/test/helpers/analysis.spec.ts b/test/helpers/analysis.spec.ts index 7beb9df7c5..0aa0748137 100644 --- a/test/helpers/analysis.spec.ts +++ b/test/helpers/analysis.spec.ts @@ -1,6 +1,7 @@ -import { extractConfigFromFile } from '../../packages/runtime/src/helpers/analysis' import { resolve } from 'pathe' +import { extractConfigFromFile } from '../../packages/runtime/src/helpers/analysis' + describe('static source analysis', () => { beforeEach(() => { // Spy on console.error diff --git a/test/helpers/config.spec.ts b/test/helpers/config.spec.ts index 763e289620..fbdc2479b5 100644 --- a/test/helpers/config.spec.ts +++ b/test/helpers/config.spec.ts @@ -1,6 +1,7 @@ -import { generateCustomHeaders, NextConfig } from '../../packages/runtime/src/helpers/config' import type { NetlifyPluginOptions } from '@netlify/build' +import { generateCustomHeaders, NextConfig } from '../../packages/runtime/src/helpers/config' + const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, diff --git a/test/helpers/edge.spec.ts b/test/helpers/edge.spec.ts index d0d55648bf..074071152b 100644 --- a/test/helpers/edge.spec.ts +++ b/test/helpers/edge.spec.ts @@ -1,6 +1,7 @@ -import { generateRscDataEdgeManifest } from '../../packages/runtime/src/helpers/edge' import type { PrerenderManifest } from 'next/dist/build' +import { generateRscDataEdgeManifest } from '../../packages/runtime/src/helpers/edge' + jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => { const { NEXT_PLUGIN_NAME } = require('../../packages/runtime/src/constants') return { diff --git a/test/helpers/files.spec.ts b/test/helpers/files.spec.ts index ecb80ecc67..4c8967969d 100644 --- a/test/helpers/files.spec.ts +++ b/test/helpers/files.spec.ts @@ -1,3 +1,8 @@ +import path, { dirname } from 'path' + +import { readFileSync, copy, ensureDir } from 'fs-extra' +import { resolve , join } from 'pathe' + import { matchMiddleware, stripLocale, @@ -8,11 +13,6 @@ import { getDependenciesOfFile, getSourceFileForPage, } from '../../packages/runtime/src/helpers/files' -import { readFileSync, copy, ensureDir } from 'fs-extra' -import path from 'path' -import { dirname } from 'path' -import { resolve } from 'pathe' -import { join } from 'pathe' import { Rewrites } from '../../packages/runtime/src/helpers/types' import { describeCwdTmpDir, moveNextDist } from '../test-utils' @@ -59,7 +59,7 @@ const REWRITES: Rewrites = [ ] describe('files utility functions', () => { - test('middleware tester matches correct paths', () => { + it('middleware tester matches correct paths', () => { const middleware = ['middle', 'sub/directory'] const paths = [ 'middle.html', @@ -76,7 +76,7 @@ describe('files utility functions', () => { } }) - test('middleware tester does not match incorrect paths', () => { + it('middleware tester does not match incorrect paths', () => { const middleware = ['middle', 'sub/directory'] const paths = [ 'middl', @@ -93,7 +93,7 @@ describe('files utility functions', () => { } }) - test('middleware tester matches root middleware', () => { + it('middleware tester matches root middleware', () => { const middleware = [''] const paths = [ 'middl', @@ -110,7 +110,7 @@ describe('files utility functions', () => { } }) - test('middleware tester matches root middleware', () => { + it('middleware tester matches root middleware', () => { const paths = [ 'middl', '', @@ -126,7 +126,7 @@ describe('files utility functions', () => { } }) - test('stripLocale correctly strips matching locales', () => { + it('stripLocale correctly strips matching locales', () => { const locales = ['en', 'fr', 'en-GB'] const paths = [ ['en/file.html', 'file.html'], @@ -140,7 +140,7 @@ describe('files utility functions', () => { } }) - test('stripLocale does not touch non-matching matching locales', () => { + it('stripLocale does not touch non-matching matching locales', () => { const locales = ['en', 'fr', 'en-GB'] const paths = ['de/file.html', 'enfile.html', 'en-US/file.html'] for (const path of paths) { @@ -148,21 +148,21 @@ describe('files utility functions', () => { } }) - test('matchesRedirect correctly matches paths with locales', () => { + it('matchesRedirect correctly matches paths with locales', () => { const paths = ['en/redirectme.html', 'en/redirectme.json', 'fr/redirectme.html', 'fr/redirectme.json'] paths.forEach((path) => { expect(matchesRedirect(path, REDIRECTS)).toBeTruthy() }) }) - test("matchesRedirect doesn't match paths with invalid locales", () => { + it("matchesRedirect doesn't match paths with invalid locales", () => { const paths = ['dk/redirectme.html', 'dk/redirectme.json', 'gr/redirectme.html', 'gr/redirectme.json'] paths.forEach((path) => { expect(matchesRedirect(path, REDIRECTS)).toBeFalsy() }) }) - test("matchesRedirect doesn't match internal redirects", () => { + it("matchesRedirect doesn't match internal redirects", () => { const paths = ['en/notrailingslash'] paths.forEach((path) => { expect(matchesRedirect(path, REDIRECTS)).toBeFalsy() diff --git a/test/helpers/functionsMetaData.spec.ts b/test/helpers/functionsMetaData.spec.ts index e445b39a5b..1835863d5f 100644 --- a/test/helpers/functionsMetaData.spec.ts +++ b/test/helpers/functionsMetaData.spec.ts @@ -1,6 +1,7 @@ import { readJSON } from 'fs-extra' import mock from 'mock-fs' import { join } from 'pathe' + import { NEXT_PLUGIN_NAME } from '../../packages/runtime/src/constants' import { writeFunctionConfiguration } from '../../packages/runtime/src/helpers/functionsMetaData' diff --git a/test/helpers/matchers.spec.ts b/test/helpers/matchers.spec.ts index 10881ac281..c8d5196316 100644 --- a/test/helpers/matchers.spec.ts +++ b/test/helpers/matchers.spec.ts @@ -1,5 +1,6 @@ -import { makeLocaleOptional, stripLookahead } from '../../packages/runtime/src/helpers/matchers' import { getEdgeFunctionPatternForPage } from '../../packages/runtime/src/helpers/edge' +import { makeLocaleOptional, stripLookahead } from '../../packages/runtime/src/helpers/matchers' + const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json` function checkPath(path: string, regex: string) { diff --git a/test/helpers/utils.spec.ts b/test/helpers/utils.spec.ts index e02bd3fb42..772072bceb 100644 --- a/test/helpers/utils.spec.ts +++ b/test/helpers/utils.spec.ts @@ -1,5 +1,6 @@ import Chance from 'chance' import { ExperimentalConfig } from 'next/dist/server/config-shared' + import { getCustomImageResponseHeaders, getRemotePatterns, diff --git a/test/helpers/verification.spec.ts b/test/helpers/verification.spec.ts index d2cd34d858..4198b1af69 100644 --- a/test/helpers/verification.spec.ts +++ b/test/helpers/verification.spec.ts @@ -1,13 +1,15 @@ +import type { NetlifyPluginOptions , NetlifyPluginUtils } from '@netlify/build' import Chance from 'chance' +import { outdent } from 'outdent' + import { checkNextSiteHasBuilt, checkZipSize, getProblematicUserRewrites, } from '../../packages/runtime/src/helpers/verification' -import { outdent } from 'outdent' -import type { NetlifyPluginOptions } from '@netlify/build' import { describeCwdTmpDir, moveNextDist } from '../test-utils' + const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, @@ -15,17 +17,14 @@ const netlifyConfig = { headers: [], } as NetlifyPluginOptions['netlifyConfig'] -import type { NetlifyPluginUtils } from '@netlify/build' type FailBuild = NetlifyPluginUtils['build']['failBuild'] const chance = new Chance() -jest.mock('fs', () => { - return { +jest.mock('fs', () => ({ ...jest.requireActual('fs'), existsSync: jest.fn(), - } -}) + })) describe('checkNextSiteHasBuilt', () => { let failBuildMock @@ -44,8 +43,8 @@ describe('checkNextSiteHasBuilt', () => { existsSync.mockReturnValue(true) const expectedFailureMessage = outdent` - Detected that "next export" was run, but site is incorrectly publishing the ".next" directory. - The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". + Detected that "next export" was run, but site is incorrectly publishing the ".next" directory. + The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". ` checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock }) @@ -59,11 +58,11 @@ describe('checkNextSiteHasBuilt', () => { existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true) const expectedFailureMessage = outdent` - 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. - However, a '.next' directory was found with a production build. - Consider changing your 'publish' directory to '.next' - If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". - ` + 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. + However, a '.next' directory was found with a production build. + Consider changing your 'publish' directory to '.next' + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". + ` checkNextSiteHasBuilt({ publish: 'someCustomDir', failBuild: failBuildMock }) @@ -74,10 +73,10 @@ describe('checkNextSiteHasBuilt', () => { existsSync.mockReturnValue(false) const expectedFailureMessage = outdent` - 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. - Your publish directory is set to "out", but in most cases it should be ".next". - If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". - ` + 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. + Your publish directory is set to "out", but in most cases it should be ".next". + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". + ` checkNextSiteHasBuilt({ publish: 'out', failBuild: failBuildMock }) expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) @@ -86,10 +85,10 @@ describe('checkNextSiteHasBuilt', () => { it('returns default error message when production build was not found', () => { existsSync.mockReturnValue(false) const expectedFailureMessage = outdent` - 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. - In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config. - If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". - ` + 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. + In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config. + If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true". + ` checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock }) expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage) diff --git a/test/index.spec.ts b/test/index.spec.ts index 60a0b72817..fa8040ca38 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,12 +1,27 @@ -import { relative } from 'pathe' + +import os from 'os' +import path, { resolve } from 'path' +import process from 'process' + +import type { NetlifyPluginOptions } from '@netlify/build' +import Chance from 'chance' +import { writeJSON, unlink, existsSync, readFileSync, ensureDir, readJson, pathExists, writeFile, move } from 'fs-extra' +import { join , relative } from 'pathe' +import { dir as getTmpDir } from 'tmp-promise' + +// @ts-expect-error - TODO: Convert runtime export to ES6 +import nextRuntimeFactory from '../packages/runtime/src' +import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } from '../packages/runtime/src/constants' +import { watchForMiddlewareChanges } from '../packages/runtime/src/helpers/compiler' +import { getRequiredServerFiles, updateRequiredServerFiles } from '../packages/runtime/src/helpers/config' import { getAllPageDependencies } from '../packages/runtime/src/templates/getPageResolver' -jest.mock('../packages/runtime/src/helpers/utils', () => { - return { +import { changeCwd, useFixture, moveNextDist } from './test-utils' + +jest.mock('../packages/runtime/src/helpers/utils', () => ({ ...jest.requireActual('../packages/runtime/src/helpers/utils'), isNextAuthInstalled: jest.fn(), - } -}) + })) jest.mock('../packages/runtime/src/helpers/functionsMetaData', () => { const { NEXT_PLUGIN_NAME } = require('../packages/runtime/src/constants') @@ -15,23 +30,7 @@ jest.mock('../packages/runtime/src/helpers/functionsMetaData', () => { getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, } }) - -import Chance from 'chance' -import { writeJSON, unlink, existsSync, readFileSync, ensureDir, readJson, pathExists, writeFile, move } from 'fs-extra' -import path from 'path' -import process from 'process' -import os from 'os' -import { dir as getTmpDir } from 'tmp-promise' -// @ts-expect-error - TODO: Convert runtime export to ES6 -import nextRuntimeFactory from '../packages/runtime/src' const nextRuntime = nextRuntimeFactory({}) -import { watchForMiddlewareChanges } from '../packages/runtime/src/helpers/compiler' -import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } from '../packages/runtime/src/constants' -import { join } from 'pathe' -import { getRequiredServerFiles, updateRequiredServerFiles } from '../packages/runtime/src/helpers/config' -import { resolve } from 'path' -import type { NetlifyPluginOptions } from '@netlify/build' -import { changeCwd, useFixture, moveNextDist } from './test-utils' const chance = new Chance() const constants = { @@ -102,14 +101,14 @@ afterEach(async () => { }) describe('preBuild()', () => { - test('fails if publishing the root of the project', () => { + it('fails if publishing the root of the project', () => { defaultArgs.netlifyConfig.build.publish = path.resolve('.') - expect(nextRuntime.onPreBuild(defaultArgs)).rejects.toThrowError( + expect(nextRuntime.onPreBuild(defaultArgs)).rejects.toThrow( /Your publish directory is pointing to the base directory of your site/, ) }) - test('fails if the build version is too old', () => { + it('fails if the build version is too old', () => { expect( nextRuntime.onPreBuild({ ...defaultArgs, @@ -118,7 +117,7 @@ describe('preBuild()', () => { ).rejects.toThrow('This version of the Next Runtime requires netlify-cli') }) - test('passes if the build version is new enough', async () => { + it('passes if the build version is new enough', async () => { expect( nextRuntime.onPreBuild({ ...defaultArgs, @@ -152,9 +151,7 @@ describe('onBuild()', () => { const { isNextAuthInstalled } = require('../packages/runtime/src/helpers/utils') beforeEach(() => { - isNextAuthInstalled.mockImplementation(() => { - return true - }) + isNextAuthInstalled.mockImplementation(() => true) }) afterEach(() => { @@ -163,7 +160,7 @@ describe('onBuild()', () => { delete process.env.CONTEXT }) - test('does not set NEXTAUTH_URL if value is already set', async () => { + it('does not set NEXTAUTH_URL if value is already set', async () => { const mockUserDefinedSiteUrl = chance.url() process.env.DEPLOY_PRIME_URL = chance.url() @@ -182,7 +179,7 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl) }) - test("sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not 'production'", async () => { + it("sets the NEXTAUTH_URL to the DEPLOY_PRIME_URL when CONTEXT env variable is not 'production'", async () => { const mockUserDefinedSiteUrl = chance.url() process.env.DEPLOY_PRIME_URL = mockUserDefinedSiteUrl process.env.URL = chance.url() @@ -205,7 +202,7 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl) }) - test("sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is 'production'", async () => { + it("sets the NEXTAUTH_URL to the user defined site URL when CONTEXT env variable is 'production'", async () => { const mockUserDefinedSiteUrl = chance.url() process.env.DEPLOY_PRIME_URL = chance.url() process.env.URL = mockUserDefinedSiteUrl @@ -228,7 +225,7 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toEqual(mockUserDefinedSiteUrl) }) - test('sets the NEXTAUTH_URL specified in the netlify.toml or in the Netlify UI', async () => { + it('sets the NEXTAUTH_URL specified in the netlify.toml or in the Netlify UI', async () => { const mockSiteUrl = chance.url() process.env.NEXTAUTH_URL = mockSiteUrl @@ -243,7 +240,7 @@ describe('onBuild()', () => { delete process.env.NEXTAUTH_URL }) - test('sets NEXTAUTH_URL when next-auth package is detected', async () => { + it('sets NEXTAUTH_URL when next-auth package is detected', async () => { const mockSiteUrl = chance.url() // Value represents the main address to the site and is either @@ -261,7 +258,7 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toEqual(mockSiteUrl) }) - test('includes the basePath on NEXTAUTH_URL when present', async () => { + it('includes the basePath on NEXTAUTH_URL when present', async () => { const mockSiteUrl = chance.url() process.env.DEPLOY_PRIME_URL = mockSiteUrl @@ -279,10 +276,8 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toEqual(`${mockSiteUrl}/foo`) }) - test('skips setting NEXTAUTH_URL when next-auth package is not found', async () => { - isNextAuthInstalled.mockImplementation(() => { - return false - }) + it('skips setting NEXTAUTH_URL when next-auth package is not found', async () => { + isNextAuthInstalled.mockImplementation(() => false) await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -293,7 +288,7 @@ describe('onBuild()', () => { expect(config.config.env.NEXTAUTH_URL).toBeUndefined() }) - test('runs onBuild', async () => { + it('runs onBuild', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -301,7 +296,7 @@ describe('onBuild()', () => { expect(onBuildHasRun(netlifyConfig)).toBe(true) }) - test('skips if NETLIFY_NEXT_PLUGIN_SKIP is set', async () => { + it('skips if NETLIFY_NEXT_PLUGIN_SKIP is set', async () => { process.env.NETLIFY_NEXT_PLUGIN_SKIP = 'true' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -310,7 +305,7 @@ describe('onBuild()', () => { delete process.env.NETLIFY_NEXT_PLUGIN_SKIP }) - test('skips if NEXT_PLUGIN_FORCE_RUN is "false"', async () => { + it('skips if NEXT_PLUGIN_FORCE_RUN is "false"', async () => { process.env.NEXT_PLUGIN_FORCE_RUN = 'false' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -319,7 +314,7 @@ describe('onBuild()', () => { delete process.env.NEXT_PLUGIN_FORCE_RUN }) - test("fails if BUILD_ID doesn't exist", async () => { + it("fails if BUILD_ID doesn't exist", async () => { await moveNextDist() await unlink(path.join(process.cwd(), '.next/BUILD_ID')) const failBuild = jest.fn().mockImplementation((err) => { @@ -331,7 +326,7 @@ describe('onBuild()', () => { expect(failBuild).toHaveBeenCalled() }) - test("fails with helpful warning if BUILD_ID doesn't exist and publish is 'out'", async () => { + it("fails with helpful warning if BUILD_ID doesn't exist and publish is 'out'", async () => { await moveNextDist() await unlink(path.join(process.cwd(), '.next/BUILD_ID')) const failBuild = jest.fn().mockImplementation((err) => { @@ -345,7 +340,7 @@ describe('onBuild()', () => { expect(failBuild).toHaveBeenCalled() }) - test('fails build if next export has run', async () => { + it('fails build if next export has run', async () => { await moveNextDist() await writeJSON(path.join(process.cwd(), '.next/export-detail.json'), {}) const failBuild = jest.fn() @@ -353,7 +348,7 @@ describe('onBuild()', () => { expect(failBuild).toHaveBeenCalled() }) - test('copy handlers to the internal functions directory', async () => { + it('copy handlers to the internal functions directory', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -366,7 +361,7 @@ describe('onBuild()', () => { expect(existsSync(`.netlify/functions-internal/___netlify-odb-handler/handlerUtils.js`)).toBeTruthy() }) - test('writes correct redirects to netlifyConfig', async () => { + it('writes correct redirects to netlifyConfig', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -376,14 +371,14 @@ describe('onBuild()', () => { expect(sorted).toMatchSnapshot() }) - test('publish dir is/has next dist', async () => { + it('publish dir is/has next dist', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.resolve('.next/BUILD_ID'))).toBeTruthy() }) - test('generates static files manifest', async () => { + it('generates static files manifest', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) const manifestPath = path.resolve('.next/static-manifest.json') @@ -392,7 +387,7 @@ describe('onBuild()', () => { expect(data).toMatchSnapshot() }) - test('moves static files to root', async () => { + it('moves static files to root', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) const data = JSON.parse(readFileSync(path.resolve('.next/static-manifest.json'), 'utf8')) @@ -402,7 +397,7 @@ describe('onBuild()', () => { }) }) - test('copies default locale files to top level', async () => { + it('copies default locale files to top level', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) const data = JSON.parse(readFileSync(path.resolve('.next/static-manifest.json'), 'utf8')) @@ -413,13 +408,13 @@ describe('onBuild()', () => { if (!file.startsWith(locale)) { return } - const trimmed = file.substring(locale.length) + const trimmed = file.slice(locale.length) expect(existsSync(path.resolve(path.join('.next', trimmed)))).toBeTruthy() }) }) // TODO - TO BE MOVED TO TEST AGAINST A PROJECT WITH MIDDLEWARE IN ANOTHER PR - test.skip('skips static files that match middleware', async () => { + it.skip('skips static files that match middleware', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -427,7 +422,7 @@ describe('onBuild()', () => { expect(existsSync(path.resolve(path.join('.next', 'server', 'pages', 'en', 'middle.html')))).toBeTruthy() }) - test('sets correct config', async () => { + it('sets correct config', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -510,7 +505,7 @@ describe('onBuild()', () => { } }) - test('generates a file referencing all page sources', async () => { + it('generates a file referencing all page sources', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) const handlerPagesFile = path.join(constants.INTERNAL_FUNCTIONS_SRC, HANDLER_FUNCTION_NAME, 'pages.js') @@ -522,7 +517,7 @@ describe('onBuild()', () => { expect(normalizeChunkNames(readFileSync(odbHandlerPagesFile, 'utf8'))).toMatchSnapshot() }) - test('generates a file referencing all when publish dir is a subdirectory', async () => { + it('generates a file referencing all when publish dir is a subdirectory', async () => { const dir = 'web/.next' await moveNextDist(dir) netlifyConfig.build.publish = path.resolve(dir) @@ -539,7 +534,7 @@ describe('onBuild()', () => { expect(normalizeChunkNames(readFileSync(odbHandlerPagesFile, 'utf8'))).toMatchSnapshot() }) - test('generates entrypoints with correct references', async () => { + it('generates entrypoints with correct references', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -558,7 +553,7 @@ describe('onBuild()', () => { expect(readFileSync(odbHandlerFile, 'utf8')).toMatch(`require("../../../.next/required-server-files.json")`) }) - test('handles empty routesManifest.staticRoutes', async () => { + it('handles empty routesManifest.staticRoutes', async () => { await moveNextDist() const manifestPath = path.resolve('.next/routes-manifest.json') const routesManifest = await readJson(manifestPath) @@ -568,7 +563,7 @@ describe('onBuild()', () => { expect(await nextRuntime.onBuild(defaultArgs)).toBeUndefined() }) - test('generates imageconfig file with entries for domains, remotePatterns, and custom response headers', async () => { + it('generates imageconfig file with entries for domains, remotePatterns, and custom response headers', async () => { await moveNextDist() const mockHeaderValue = chance.string() @@ -598,33 +593,33 @@ describe('onBuild()', () => { }) }) - test('generates an ipx function by default', async () => { + it('generates an ipx function by default', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.join('.netlify', 'functions-internal', '_ipx', '_ipx.js'))).toBeTruthy() }) // Enabled while edge images are off by default - test('does not generate an ipx edge function by default', async () => { + it('does not generate an ipx edge function by default', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.join('.netlify', 'edge-functions', 'ipx', 'index.ts'))).toBeFalsy() }) - test('generates an ipx edge function if force is set', async () => { + it('generates an ipx edge function if force is set', async () => { process.env.NEXT_FORCE_EDGE_IMAGES = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.join('.netlify', 'edge-functions', 'ipx', 'index.ts'))).toBeTruthy() }) - test('generates edge-functions manifest', async () => { + it('generates edge-functions manifest', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.join('.netlify', 'edge-functions', 'manifest.json'))).toBeTruthy() }) - test('generates generator field within the edge-functions manifest', async () => { + it('generates generator field within the edge-functions manifest', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) const manifestPath = await readJson(path.resolve('.netlify/edge-functions/manifest.json')) @@ -639,7 +634,7 @@ describe('onBuild()', () => { ) }) - test('generates generator field within the edge-functions manifest includes IPX', async () => { + it('generates generator field within the edge-functions manifest includes IPX', async () => { process.env.NEXT_FORCE_EDGE_IMAGES = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -655,7 +650,7 @@ describe('onBuild()', () => { ) }) - test('does not generate an ipx function when DISABLE_IPX is set', async () => { + it('does not generate an ipx function when DISABLE_IPX is set', async () => { process.env.DISABLE_IPX = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -663,7 +658,7 @@ describe('onBuild()', () => { delete process.env.DISABLE_IPX }) - test('creates 404 redirect when DISABLE_IPX is set', async () => { + it('creates 404 redirect when DISABLE_IPX is set', async () => { process.env.DISABLE_IPX = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -677,13 +672,13 @@ describe('onBuild()', () => { delete process.env.DISABLE_IPX }) - test('generates an ipx edge function by default', async () => { + it('generates an ipx edge function by default', async () => { await moveNextDist() await nextRuntime.onBuild(defaultArgs) expect(existsSync(path.join('.netlify', 'edge-functions', 'ipx', 'index.ts'))).toBeTruthy() }) - test('does not generate an ipx edge function if the feature is disabled', async () => { + it('does not generate an ipx edge function if the feature is disabled', async () => { process.env.NEXT_DISABLE_EDGE_IMAGES = '1' await moveNextDist() await nextRuntime.onBuild(defaultArgs) @@ -691,7 +686,7 @@ describe('onBuild()', () => { delete process.env.NEXT_DISABLE_EDGE_IMAGES }) - test('does not generate an ipx edge function if Netlify Edge is disabled', async () => { + it('does not generate an ipx edge function if Netlify Edge is disabled', async () => { process.env.NEXT_DISABLE_NETLIFY_EDGE = '1' await moveNextDist() @@ -708,7 +703,7 @@ describe('onBuild()', () => { delete process.env.NEXT_DISABLE_NETLIFY_EDGE }) - test('moves static files to a subdirectory if basePath is set', async () => { + it('moves static files to a subdirectory if basePath is set', async () => { await moveNextDist() const initialConfig = await getRequiredServerFiles(netlifyConfig.build.publish) @@ -727,7 +722,7 @@ describe('onBuild()', () => { }) describe('onPostBuild', () => { - test('saves cache with right paths', async () => { + it('saves cache with right paths', async () => { await moveNextDist() const save = jest.fn() @@ -740,7 +735,7 @@ describe('onPostBuild', () => { expect(save).toHaveBeenCalledWith(path.resolve('.next/cache')) }) - test('warns if old functions exist', async () => { + it('warns if old functions exist', async () => { await moveNextDist() const list = jest.fn().mockResolvedValue([ @@ -776,7 +771,7 @@ describe('onPostBuild', () => { console.log = oldLog }) - test('warns if NETLIFY_NEXT_PLUGIN_SKIP is set', async () => { + it('warns if NETLIFY_NEXT_PLUGIN_SKIP is set', async () => { await moveNextDist() process.env.NETLIFY_NEXT_PLUGIN_SKIP = 'true' @@ -790,7 +785,7 @@ describe('onPostBuild', () => { delete process.env.NETLIFY_NEXT_PLUGIN_SKIP }) - test('warns if NEXT_PLUGIN_FORCE_RUN is "false"', async () => { + it('warns if NEXT_PLUGIN_FORCE_RUN is "false"', async () => { await moveNextDist() process.env.NEXT_PLUGIN_FORCE_RUN = 'false' @@ -805,7 +800,7 @@ describe('onPostBuild', () => { delete process.env.NEXT_PLUGIN_FORCE_RUN }) - test('adds headers to Netlify configuration', async () => { + it('adds headers to Netlify configuration', async () => { await moveNextDist() const show = jest.fn() @@ -892,7 +887,7 @@ describe('onPostBuild', () => { ]) }) - test('appends headers to existing headers in the Netlify configuration', async () => { + it('appends headers to existing headers in the Netlify configuration', async () => { await moveNextDist() netlifyConfig.headers = [ @@ -994,7 +989,7 @@ describe('onPostBuild', () => { ]) }) - test('appends no additional headers in the Netlify configuration when none are in the routes manifest', async () => { + it('appends no additional headers in the Netlify configuration when none are in the routes manifest', async () => { await moveNextDist() netlifyConfig.headers = [ @@ -1232,7 +1227,7 @@ describe('the dev middleware watcher', () => { await isReady expect(middlewareExists()).toBeFalsy() await writeFile(path.join(process.cwd(), 'middleware.ts'), middlewareSourceTs) - let isBuilt = nextBuild() + const isBuilt = nextBuild() await writeFile(path.join(process.cwd(), 'middleware.js'), middlewareSourceJs) await isBuilt expect(middlewareExists()).toBeFalsy() diff --git a/test/templates/handlerUtils.spec.ts b/test/templates/handlerUtils.spec.ts index d5cc361943..26516ba2da 100644 --- a/test/templates/handlerUtils.spec.ts +++ b/test/templates/handlerUtils.spec.ts @@ -1,3 +1,9 @@ +import os from 'os' +import path from 'path' + +import { unlink, existsSync, readFileSync, ensureDir } from 'fs-extra' +import { join } from 'pathe' + import { normalizeRoute, unlocalizeRoute, @@ -5,10 +11,6 @@ import { localizeDataRoute, downloadFile, } from '../../packages/runtime/src/templates/handlerUtils' -import { join } from 'pathe' -import os from 'os' -import path from 'path' -import { unlink, existsSync, readFileSync, ensureDir } from 'fs-extra' describe('normalizeRoute', () => { it('removes a trailing slash from a route', () => { @@ -119,7 +121,7 @@ describe('downloadFile', () => { const url = 'https://example.com/nonexistentfile' const tmpFile = join(os.tmpdir(), 'next-test', 'downloadfile.txt') await ensureDir(path.dirname(tmpFile)) - await expect(downloadFile(url, tmpFile)).rejects.toThrowError( + await expect(downloadFile(url, tmpFile)).rejects.toThrow( 'Failed to download https://example.com/nonexistentfile: 404 Not Found', ) }) diff --git a/test/test-utils.ts b/test/test-utils.ts index 3010514d10..3d8a4fd54d 100644 --- a/test/test-utils.ts +++ b/test/test-utils.ts @@ -1,5 +1,5 @@ -import path from 'path' -import { dirname } from 'path' +import path, { dirname } from 'path' + import cpy from 'cpy' import { writeJSON, existsSync, ensureDir, readJson, copy } from 'fs-extra' import { dir as getTmpDir } from 'tmp-promise' @@ -26,11 +26,7 @@ const rewriteAppDir = async function (dir = '.next') { // Move .next from sample project to current directory export const moveNextDist = async function (dir = '.next', copyMods = false) { - if (copyMods) { - await copyModules(['next', 'sharp']) - } else { - await stubModules(['next', 'sharp']) - } + await (copyMods ? copyModules(['next', 'sharp']) : stubModules(['next', 'sharp'])); await ensureDir(dirname(dir)) await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir)) @@ -68,6 +64,7 @@ export const useFixture = async function (fixtureName) { // Change current cwd() to a temporary directory export const describeCwdTmpDir = (name: string, fn: () => void): void => { + // eslint-disable-next-line jest/valid-title describe(name, () => { let restoreCwd let cleanup From 69054c61fe00bad9ba5558d8930bb361abdbfff2 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Thu, 27 Apr 2023 14:56:11 +0200 Subject: [PATCH 07/10] chore: more manual fixes --- .eslintrc.js | 4 ++++ package-lock.json | 3 ++- package.json | 1 + test/helpers/edge.spec.ts | 10 ++++------ test/helpers/files.spec.ts | 4 ++-- test/helpers/matchers.spec.ts | 4 ++-- test/helpers/verification.spec.ts | 3 ++- test/index.spec.ts | 16 +++++++++------- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 0a6b965381..1510231221 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -72,6 +72,10 @@ module.exports = { 'prefer-destructuring': 0, '@typescript-eslint/no-unused-vars': 0, 'unicorn/no-await-expression-member': 0, + 'import/no-anonymous-default-export': 0, + 'no-shadow': 0, + '@typescript-eslint/no-var-requires': 0, + 'require-await': 0, // esling-plugin-jest specific rules 'jest/consistent-test-it': ['error', { fn: 'it', withinDescribe: 'it' }], 'jest/no-disabled-tests': 0, diff --git a/package-lock.json b/package-lock.json index d1634fd947..7be8f96680 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,6 +56,7 @@ "mock-fs": "^5.2.0", "netlify-plugin-cypress": "^2.2.1", "npm-run-all": "^4.1.5", + "outdent": "^0.8.0", "pathe": "^1.1.0", "playwright-chromium": "1.28.1", "prettier": "^2.1.2", @@ -27703,7 +27704,7 @@ "requires": { "@netlify/edge-functions": "^2.0.0", "@types/node": "^17.0.25", - "chance": "*", + "chance": "^1.1.11", "next": "^13.3.0", "npm-run-all": "^4.1.5", "typescript": "^4.6.3" diff --git a/package.json b/package.json index e55528a57b..9278d30d4b 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "mock-fs": "^5.2.0", "netlify-plugin-cypress": "^2.2.1", "npm-run-all": "^4.1.5", + "outdent": "^0.8.0", "pathe": "^1.1.0", "playwright-chromium": "1.28.1", "prettier": "^2.1.2", diff --git a/test/helpers/edge.spec.ts b/test/helpers/edge.spec.ts index 074071152b..66f5534a44 100644 --- a/test/helpers/edge.spec.ts +++ b/test/helpers/edge.spec.ts @@ -1,17 +1,15 @@ import type { PrerenderManifest } from 'next/dist/build' +import { NEXT_PLUGIN_NAME } from '../../packages/runtime/src/constants' import { generateRscDataEdgeManifest } from '../../packages/runtime/src/helpers/edge' -jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => { - const { NEXT_PLUGIN_NAME } = require('../../packages/runtime/src/constants') - return { +jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => ({ ...jest.requireActual('../../packages/runtime/src/helpers/functionsMetaData'), getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, - } -}) + })) const basePrerenderManifest: PrerenderManifest = { - version: 3, + version: 4, routes: {}, dynamicRoutes: {}, notFoundRoutes: [], diff --git a/test/helpers/files.spec.ts b/test/helpers/files.spec.ts index 4c8967969d..c4d0200731 100644 --- a/test/helpers/files.spec.ts +++ b/test/helpers/files.spec.ts @@ -14,7 +14,7 @@ import { getSourceFileForPage, } from '../../packages/runtime/src/helpers/files' import { Rewrites } from '../../packages/runtime/src/helpers/types' -import { describeCwdTmpDir, moveNextDist } from '../test-utils' +import { describeCwdTmpDir } from '../test-utils' const TEST_DIR = resolve(__dirname, '..') @@ -110,7 +110,7 @@ describe('files utility functions', () => { } }) - it('middleware tester matches root middleware', () => { + it('middleware tester does not match undefined', () => { const paths = [ 'middl', '', diff --git a/test/helpers/matchers.spec.ts b/test/helpers/matchers.spec.ts index c8d5196316..96cbca77a9 100644 --- a/test/helpers/matchers.spec.ts +++ b/test/helpers/matchers.spec.ts @@ -3,7 +3,7 @@ import { makeLocaleOptional, stripLookahead } from '../../packages/runtime/src/h const makeDataPath = (path: string) => `/_next/data/build-id${path === '/' ? '/index' : path}.json` -function checkPath(path: string, regex: string) { +const checkPath = (path: string, regex: string) => { const re = new RegExp(regex) const dataPath = makeDataPath(path) const testPath = re.test(path) @@ -29,7 +29,7 @@ describe('the middleware path matcher', () => { // The regex generated by Next for the path "/static" with i18n enabled (>= 13.3.0) const regexTwo = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/((?!_next\\/)[^/.]{1,}))\\/static(.json)?[\\/#\\?]?$' - function assertion(input) { + const assertion = (input) => { expect(checkPath('/static', input)).toBe(false) expect(checkPath('/static', makeLocaleOptional(input))).toBe(true) expect(checkPath('/en/static', makeLocaleOptional(input))).toBe(true) diff --git a/test/helpers/verification.spec.ts b/test/helpers/verification.spec.ts index 4198b1af69..99ff589b63 100644 --- a/test/helpers/verification.spec.ts +++ b/test/helpers/verification.spec.ts @@ -21,6 +21,8 @@ type FailBuild = NetlifyPluginUtils['build']['failBuild'] const chance = new Chance() +const { existsSync } = require('fs') + jest.mock('fs', () => ({ ...jest.requireActual('fs'), existsSync: jest.fn(), @@ -28,7 +30,6 @@ jest.mock('fs', () => ({ describe('checkNextSiteHasBuilt', () => { let failBuildMock - const { existsSync } = require('fs') beforeEach(() => { failBuildMock = jest.fn() as unknown as FailBuild diff --git a/test/index.spec.ts b/test/index.spec.ts index fa8040ca38..b08d42212f 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -10,6 +10,7 @@ import { join , relative } from 'pathe' import { dir as getTmpDir } from 'tmp-promise' // @ts-expect-error - TODO: Convert runtime export to ES6 +// eslint-disable-next-line import/default import nextRuntimeFactory from '../packages/runtime/src' import { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } from '../packages/runtime/src/constants' import { watchForMiddlewareChanges } from '../packages/runtime/src/helpers/compiler' @@ -44,6 +45,7 @@ const utils = { throw new Error(message) }, }, + // eslint-disable-next-line no-void run: async () => void 0, cache: { save: jest.fn(), @@ -101,15 +103,15 @@ afterEach(async () => { }) describe('preBuild()', () => { - it('fails if publishing the root of the project', () => { + it('fails if publishing the root of the project', async () => { defaultArgs.netlifyConfig.build.publish = path.resolve('.') - expect(nextRuntime.onPreBuild(defaultArgs)).rejects.toThrow( + await expect(nextRuntime.onPreBuild(defaultArgs)).rejects.toThrow( /Your publish directory is pointing to the base directory of your site/, ) }) - it('fails if the build version is too old', () => { - expect( + it('fails if the build version is too old', async () => { + await expect( nextRuntime.onPreBuild({ ...defaultArgs, constants: { IS_LOCAL: true, NETLIFY_BUILD_VERSION: '18.15.0' }, @@ -118,7 +120,7 @@ describe('preBuild()', () => { }) it('passes if the build version is new enough', async () => { - expect( + await expect( nextRuntime.onPreBuild({ ...defaultArgs, constants: { IS_LOCAL: true, NETLIFY_BUILD_VERSION: '18.16.1' }, @@ -320,7 +322,7 @@ describe('onBuild()', () => { const failBuild = jest.fn().mockImplementation((err) => { throw new Error(err) }) - expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( + await expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`, ) expect(failBuild).toHaveBeenCalled() @@ -334,7 +336,7 @@ describe('onBuild()', () => { }) netlifyConfig.build.publish = path.resolve('out') - expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( + await expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( `Your publish directory is set to "out", but in most cases it should be ".next".`, ) expect(failBuild).toHaveBeenCalled() From d118a4053df725a40c0565c36be16a9a7b9654fd Mon Sep 17 00:00:00 2001 From: LekoArts Date: Fri, 28 Apr 2023 08:32:22 +0200 Subject: [PATCH 08/10] chore: add types --- package-lock.json | 14 ++++++++++++++ packages/next/package.json | 1 + 2 files changed, 15 insertions(+) diff --git a/package-lock.json b/package-lock.json index 7be8f96680..ac4cbff04f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5894,6 +5894,12 @@ "@types/responselike": "^1.0.0" } }, + "node_modules/@types/chance": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@types/chance/-/chance-1.1.3.tgz", + "integrity": "sha512-X6c6ghhe4/sQh4XzcZWSFaTAUOda38GQHmq9BUanYkOE/EO7ZrkazwKmtsj3xzTjkLWmwULE++23g3d3CCWaWw==", + "dev": true + }, "node_modules/@types/fs-extra": { "version": "9.0.13", "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", @@ -24344,6 +24350,7 @@ "license": "MIT", "devDependencies": { "@netlify/edge-functions": "^2.0.0", + "@types/chance": "^1.1.3", "@types/node": "^17.0.25", "chance": "^1.1.11", "next": "^13.3.0", @@ -27703,6 +27710,7 @@ "version": "file:packages/next", "requires": { "@netlify/edge-functions": "^2.0.0", + "@types/chance": "*", "@types/node": "^17.0.25", "chance": "^1.1.11", "next": "^13.3.0", @@ -28464,6 +28472,12 @@ "@types/responselike": "^1.0.0" } }, + "@types/chance": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@types/chance/-/chance-1.1.3.tgz", + "integrity": "sha512-X6c6ghhe4/sQh4XzcZWSFaTAUOda38GQHmq9BUanYkOE/EO7ZrkazwKmtsj3xzTjkLWmwULE++23g3d3CCWaWw==", + "dev": true + }, "@types/fs-extra": { "version": "9.0.13", "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", diff --git a/packages/next/package.json b/packages/next/package.json index 7c5c250844..15667ad541 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -8,6 +8,7 @@ ], "devDependencies": { "@netlify/edge-functions": "^2.0.0", + "@types/chance": "^1.1.3", "@types/node": "^17.0.25", "chance": "^1.1.11", "next": "^13.3.0", From c2efa3c6e1d98fc396d72275a085662965f0b056 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Fri, 28 Apr 2023 08:33:51 +0200 Subject: [PATCH 09/10] chore: linting --- test/helpers/edge.spec.ts | 6 +++--- test/helpers/files.spec.ts | 2 +- test/helpers/verification.spec.ts | 9 ++++----- test/index.spec.ts | 19 ++++++++++--------- test/test-utils.ts | 2 +- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/test/helpers/edge.spec.ts b/test/helpers/edge.spec.ts index 66f5534a44..ba9a384c05 100644 --- a/test/helpers/edge.spec.ts +++ b/test/helpers/edge.spec.ts @@ -4,9 +4,9 @@ import { NEXT_PLUGIN_NAME } from '../../packages/runtime/src/constants' import { generateRscDataEdgeManifest } from '../../packages/runtime/src/helpers/edge' jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => ({ - ...jest.requireActual('../../packages/runtime/src/helpers/functionsMetaData'), - getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, - })) + ...jest.requireActual('../../packages/runtime/src/helpers/functionsMetaData'), + getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, +})) const basePrerenderManifest: PrerenderManifest = { version: 4, diff --git a/test/helpers/files.spec.ts b/test/helpers/files.spec.ts index c4d0200731..02672a3958 100644 --- a/test/helpers/files.spec.ts +++ b/test/helpers/files.spec.ts @@ -1,7 +1,7 @@ import path, { dirname } from 'path' import { readFileSync, copy, ensureDir } from 'fs-extra' -import { resolve , join } from 'pathe' +import { resolve, join } from 'pathe' import { matchMiddleware, diff --git a/test/helpers/verification.spec.ts b/test/helpers/verification.spec.ts index 99ff589b63..dcb508e551 100644 --- a/test/helpers/verification.spec.ts +++ b/test/helpers/verification.spec.ts @@ -1,4 +1,4 @@ -import type { NetlifyPluginOptions , NetlifyPluginUtils } from '@netlify/build' +import type { NetlifyPluginOptions, NetlifyPluginUtils } from '@netlify/build' import Chance from 'chance' import { outdent } from 'outdent' @@ -9,7 +9,6 @@ import { } from '../../packages/runtime/src/helpers/verification' import { describeCwdTmpDir, moveNextDist } from '../test-utils' - const netlifyConfig = { build: { command: 'npm run build' }, functions: {}, @@ -24,9 +23,9 @@ const chance = new Chance() const { existsSync } = require('fs') jest.mock('fs', () => ({ - ...jest.requireActual('fs'), - existsSync: jest.fn(), - })) + ...jest.requireActual('fs'), + existsSync: jest.fn(), +})) describe('checkNextSiteHasBuilt', () => { let failBuildMock diff --git a/test/index.spec.ts b/test/index.spec.ts index b08d42212f..1e9e5279f6 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,4 +1,3 @@ - import os from 'os' import path, { resolve } from 'path' import process from 'process' @@ -6,7 +5,7 @@ import process from 'process' import type { NetlifyPluginOptions } from '@netlify/build' import Chance from 'chance' import { writeJSON, unlink, existsSync, readFileSync, ensureDir, readJson, pathExists, writeFile, move } from 'fs-extra' -import { join , relative } from 'pathe' +import { join, relative } from 'pathe' import { dir as getTmpDir } from 'tmp-promise' // @ts-expect-error - TODO: Convert runtime export to ES6 @@ -20,9 +19,9 @@ import { getAllPageDependencies } from '../packages/runtime/src/templates/getPag import { changeCwd, useFixture, moveNextDist } from './test-utils' jest.mock('../packages/runtime/src/helpers/utils', () => ({ - ...jest.requireActual('../packages/runtime/src/helpers/utils'), - isNextAuthInstalled: jest.fn(), - })) + ...jest.requireActual('../packages/runtime/src/helpers/utils'), + isNextAuthInstalled: jest.fn(), +})) jest.mock('../packages/runtime/src/helpers/functionsMetaData', () => { const { NEXT_PLUGIN_NAME } = require('../packages/runtime/src/constants') @@ -322,7 +321,9 @@ describe('onBuild()', () => { const failBuild = jest.fn().mockImplementation((err) => { throw new Error(err) }) - await expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( + await expect(() => + nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } }), + ).rejects.toThrow( `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`, ) expect(failBuild).toHaveBeenCalled() @@ -336,9 +337,9 @@ describe('onBuild()', () => { }) netlifyConfig.build.publish = path.resolve('out') - await expect(() => nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( - `Your publish directory is set to "out", but in most cases it should be ".next".`, - ) + await expect(() => + nextRuntime.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } }), + ).rejects.toThrow(`Your publish directory is set to "out", but in most cases it should be ".next".`) expect(failBuild).toHaveBeenCalled() }) diff --git a/test/test-utils.ts b/test/test-utils.ts index 3d8a4fd54d..76313c1a69 100644 --- a/test/test-utils.ts +++ b/test/test-utils.ts @@ -26,7 +26,7 @@ const rewriteAppDir = async function (dir = '.next') { // Move .next from sample project to current directory export const moveNextDist = async function (dir = '.next', copyMods = false) { - await (copyMods ? copyModules(['next', 'sharp']) : stubModules(['next', 'sharp'])); + await (copyMods ? copyModules(['next', 'sharp']) : stubModules(['next', 'sharp'])) await ensureDir(dirname(dir)) await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), dir)) From 2e58c6cb1f6543fdeaad66b13fcaa9021a338548 Mon Sep 17 00:00:00 2001 From: LekoArts Date: Fri, 28 Apr 2023 08:58:04 +0200 Subject: [PATCH 10/10] chore: fix test --- test/helpers/edge.spec.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/helpers/edge.spec.ts b/test/helpers/edge.spec.ts index ba9a384c05..8ccfffabc8 100644 --- a/test/helpers/edge.spec.ts +++ b/test/helpers/edge.spec.ts @@ -1,12 +1,14 @@ import type { PrerenderManifest } from 'next/dist/build' -import { NEXT_PLUGIN_NAME } from '../../packages/runtime/src/constants' import { generateRscDataEdgeManifest } from '../../packages/runtime/src/helpers/edge' -jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => ({ - ...jest.requireActual('../../packages/runtime/src/helpers/functionsMetaData'), - getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, -})) +jest.mock('../../packages/runtime/src/helpers/functionsMetaData', () => { + const { NEXT_PLUGIN_NAME } = require('../../packages/runtime/src/constants') + return { + ...jest.requireActual('../../packages/runtime/src/helpers/functionsMetaData'), + getPluginVersion: async () => `${NEXT_PLUGIN_NAME}@1.0.0`, + } +}) const basePrerenderManifest: PrerenderManifest = { version: 4,