Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): refactor test config, enable vitest and migrate packages to vitest #5946

Merged
merged 13 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cli-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: |
node -v
npm -v
pnpm test -- --silent --selectProjects=@sanity/cli
pnpm test:jest --silent --selectProjects=@sanity/cli
env:
# Update token in github and change below to ${{ secrets.SANITY_CI_CLI_AUTH_TOKEN }} after merge to next
SANITY_CI_CLI_AUTH_TOKEN_STAGING: ${{ secrets.SANITY_CI_CLI_AUTH_TOKEN_STAGING }}
77 changes: 77 additions & 0 deletions .github/workflows/jest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Unit tests

on:
# Build on pushes branches that have a PR (including drafts)
pull_request:
# Build on commits pushed to branches without a PR if it's in the allowlist
push:
branches: [next]

jobs:
test:
timeout-minutes: 60
name: Jest tests (${{ matrix.os }} / node ${{ matrix.node }})
runs-on: ${{ matrix.os }}
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
continue-on-error: ${{ matrix.experimental }}

strategy:
# we want to know if a test fails on a specific node version
fail-fast: false
matrix:
os: [ubuntu-latest]
node: [18, 20]
experimental: [false]
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
# include:
# - os: windows-latest
# node: 16
# experimental: true

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- uses: pnpm/action-setup@v4
name: Install pnpm
id: pnpm-install
with:
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

- name: Cache node modules
id: cache-node-modules
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ env.cache-name }}-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
v1-${{ runner.os }}-pnpm-store-${{ env.cache-name }}-
v1-${{ runner.os }}-pnpm-store-
v1-${{ runner.os }}-

- name: Install project dependencies
run: pnpm install

- name: Test
id: test
run: |
node -v
pnpm test:jest --silent --shard=${{ matrix.shardIndex}}/${{ matrix.shardTotal }} --ignoreProjects=@sanity/cli
env:
GITHUB_SHARD_IDENTIFIER: ${{ matrix.shardIndex }}-${{ matrix.shardTotal }}
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,4 @@ jobs:
id: test
run: |
node -v
npm -v
pnpm test -- --silent --shard=${{ matrix.shardIndex}}/${{ matrix.shardTotal }} --ignoreProjects=@sanity/cli
env:
GITHUB_SHARD_IDENTIFIER: ${{ matrix.shardIndex }}-${{ matrix.shardTotal }}
pnpm test:vitest
32 changes: 0 additions & 32 deletions dev/aliases.cjs

This file was deleted.

20 changes: 0 additions & 20 deletions jest.config.cjs

This file was deleted.

17 changes: 17 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This tells jest which projects exists
*/
import path from 'node:path'
import globby from 'globby'
import {resolveDirName} from '@repo/test-config/jest'

const jestConfigFiles = globby.sync('*/**/jest.config.mjs', {ignore: ['**/node_modules']})

/** @type {import("jest").Config} */
export default {
projects: jestConfigFiles
.map((file) => path.relative(resolveDirName(import.meta.url), path.dirname(file)))
.map((projectPath) => `<rootDir>/${projectPath}`),
// Ignore e2e tests
modulePathIgnorePatterns: ['<rootDir>/test/'],
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@
"release:rc": "pnpm build && lerna publish --force-publish --dist-tag=dev-preview --preid=rc --exact",
"release:tag": "RELEASE_TAG=\"${RELEASE_TAG:-canary}\"; pnpm build && lerna publish --canary --force-publish --no-git-tag-version --dist-tag=$RELEASE_TAG --preid=$RELEASE_TAG --exact",
"start": "run-s dev",
"test": "jest --forceExit",
"test": "run-s test:vitest test:jest",
"test:jest": "jest --forceExit",
"test:vitest": "vitest --run",
"test:e2e": "playwright test",
"test:exports": "turbo run test --filter=@repo/test-exports",
"tsdoc:dev": "sanity-tsdoc dev",
Expand All @@ -97,14 +99,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 +154,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 All @@ -169,6 +170,7 @@
"typescript": "5.6.2",
"vite": "^4.5.3",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.1.1",
"yargs": "^17.3.0"
},
"optionalDependencies": {
Expand Down
33 changes: 33 additions & 0 deletions packages/@repo/dev-aliases/dev-aliases.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable tsdoc/syntax */

// NOTE: THIS FILE NEEDS TO REMAIN COMMONJS
// It can be converted to ESM/TS when we either not use jest anymore, or node/jest runner natively supports ESM (including with import.meta etc).

/**
* The path mappings/aliases used by various tools in the monorepo to map imported modules to
* source files in order to speed up rebuilding and avoid having a separate watcher process to build
* from `src` to `lib`.
*
* This file is currently read by:
* - Vite when running the dev server (only when running in this monorepo)
* - jest when running test suite
*
* @type Record<string, string>
*/
const devAliases = {
// NOTE: do not use regex in the module expressions,
// because they will be escaped by the jest config
'@sanity/block-tools': '@sanity/block-tools/src',
'@sanity/diff': '@sanity/diff/src',
'@sanity/cli': '@sanity/cli/src',
'@sanity/mutator': '@sanity/mutator/src',
'@sanity/schema': '@sanity/schema/src/_exports',
'@sanity/migrate': '@sanity/migrate/src/_exports',
'@sanity/types': '@sanity/types/src',
'@sanity/util': '@sanity/util/src/_exports',
'@sanity/vision': '@sanity/vision/src',
'sanity': 'sanity/src/_exports',
'groq': 'groq/src/_exports.mts',
}

module.exports = devAliases
12 changes: 12 additions & 0 deletions packages/@repo/dev-aliases/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@repo/dev-aliases",
"version": "3.57.4",
"private": true,
"description": "Dev aliases for the sanity monorepo",
"type": "module",
"exports": {
".": "./dev-aliases.cjs",
"./vite": "./vite.mjs"
},
"main": "./dev-aliases.cjs"
}
18 changes: 18 additions & 0 deletions packages/@repo/dev-aliases/vite.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import devAliases from './dev-aliases.cjs'

import {dirname, resolve} from 'node:path'

const PACKAGES_PATH = resolve(getDirname(import.meta.url), '..', '..')

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

export function getViteAliases() {
return Object.fromEntries(
Object.entries(devAliases).map(([packageName, aliasPath]) => [
packageName,
resolve(PACKAGES_PATH, aliasPath),
]),
)
}
18 changes: 18 additions & 0 deletions packages/@repo/dev-aliases/vitest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import devAliases from './dev-aliases.cjs'

import {dirname, resolve} from 'node:path'

const PACKAGES_PATH = resolve(getDirname(import.meta.url), '..', '..')

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

export function getViteAliases() {
return Object.fromEntries(
Object.entries(devAliases).map(([packageName, aliasPath]) => [
packageName,
resolve(PACKAGES_PATH, aliasPath),
]),
)
}
2 changes: 1 addition & 1 deletion packages/@repo/package.bundle/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path')
const path = require('node:path')

const ROOT_PATH = path.resolve(__dirname, '../../..')

Expand Down
4 changes: 1 addition & 3 deletions packages/@repo/package.config/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict'

const path = require('path')
const path = require('node:path')

const ROOT_PATH = path.resolve(__dirname, '../../..')

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'
Loading
Loading