Skip to content

Commit

Permalink
fix tests so they can run on node 18
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Sep 20, 2024
1 parent fe17269 commit 676f3e6
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 33 deletions.
4 changes: 3 additions & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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'],
Expand All @@ -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) => `<rootDir>/${projectPath}`)
/** @type {import("jest").Config} */

export default {
projects: p,
// Ignore e2e tests
Expand Down
7 changes: 3 additions & 4 deletions packages/@sanity/block-tools/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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'],
})
7 changes: 3 additions & 4 deletions packages/@sanity/cli/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/test/shared/globalSetup.ts',
globalTeardown: '<rootDir>/test/shared/globalTeardown.ts',
rootDir: import.meta.dirname,
rootDir: resolveDirNameFromUrl(import.meta.url),
setupFilesAfterEnv: ['<rootDir>/test/shared/setupAfterEnv.ts'],
slowTestThreshold: 60000,
testEnvironment: 'node',
Expand Down
5 changes: 2 additions & 3 deletions packages/@sanity/codegen/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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',
})
5 changes: 2 additions & 3 deletions packages/@sanity/mutator/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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),
})
5 changes: 2 additions & 3 deletions packages/@sanity/schema/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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',
})
5 changes: 2 additions & 3 deletions packages/@sanity/types/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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',
})
5 changes: 2 additions & 3 deletions packages/@sanity/util/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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),
})
8 changes: 3 additions & 5 deletions packages/sanity/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -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: '<rootDir>/test/setup/global.ts',
setupFiles: ['<rootDir>/test/setup/environment.ts'],
setupFilesAfterEnv: ['<rootDir>/test/setup/afterEnv.ts'],
Expand Down
19 changes: 15 additions & 4 deletions test/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -58,9 +69,9 @@ export function createJestConfig(config = {}) {
'<rootDir>/coverage/',
'<rootDir>/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',
Expand Down
5 changes: 5 additions & 0 deletions test/importMetaPonyFill.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {dirname} from 'node:path'

export function getDirname(importMetaUrl) {
return dirname(importMetaUrl.replace('file://', ''))
}

0 comments on commit 676f3e6

Please sign in to comment.