forked from hashicorp/dev-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.config.ts
71 lines (64 loc) · 1.9 KB
/
jest.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { Config } from '@jest/types'
import makeConfig from '@hashicorp/platform-configs/jest/config.js'
/**
* We use a dynamic jest config in order to be able to run both
* ESM and CommonJS tests using the same config, with separate test commands.
*
* This approach was taken from `@hashicorp/web-platform-packages`.
*/
const isRunningInEsmMode = !!process.env.TEST_ESM
/**
* These tests are authored using native ESModules,
* and so we want to configure jest differently to handle that.
*
* TODO: determine this from "type": "module" in package.json
*/
const ESM_TEST_DIRS = ['src/lib/remark-plugins/rehype-sanitize']
/**
* Override the base next jest-transformer to force it into ESM mode
*/
const esmConfig: Config.InitialOptions = {
setupFilesAfterEnv: [],
extensionsToTreatAsEsm: ['.ts', '.tsx'],
transform: {
'^.+\\.(js|jsx|ts|tsx|mjs)$': [
require.resolve('next/dist/build/swc/jest-transformer.js'),
{
isEsmProject: true,
},
],
},
}
/**
* Exclude ESM-native packages from running when not in ESM mode
*/
const ignorePatternForModuleType = isRunningInEsmMode
? `<rootDir>/(?!${ESM_TEST_DIRS.join('|')}).*/.*`
: `<rootDir>/(${ESM_TEST_DIRS.join('|')})/.*`
/**
* Build the final config.
*
* Note that when `isRunningInEsmMode`, options may be overridden
* when spreading `...esmConfig`.
*/
const config: Config.InitialOptions = {
setupFilesAfterEnv: ['<rootDir>/.test/setup-jest.js'],
roots: ['config', 'src', 'build-libs', 'stylelint-rules'],
moduleDirectories: ['node_modules', '<rootDir>/src'],
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
],
testPathIgnorePatterns: [
ignorePatternForModuleType,
'<rootDir>/src/__tests__/e2e',
],
testEnvironment: 'jsdom',
...(isRunningInEsmMode && esmConfig),
}
export default makeConfig({ nextDir: './', ...config })