Skip to content

Commit

Permalink
chore(test): move jest config to @repo/test-config/jest
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Sep 25, 2024
1 parent 8938d6e commit 24618f9
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 43 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@
},
"prettier": "@sanity/prettier-config",
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/preset-env": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@google-cloud/storage": "^7.11.0",
"@jest/globals": "^29.7.0",
"@playwright/test": "1.44.1",
"@repo/dev-aliases": "workspace:*",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@sanity/client": "^6.21.3",
"@sanity/eslint-config-i18n": "1.0.0",
Expand Down Expand Up @@ -152,7 +152,6 @@
"globby": "^10.0.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"js-yaml": "^4.1.0",
"lerna": "^8.1.8",
"lint-staged": "^12.1.2",
Expand Down
40 changes: 25 additions & 15 deletions test/config.cjs → ...epo/test-config/jest/createJestConfig.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
/* eslint-disable tsdoc/syntax */

const path = require('node:path')
const {escapeRegExp, omit} = require('lodash')
const devAliases = require('../dev/aliases.cjs')
import devAliases from '@repo/dev-aliases'

import path from 'node:path'
import {escapeRegExp, omit} from 'lodash-es'
import {resolveDirName} from './resolveDirName.mjs'

const dirname = resolveDirName(import.meta.url)

/** 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(__dirname, '..')
const ROOT_PATH = path.resolve(dirname, '..', '..', '..', '..')

const jestAliases = Object.fromEntries(
Object.entries(devAliases).map(([packageName, aliasPath]) => [
packageName,
path.join('./packages', aliasPath),
]),
)

/** The default module name mapper (aka. aliases) for jest tests in the Sanity monorepo. */
const defaultModuleNameMapper = resolveAliasPaths({
...aliasesToModuleNameWrapper(devAliases),
...aliasesToModuleNameWrapper(jestAliases),
'.*\\.module\\.css$': './test/mocks/emptyObject',
'.*\\.css$': './test/mocks/undefined',
})
Expand All @@ -23,7 +34,7 @@ const defaultModuleNameMapper = resolveAliasPaths({
* @param {import('jest').Config} config - Initial Jest configuration options.
* @returns {import('jest').Config} The resulting Jest configuration options.
*/
exports.createJestConfig = function createJestConfig(config = {}) {
export function createJestConfig(config = {}) {
const {
testMatch = [],
setupFiles = [],
Expand Down Expand Up @@ -57,9 +68,9 @@ exports.createJestConfig = function createJestConfig(config = {}) {
'<rootDir>/coverage/',
'<rootDir>/lib/',
],
resolver: path.resolve(__dirname, './resolver.cjs'),
testEnvironment: path.resolve(__dirname, './jsdom.jest.env.ts'),
setupFiles: [...setupFiles, path.resolve(__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 All @@ -81,18 +92,17 @@ exports.createJestConfig = function createJestConfig(config = {}) {
'@babel/preset-env',
{
targets: {
node: '14',
chrome: '61',
safari: '11.3',
firefox: '60',
edge: '79',
node: '18',
safari: '16',
chrome: '110',
firefox: '110',
edge: '110',
},
},
],
'@babel/preset-typescript',
['@babel/preset-react', {runtime: 'automatic'}],
],
plugins: ['@babel/plugin-proposal-class-properties'],
},
],
},
Expand Down
3 changes: 3 additions & 0 deletions packages/@repo/test-config/jest/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {createJestConfig} from './createJestConfig.mjs'
export {resolveDirName} from './resolveDirName.mjs'
export {readPackageName} from './readPackageName.mjs'
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default class JSDOMEnvironmentWithDomRange extends JSDOMEnvironment {
public async setup(): Promise<void> {
await super.setup()
if (typeof this.global.TextEncoder === 'undefined') {
const {TextEncoder, TextDecoder} = require('util')
const {TextEncoder, TextDecoder} = require('node:util')
this.global.TextEncoder = TextEncoder
this.global.TextDecoder = TextDecoder
}
Expand Down
8 changes: 8 additions & 0 deletions packages/@repo/test-config/jest/readPackageName.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'node:fs'
import path from 'node:path'
import {resolveDirName} from './resolveDirName.mjs'

export function readPackageName(dirNameUrl) {
return JSON.parse(fs.readFileSync(path.join(resolveDirName(dirNameUrl), 'package.json'), 'utf-8'))
.name
}
5 changes: 5 additions & 0 deletions packages/@repo/test-config/jest/resolveDirName.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {dirname} from 'node:path'

export function resolveDirName(importMetaUrl) {
return dirname(importMetaUrl.replace('file://', ''))
}
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions packages/@repo/test-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@repo/test-config",
"version": "3.57.4",
"private": true,
"description": "Test (as in unit test) config shared across packages in the sanity monorepo",
"type": "module",
"exports": {
"./jest": "./jest/index.mjs",
"./vitest": "./vitest/index.mjs"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/dev-aliases": "workspace:*",
"dotenv": "^16.4.5",
"jest-environment-jsdom": "^29.7.0",
"lodash-es": "^4.17.21"
}
}
1 change: 1 addition & 0 deletions packages/@sanity/block-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@sanity/schema": "3.58.0",
"@types/jsdom": "^20.0.0",
"@types/lodash": "^4.17.7",
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@rexxars/gitconfiglocal": "^3.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@sanity/eslint-config-studio": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.8",
"@types/babel__register": "^7.17.3",
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/mutator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@types/debug": "^4.1.5",
"@types/lodash": "^4.17.7",
"rimraf": "^3.0.2"
Expand Down
2 changes: 2 additions & 0 deletions packages/@sanity/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@sanity/icons": "^3.4.0",
"@types/arrify": "^1.0.4",
"@types/lodash": "^4.17.7",
"@types/object-inspect": "^1.13.0",
"@types/react": "^18.3.5",
"rimraf": "^3.0.2"
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@sanity/insert-menu": "1.0.9",
"rimraf": "^3.0.2"
}
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@types/moment": "^2.13.0",
"rimraf": "^3.0.2"
},
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
"@playwright/test": "1.44.1",
"@repo/dev-aliases": "workspace:*",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@sanity/codegen": "3.58.0",
"@sanity/generate-help-url": "^3.0.0",
"@sanity/pkg-utils": "6.11.2",
Expand Down
1 change: 1 addition & 0 deletions perf/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/test-config": "workspace:*",
"@types/lodash": "^4.17.7",
"@types/node": "^18.15.3",
"esbuild": "0.21.5",
Expand Down
Loading

0 comments on commit 24618f9

Please sign in to comment.