From 676f3e6103ceeb65e96ef474c2be5ca362e4c37e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= Date: Fri, 20 Sep 2024 10:37:32 +0200 Subject: [PATCH] fix tests so they can run on node 18 --- jest.config.mjs | 4 +++- packages/@sanity/block-tools/jest.config.mjs | 7 +++---- packages/@sanity/cli/jest.config.mjs | 7 +++---- packages/@sanity/codegen/jest.config.mjs | 5 ++--- packages/@sanity/mutator/jest.config.mjs | 5 ++--- packages/@sanity/schema/jest.config.mjs | 5 ++--- packages/@sanity/types/jest.config.mjs | 5 ++--- packages/@sanity/util/jest.config.mjs | 5 ++--- packages/sanity/jest.config.mjs | 8 +++----- test/config.mjs | 19 +++++++++++++++---- test/importMetaPonyFill.mjs | 5 +++++ 11 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 test/importMetaPonyFill.mjs diff --git a/jest.config.mjs b/jest.config.mjs index 36ceaf5d9bb..fc129693261 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -1,5 +1,6 @@ import path from 'node:path' import globby from 'globby' +import {getDirname} from './test/importMetaPonyFill.mjs' const jestConfigFiles = globby.sync('*/**/jest.config.mjs', { ignore: ['**/node_modules'], @@ -8,10 +9,11 @@ const jestConfigFiles = globby.sync('*/**/jest.config.mjs', { const IGNORE_PROJECTS = [] const p = jestConfigFiles - .map((file) => path.relative(import.meta.dirname, path.dirname(file))) + .map((file) => path.relative(getDirname(import.meta.url), path.dirname(file))) .filter((projectPath) => !IGNORE_PROJECTS.includes(projectPath)) .map((projectPath) => `/${projectPath}`) /** @type {import("jest").Config} */ + export default { projects: p, // Ignore e2e tests diff --git a/packages/@sanity/block-tools/jest.config.mjs b/packages/@sanity/block-tools/jest.config.mjs index 7990482f263..1b17cada179 100644 --- a/packages/@sanity/block-tools/jest.config.mjs +++ b/packages/@sanity/block-tools/jest.config.mjs @@ -1,10 +1,9 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName, resolveDirNameFromUrl} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), testEnvironment: 'node', - rootDir: import.meta.dirname, + rootDir: resolveDirNameFromUrl(import.meta.url), setupFilesAfterEnv: ['./test/setup.ts'], }) diff --git a/packages/@sanity/cli/jest.config.mjs b/packages/@sanity/cli/jest.config.mjs index 0b37b821638..712b7b63d96 100644 --- a/packages/@sanity/cli/jest.config.mjs +++ b/packages/@sanity/cli/jest.config.mjs @@ -1,12 +1,11 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName, resolveDirNameFromUrl} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), globalSetup: '/test/shared/globalSetup.ts', globalTeardown: '/test/shared/globalTeardown.ts', - rootDir: import.meta.dirname, + rootDir: resolveDirNameFromUrl(import.meta.url), setupFilesAfterEnv: ['/test/shared/setupAfterEnv.ts'], slowTestThreshold: 60000, testEnvironment: 'node', diff --git a/packages/@sanity/codegen/jest.config.mjs b/packages/@sanity/codegen/jest.config.mjs index c79a41aae76..d4e50d4a18d 100644 --- a/packages/@sanity/codegen/jest.config.mjs +++ b/packages/@sanity/codegen/jest.config.mjs @@ -1,9 +1,8 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), testEnvironment: 'node', }) diff --git a/packages/@sanity/mutator/jest.config.mjs b/packages/@sanity/mutator/jest.config.mjs index 38e3e1b01e3..ceed0d93ef6 100644 --- a/packages/@sanity/mutator/jest.config.mjs +++ b/packages/@sanity/mutator/jest.config.mjs @@ -1,7 +1,6 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), }) diff --git a/packages/@sanity/schema/jest.config.mjs b/packages/@sanity/schema/jest.config.mjs index c125fb71b52..4b8441d3b71 100644 --- a/packages/@sanity/schema/jest.config.mjs +++ b/packages/@sanity/schema/jest.config.mjs @@ -1,8 +1,7 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), testEnvironment: 'node', }) diff --git a/packages/@sanity/types/jest.config.mjs b/packages/@sanity/types/jest.config.mjs index c125fb71b52..4b8441d3b71 100644 --- a/packages/@sanity/types/jest.config.mjs +++ b/packages/@sanity/types/jest.config.mjs @@ -1,8 +1,7 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), testEnvironment: 'node', }) diff --git a/packages/@sanity/util/jest.config.mjs b/packages/@sanity/util/jest.config.mjs index 38e3e1b01e3..ceed0d93ef6 100644 --- a/packages/@sanity/util/jest.config.mjs +++ b/packages/@sanity/util/jest.config.mjs @@ -1,7 +1,6 @@ -import {createJestConfig} from '../../../test/config.mjs' +import {createJestConfig, readPackageName} from '../../../test/config.mjs' import fs from 'node:fs' import path from 'node:path' export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), }) diff --git a/packages/sanity/jest.config.mjs b/packages/sanity/jest.config.mjs index 413753c5072..4a96dfaf596 100644 --- a/packages/sanity/jest.config.mjs +++ b/packages/sanity/jest.config.mjs @@ -1,12 +1,10 @@ import path from 'node:path' -import {createJestConfig} from '../../test/config.mjs' -import fs from 'node:fs' +import {createJestConfig, readPackageName, resolveDirNameFromUrl} from '../../test/config.mjs' -const cliPath = path.resolve(import.meta.dirname, './src/_internal/cli') +const cliPath = path.resolve(resolveDirNameFromUrl(import.meta.url), './src/_internal/cli') export default createJestConfig({ - displayName: JSON.parse(fs.readFileSync(path.join(import.meta.dirname, 'package.json'), 'utf-8')) - .name, + displayName: readPackageName(import.meta.url), globalSetup: '/test/setup/global.ts', setupFiles: ['/test/setup/environment.ts'], setupFilesAfterEnv: ['/test/setup/afterEnv.ts'], diff --git a/test/config.mjs b/test/config.mjs index dbd49fcf86e..3decd7c1c6c 100644 --- a/test/config.mjs +++ b/test/config.mjs @@ -4,12 +4,23 @@ import {getJestAliases} from '@repo/dev-aliases' import path from 'node:path' import {escapeRegExp, omit} from 'lodash-es' +import {getDirname} from './importMetaPonyFill.mjs' +import fs from 'node:fs' +const dirname = getDirname(import.meta.url) + +export function resolveDirNameFromUrl(url) { + return getDirname(url) +} +export function readPackageName(dirNameUrl) { + return JSON.parse(fs.readFileSync(path.join(getDirname(dirNameUrl), 'package.json'), 'utf-8')) + .name +} /** Regex for matching file extensions. */ const RE_EXT = /\.[0-9a-z]+$/i /** Path to the root of the Sanity monorepo. */ -const ROOT_PATH = path.resolve(import.meta.dirname, '..') +const ROOT_PATH = path.resolve(dirname, '..') /** The default module name mapper (aka. aliases) for jest tests in the Sanity monorepo. */ const defaultModuleNameMapper = resolveAliasPaths({ @@ -58,9 +69,9 @@ export function createJestConfig(config = {}) { '/coverage/', '/lib/', ], - resolver: path.resolve(import.meta.dirname, './resolver.cjs'), - testEnvironment: path.resolve(import.meta.dirname, './jsdom.jest.env.ts'), - setupFiles: [...setupFiles, path.resolve(import.meta.dirname, './setup.ts')], + resolver: path.resolve(dirname, './resolver.cjs'), + testEnvironment: path.resolve(dirname, './jsdom.jest.env.ts'), + setupFiles: [...setupFiles, path.resolve(dirname, './setup.ts')], // testEnvironment: 'jsdom', testEnvironmentOptions: { url: 'http://localhost:3333', diff --git a/test/importMetaPonyFill.mjs b/test/importMetaPonyFill.mjs new file mode 100644 index 00000000000..4920658022a --- /dev/null +++ b/test/importMetaPonyFill.mjs @@ -0,0 +1,5 @@ +import {dirname} from 'node:path' + +export function getDirname(importMetaUrl) { + return dirname(importMetaUrl.replace('file://', '')) +}