Files
brave-core/jest.config.ts
T
Pete Miller 48bb69bd43 Upgrade jest to 30.3.0 (#35584)
* Upgrade jest to 30.3.0

Remove use of https://github.com/brave/jest

# Conflicts:
#	package-lock.json

* more improvements that can be made with jest upgrade

* format
2026-04-21 10:07:34 +01:00

167 lines
5.6 KiB
TypeScript

/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html
import fs from 'fs'
import type { Config } from 'jest'
import { createJsWithTsEsmPreset } from 'ts-jest'
const crossPlatforms = ['mac', 'win']
const buildConfigs = ['Component', 'Static', 'Debug', 'Release']
const extraArchitectures = ['arm64', 'x86']
function getBuildOutputPathList(buildOutputRelativePath: string): string[] {
return buildConfigs
.reduce(
(outDirs, outDir) => [
...outDirs,
outDir,
...crossPlatforms.map((platform) => `${platform}_${outDir}`),
],
[],
)
.reduce(
(outDirs, outDir) => [
...outDirs,
outDir,
...extraArchitectures.map((arch) => `${outDir}_${arch}`),
],
[],
)
.map((outDir) => `<rootDir>/../out/${outDir}/${buildOutputRelativePath}`)
}
function getReporters() {
if (process.env.TEAMCITY_VERSION !== undefined) {
return [
[
'<rootDir>/tools/jest_teamcity_reporter/jest_teamcity_reporter.js',
{ 'suiteName': 'test-unit' },
],
]
} else {
return ['default']
}
}
function getBuildConfig() {
// Try to find the build config in any of the possible build directories
const possiblePaths = getBuildOutputPathList('gen/brave/build_flags.json')
for (const configPath of possiblePaths) {
// Remove <rootDir> placeholder and resolve the actual path
const actualPath = configPath.replace('<rootDir>', import.meta.dirname)
if (fs.existsSync(actualPath)) {
try {
return JSON.parse(fs.readFileSync(actualPath, 'utf8'))
} catch (e) {
// Continue to next path
}
}
}
// Default to all features enabled if no config found
return {
enable_ai_chat: true,
enable_brave_wallet: true,
enable_email_aliases: true,
}
}
const buildConfig = getBuildConfig()
const jestConfig: Config = {
...createJsWithTsEsmPreset({
tsconfig: 'tsconfig-jest.json',
useESM: true,
}),
testEnvironment: '<rootDir>/components/test/testEnvironment.ts',
moduleFileExtensions: ['js', 'tsx', 'ts', 'json'],
reporters: getReporters(),
clearMocks: true,
resetMocks: true,
resetModules: true,
coverageProvider: 'v8',
collectCoverageFrom: [
'<rootDir>/build/commands/lib/*',
'<rootDir>/components/**/**/*.ts',
'<rootDir>/components/**/**/*.tsx',
'!<rootDir>/components/definitions/*',
'!<rootDir>/components/**/constants/*',
'!<rootDir>/components/**/reducers/index.ts',
'!<rootDir>/components/**/store.ts',
'!<rootDir>/components/test/*',
'!<rootDir>/build/commands/lib/start.js',
'!<rootDir>/build/commands/lib/jsconfig.json',
],
testMatch: [
'<rootDir>/**/*.test.{js,ts,tsx}',
'<rootDir>/components/test/**/*_test.{ts,tsx}',
],
testPathIgnorePatterns: [
'<rootDir>/build/commands/lib/test.js',
'<rootDir>/build/rustup',
'<rootDir>/third_party',
'<rootDir>/tools/crates/vendor',
...(buildConfig.enable_ai_chat ? [] : ['<rootDir>/components/ai_chat']),
...(buildConfig.enable_brave_wallet
? []
: ['<rootDir>/components/brave_wallet_ui']),
...(buildConfig.enable_email_aliases
? []
: ['<rootDir>/components/email_aliases']),
],
testTimeout: 30000,
transformIgnorePatterns: [
// prevent jest from transforming itself
// https://github.com/jestjs/jest/issues/9503#issuecomment-709041807
'<rootDir>/node_modules/@babel',
'<rootDir>/node_modules/@jest',
'<rootDir>/node_modules/jest-runtime',
'<rootDir>/node_modules/lodash',
'signal-exit',
'is-typedarray',
],
setupFilesAfterEnv: ['<rootDir>/components/test/testSetup.ts'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|svg|ttf|woff|woff2)$':
'<rootDir>/components/test/fileMock.ts',
'\\.(css|less|scss)$': 'identity-obj-proxy',
'^\\$web-common\\/(.*)': '<rootDir>/components/common/$1',
'^brave-ui$': '<rootDir>/node_modules/@brave/brave-ui',
'^brave-ui\\/(.*)': '<rootDir>/node_modules/@brave/brave-ui/$1',
'^\\$test-utils\\/(.*)': '<rootDir>/components/test/$1',
// mocks for brave-wallet and brave-rewards proxies
'\\/brave_rewards_api_proxy$':
'<rootDir>/components/brave_wallet_ui/'
+ 'common/async/__mocks__/brave_rewards_api_proxy.ts',
'\\/bridge$':
'<rootDir>/components/brave_wallet_ui/common/async/__mocks__/bridge.ts',
// TODO(https://github.com/brave/brave-browser/issues/54640): The ordering
// here can get problematic for devs who have more than 1 build type at a
// time, since if the file exists at the first path, it will be used for
// Type analysis instead of the second path, even if it's more recent.
// It can also break if CI or devs perform a build in a directory not known
// by this list.
// Instead, we should get the directory from config.js:outputDir.
'^gen\\/(.*)': getBuildOutputPathList('gen/$1'),
'chrome://resources\\/(.*)': getBuildOutputPathList(
'gen/ui/webui/resources/tsc/$1',
),
'chrome://interstitials\\/(.*)': getBuildOutputPathList(
'gen/components/security_interstitials/core/$1',
),
// workaround for https://github.com/LedgerHQ/ledger-live/issues/763
'@ledgerhq/devices/hid-framing':
'<rootDir>/node_modules/@ledgerhq/devices/lib/hid-framing',
},
}
export default jestConfig