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): jest 29 migration #5069

Merged
merged 18 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
91 changes: 87 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ on:
branches: [next]

jobs:
test:
install:
timeout-minutes: 60
name: Test (${{ matrix.os }} / node ${{ matrix.node }})
name: Install (${{ 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]
Expand Down Expand Up @@ -51,6 +50,19 @@ jobs:
${{ runner.os }}-modules-
${{ runner.os }}-

- name: Cache build
id: cache-build
uses: actions/cache@v3
env:
cache-name: cache-build
with:
path: './*'
key: ${{ runner.os }}-build-${{ matrix.node }}-${{ env.cache-name }}-${{ github.run_id }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-

- name: Install project dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile
Expand All @@ -75,11 +87,82 @@ jobs:
- name: Build CLI
run: yarn build:cli # Needed for CLI tests

test:
timeout-minutes: 60
name: Test (${{ 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 }}
needs: [install]

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: actions/cache@v3
id: restore-node-modules
env:
cache-name: cache-node-modules
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ env.cache-name }}-${{ hashFiles('**/yarn.lock') }}

- uses: actions/cache@v3
id: restore-build
env:
cache-name: cache-build
with:
path: ./*
key: ${{ runner.os }}-build-${{ matrix.node }}-${{ env.cache-name }}-${{ github.run_id }}

- name: Install project dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: yarn install --frozen-lockfile

- name: Store Playwright's Version
run: |
PLAYWRIGHT_VERSION=$(npx playwright --version | sed 's/Version //')
echo "Playwright's Version: $PLAYWRIGHT_VERSION"
echo "PLAYWRIGHT_VERSION=$PLAYWRIGHT_VERSION" >> $GITHUB_ENV

- name: Cache Playwright Browsers for Playwright's Version
id: cache-playwright-browsers
uses: actions/cache@v3
with:
path: ~/.cache/ms-playwright
key: playwright-browsers-${{ env.PLAYWRIGHT_VERSION }}

- name: Install Playwright Browsers
if: steps.cache-playwright-browsers.outputs.cache-hit != 'true'
run: npx playwright install --with-deps

- name: Test
id: test
run: |
node -v
npm -v
yarn test --silent
yarn test --silent --shard=${{ matrix.shardIndex}}/${{ matrix.shardTotal }}
env:
SANITY_CI_CLI_AUTH_TOKEN: ${{ secrets.SANITY_CI_CLI_AUTH_TOKEN }}
GITHUB_SHARD_IDENTIFIER: ${{ matrix.shardIndex }}-${{ matrix.shardTotal }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ blob-report
.yarn
.yarnrc
.turbo
.yalc
yalc.lock

## Documentation Report
scripts/docs-report.md

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@
"@sanity/pkg-utils": "^2.3.10",
"@sanity/tsdoc": "1.0.0-alpha.38",
"@sanity/uuid": "^3.0.2",
"@types/jest": "^27.5.2",
"@types/jest": "^29.5.6",
"@types/mkdirp": "^1.0.2",
"@types/node": "^14.18.9",
"@types/react": "^18.2.25",
"@types/tmp": "^0.2.3",
"@types/yargs": "^17.0.7",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"babel-jest": "^27.5.1",
"babel-jest": "^29.7.0",
"cac": "^6.7.12",
"chalk": "^4.1.2",
"depcheck": "^1.4.3",
Expand All @@ -123,7 +123,9 @@
"glob": "^7.2.0",
"globby": "^10.0.0",
"husky": "^7.0.4",
"jest": "^27.5.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-environment-node": "^29.7.0",
"lerna": "^4.0.0",
"lint-staged": "^12.1.2",
"lodash": "^4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/test/shared/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const getTestId = () => {

const localId = readFileSync(testIdPath, 'utf8').trim()
const ghRunId = `${process.env.GITHUB_RUN_ID || ''}`.slice(-4)
const ghId = `${ghRunId}-${process.env.GITHUB_RUN_NUMBER}-${process.env.GITHUB_RUN_ATTEMPT}`
const ghId = `${ghRunId}-${process.env.GITHUB_RUN_NUMBER}-${process.env.GITHUB_RUN_ATTEMPT}-${process.env.GITHUB_SHARD_IDENTIFIER}`
const githubId = process.env.GITHUB_RUN_ID ? `gh-${ghId}` : ''
const runId = `${githubId || localId}`.replace(/\W/g, '-').replace(/(^-+|-+$)/g, '')

Expand Down
6 changes: 3 additions & 3 deletions packages/@sanity/portable-text-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"@types/debug": "^4.1.5",
"@types/express": "^4.16.1",
"@types/express-ws": "^3.0.1",
"@types/jest": "^27.5.2",
"@types/jest": "^29.5.6",
"@types/lodash": "^4.14.149",
"@types/node": "^14.18.9",
"@types/node-ipc": "^9.2.0",
Expand All @@ -92,10 +92,10 @@
"@vitejs/plugin-react": "^4.1.0",
"express": "^4.17.1",
"express-ws": "^5.0.2",
"jest": "^27.5.1",
"jest": "^29.7.0",
"jest-dev-server": "^9.0.0",
"jest-environment-node": "^27.5.1",
"node-ipc": "npm:@node-ipc/[email protected]",
"jest-environment-node": "^29.7.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rimraf": "^3.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module.exports = createJestConfig({
globalSetup: '<rootDir>/test/setup/global.ts',
setupFiles: ['<rootDir>/test/setup/environment.ts'],
setupFilesAfterEnv: ['<rootDir>/test/setup/afterEnv.ts'],
modulePathIgnorePatterns: ['<rootDir>/playwright-ct'],
})
17 changes: 10 additions & 7 deletions test/config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,16 @@ exports.createJestConfig = function createJestConfig(
'<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')],
testEnvironment: 'jsdom',
// testEnvironment: 'jsdom',
testEnvironmentOptions: {
url: 'http://localhost:3333',
},
/**
* Normally jest would look for test filenames matching {spec|test}.{js,ts,tsx}
* Currently component tests using playwright "owns" test files with the .spec.tsx ending
* while unit tests with jest "owns" test files with the .test.{ts|tsx} ending.
*/
testMatch: [...testMatch, '<rootDir>/**/*.test.{js,ts,tsx}'],
testMatch: [...testMatch, '<rootDir>/**/*.{test,spec}.{js,ts,tsx}'],
transformIgnorePatterns: ['/node_modules/(?!(get-random-values-esm)/)'],
testPathIgnorePatterns: ['/node_modules/', '/.yalc/'],
transform: {
...transform,
'\\.[jt]sx?$': [
Expand All @@ -78,6 +77,10 @@ exports.createJestConfig = function createJestConfig(
{rootMode: 'upward'},
],
},
snapshotFormat: {
escapeString: true,
printBasicPrototype: true,
},
...restOfInputConfig,
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/jsdom.jest.env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import JSDOMEnvironment from 'jest-environment-jsdom'

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')
this.global.TextEncoder = TextEncoder
this.global.TextDecoder = TextDecoder
}
}
}
29 changes: 29 additions & 0 deletions test/resolver.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: (pkg) => {
// This is a workaround for https://github.com/uuidjs/uuid/pull/616
//
// jest-environment-jsdom 28+ tries to use browser exports instead of default exports,
// but uuid only offers an ESM browser export and not a CommonJS one. Jest does not yet
// support ESM modules natively, so this causes a Jest error related to trying to parse
// "export" syntax.
//
// This workaround prevents Jest from considering uuid's module-based exports at all;
// it falls back to uuid's CommonJS+node "main" property.
//
// Once we're able to migrate our Jest config to ESM and a browser crypto
// implementation is available for the browser+ESM version of uuid to use (eg, via
// https://github.com/jsdom/jsdom/pull/3352 or a similar polyfill), this can go away.
if (['nanoid', 'uuid', 'get-random-values-esm'].includes(pkg.name)) {
delete pkg.exports
delete pkg.module
}
return pkg
},
})
}
Loading
Loading