This package contains a set of jest configs that set up runners for running unit tests, integration tests, eslint, and prettier.
Use the package manager of your choice to install:
-
npm:
npm install --save-dev @cprussin/jest-config
-
pnpm:
pnpm add -D @cprussin/jest-config
-
yarn:
yarn add -D @cprussin/jest-config
- Set up your
jest.config.js
, for example:
export { base as default } from "@cprussin/jest-config";
-
Run
jest
to run format checks, lint checks, unit tests, and integration tests, or use--selectProjects
to run a subset of checks. -
Add scripts to your
package.json
if you'd like. For instance:
{
"scripts": {
"test:format": "jest --selectProjects format",
"test:integration": "jest --selectProjects integration",
"test:lint": "jest --selectProjects lint",
"test:unit": "jest --coverage --selectProjects unit"
}
}
Configurations exported with this package will set up the following jest projects:
-
format
: Check code style with prettier usingjest-runner-prettier
. -
integration
: Tests that run out of theintegration-tests
directory. Functionally not much different thanunit
, but you can have a separate configuration, different pipeline dependencies, etc. -
lint
: Run eslint checks usingjest-runner-eslint
. -
unit
: Need I say more?
Running jest
will run all four projects. You can run a subset of the
projects using jest's --selectProjects
argument, as usual.
If you need to, you can extend or wrap the configs generated here by calling the config you want and passing an object as an argument that describes your modifications. The object can have any combination of the following keys:
-
format
: Specify extensions or wrappers for theformat
project. -
global
: Specify extensions or wrappers for the top-level jest config. -
integration
: Specify extensions or wrappers for theintegration
project. -
lint
: Specify extensions or wrappers for thelint
project. -
unit
: Specify extensions or wrappers for theunit
project.
The value for each of those keys should be an object containing either or
both of config
, an object whose values will be spread into the relevant
project or global config, and wrapper
, a function that should take the
config and return a promise containing the final config. For example:
import { base } from "@cprussin/jest-config";
export default base({
global: {
wrapper: async (oldGlobalConfig) => doSomethingAsyncWith(oldGlobalConfig),
config: {
passWithNoTests: true,
},
},
unit: {
wrapper: async (oldUnitConfig) => doSomethingAsyncWith(oldUnitConfig),
},
format: {
config: {
testPathIgnorePatterns: ["foo/*"],
},
},
});
Note that the extensions will be spread before calling the wrapper (so the
values in the config
option will be present in the argument to the
wrapper
).
Ƭ ExtraConfig<T
>: Object
This is the type for values in the optional object that can be passed to each config exported by this package.
Name | Type | Description |
---|---|---|
T |
extends Config.InitialOptions | Config.InitialProjectOptions
|
either InitialOptions (for the global key) or InitialProjectOptions (for all other keys) from jest |
Name | Type | Description |
---|---|---|
config? |
T |
Additional values to spread into the default config. |
wrapper? |
(config : T ) => Promise <T > |
If provided, this wrapper function will be called with the default config to generate the final config. |
packages/jest-config/src/index.ts:118
Ƭ ExtraConfigs: Object
This is the type of the optional argument for each of the exported configurations.
Name | Type | Description |
---|---|---|
format? |
ExtraConfig <Config.InitialProjectOptions > |
Extra configurations and an an optional wrapper to apply to the format project config. |
global? |
ExtraConfig <Config.InitialOptions > |
Extra configurations and an an optional wrapper to apply to the global config. |
integration? |
ExtraConfig <Config.InitialProjectOptions > |
Extra configurations and an an optional wrapper to apply to the integration project config. |
lint? |
ExtraConfig <Config.InitialProjectOptions > |
Extra configurations and an an optional wrapper to apply to the lint project config. |
unit? |
ExtraConfig <Config.InitialProjectOptions > |
Extra configurations and an an optional wrapper to apply to the unit project config. |
packages/jest-config/src/index.ts:142
▸ base(extra?
): Promise
<Partial
<{ automock
: boolean
; bail
: number
| boolean
; cache
: boolean
; cacheDirectory
: string
; changedFilesWithAncestor
: boolean
; changedSince
: string
; ci
: boolean
; clearMocks
: boolean
; collectCoverage
: boolean
; collectCoverageFrom
: string
[] ; coverageDirectory
: string
; coveragePathIgnorePatterns
: string
[] ; coverageProvider
: CoverageProvider
; coverageReporters
: CoverageReporters
; coverageThreshold
: CoverageThreshold
; dependencyExtractor
: string
; detectLeaks
: boolean
; detectOpenHandles
: boolean
; displayName
: string
| DisplayName
; errorOnDeprecated
: boolean
; expand
: boolean
; extensionsToTreatAsEsm
: string
[] ; fakeTimers
: FakeTimers
; filter
: string
; findRelatedTests
: boolean
; forceCoverageMatch
: string
[] ; forceExit
: boolean
; globalSetup
: undefined
| null
| string
; globalTeardown
: undefined
| null
| string
; globals
: ConfigGlobals
; haste
: HasteConfig
; id
: string
; injectGlobals
: boolean
; json
: boolean
; lastCommit
: boolean
; listTests
: boolean
; logHeapUsage
: boolean
; maxConcurrency
: number
; maxWorkers
: string
| number
; moduleDirectories
: string
[] ; moduleFileExtensions
: string
[] ; moduleNameMapper
: { [key: string]
: string
| string
[]; } ; modulePathIgnorePatterns
: string
[] ; modulePaths
: string
[] ; noStackTrace
: boolean
; notify
: boolean
; notifyMode
: string
; onlyChanged
: boolean
; onlyFailures
: boolean
; openHandlesTimeout
: number
; outputFile
: string
; passWithNoTests
: boolean
; preset
: undefined
| null
| string
; prettierPath
: undefined
| null
| string
; projects
: (string
| InitialProjectOptions
)[] ; randomize
: boolean
; replname
: undefined
| null
| string
; reporters
: (string
| ReporterConfig
)[] ; resetMocks
: boolean
; resetModules
: boolean
; resolver
: undefined
| null
| string
; restoreMocks
: boolean
; rootDir
: string
; roots
: string
[] ; runTestsByPath
: boolean
; runner
: string
; runtime
: string
; sandboxInjectedGlobals
: string
[] ; setupFiles
: string
[] ; setupFilesAfterEnv
: string
[] ; showSeed
: boolean
; silent
: boolean
; skipFilter
: boolean
; skipNodeResolution
: boolean
; slowTestThreshold
: number
; snapshotFormat
: { callToJSON?
: boolean
; compareKeys?
: null
; escapeRegex?
: boolean
; escapeString?
: boolean
; highlight?
: boolean
; indent?
: number
; maxDepth?
: number
; maxWidth?
: number
; min?
: boolean
; printBasicPrototype?
: boolean
; printFunctionName?
: boolean
; theme?
: { comment?
: string
; content?
: string
; prop?
: string
; tag?
: string
; value?
: string
} } ; snapshotResolver
: string
; snapshotSerializers
: string
[] ; testEnvironment
: string
; testEnvironmentOptions
: Record
<string
, unknown
> ; testFailureExitCode
: string
| number
; testLocationInResults
: boolean
; testMatch
: string
[] ; testNamePattern
: string
; testPathIgnorePatterns
: string
[] ; testRegex
: string
| string
[] ; testResultsProcessor
: string
; testRunner
: string
; testSequencer
: string
; testTimeout
: number
; transform
: { [regex: string]
: string
| TransformerConfig
; } ; transformIgnorePatterns
: string
[] ; unmockedModulePathPatterns
: string
[] ; updateSnapshot
: boolean
; useStderr
: boolean
; verbose?
: boolean
; watch
: boolean
; watchAll
: boolean
; watchPathIgnorePatterns
: string
[] ; watchPlugins
: (string
| [string
, Record
<string
, unknown
>])[] ; watchman
: boolean
; workerIdleMemoryLimit
: string
| number
; workerThreads
: boolean
}>>
The base config which is recommended in most cases that don't need DOM
testing. Sets up the standard projects and uses ts-jest
to run unit tests.
Name | Type | Description |
---|---|---|
extra |
ExtraConfigs |
extensions and wrappers for the projects and global jest config |
Promise
<Partial
<{ automock
: boolean
; bail
: number
| boolean
; cache
: boolean
; cacheDirectory
: string
; changedFilesWithAncestor
: boolean
; changedSince
: string
; ci
: boolean
; clearMocks
: boolean
; collectCoverage
: boolean
; collectCoverageFrom
: string
[] ; coverageDirectory
: string
; coveragePathIgnorePatterns
: string
[] ; coverageProvider
: CoverageProvider
; coverageReporters
: CoverageReporters
; coverageThreshold
: CoverageThreshold
; dependencyExtractor
: string
; detectLeaks
: boolean
; detectOpenHandles
: boolean
; displayName
: string
| DisplayName
; errorOnDeprecated
: boolean
; expand
: boolean
; extensionsToTreatAsEsm
: string
[] ; fakeTimers
: FakeTimers
; filter
: string
; findRelatedTests
: boolean
; forceCoverageMatch
: string
[] ; forceExit
: boolean
; globalSetup
: undefined
| null
| string
; globalTeardown
: undefined
| null
| string
; globals
: ConfigGlobals
; haste
: HasteConfig
; id
: string
; injectGlobals
: boolean
; json
: boolean
; lastCommit
: boolean
; listTests
: boolean
; logHeapUsage
: boolean
; maxConcurrency
: number
; maxWorkers
: string
| number
; moduleDirectories
: string
[] ; moduleFileExtensions
: string
[] ; moduleNameMapper
: { [key: string]
: string
| string
[]; } ; modulePathIgnorePatterns
: string
[] ; modulePaths
: string
[] ; noStackTrace
: boolean
; notify
: boolean
; notifyMode
: string
; onlyChanged
: boolean
; onlyFailures
: boolean
; openHandlesTimeout
: number
; outputFile
: string
; passWithNoTests
: boolean
; preset
: undefined
| null
| string
; prettierPath
: undefined
| null
| string
; projects
: (string
| InitialProjectOptions
)[] ; randomize
: boolean
; replname
: undefined
| null
| string
; reporters
: (string
| ReporterConfig
)[] ; resetMocks
: boolean
; resetModules
: boolean
; resolver
: undefined
| null
| string
; restoreMocks
: boolean
; rootDir
: string
; roots
: string
[] ; runTestsByPath
: boolean
; runner
: string
; runtime
: string
; sandboxInjectedGlobals
: string
[] ; setupFiles
: string
[] ; setupFilesAfterEnv
: string
[] ; showSeed
: boolean
; silent
: boolean
; skipFilter
: boolean
; skipNodeResolution
: boolean
; slowTestThreshold
: number
; snapshotFormat
: { callToJSON?
: boolean
; compareKeys?
: null
; escapeRegex?
: boolean
; escapeString?
: boolean
; highlight?
: boolean
; indent?
: number
; maxDepth?
: number
; maxWidth?
: number
; min?
: boolean
; printBasicPrototype?
: boolean
; printFunctionName?
: boolean
; theme?
: { comment?
: string
; content?
: string
; prop?
: string
; tag?
: string
; value?
: string
} } ; snapshotResolver
: string
; snapshotSerializers
: string
[] ; testEnvironment
: string
; testEnvironmentOptions
: Record
<string
, unknown
> ; testFailureExitCode
: string
| number
; testLocationInResults
: boolean
; testMatch
: string
[] ; testNamePattern
: string
; testPathIgnorePatterns
: string
[] ; testRegex
: string
| string
[] ; testResultsProcessor
: string
; testRunner
: string
; testSequencer
: string
; testTimeout
: number
; transform
: { [regex: string]
: string
| TransformerConfig
; } ; transformIgnorePatterns
: string
[] ; unmockedModulePathPatterns
: string
[] ; updateSnapshot
: boolean
; useStderr
: boolean
; verbose?
: boolean
; watch
: boolean
; watchAll
: boolean
; watchPathIgnorePatterns
: string
[] ; watchPlugins
: (string
| [string
, Record
<string
, unknown
>])[] ; watchman
: boolean
; workerIdleMemoryLimit
: string
| number
; workerThreads
: boolean
}>>
the jest config
Example
// jest.config.js
export { base as default } from "@cprussin/jest-config";
Example
// jest.config.js
import { base } from "@cprussin/jest-config";
export default base({
global: {
config: {
passWithnoTests: true
}
}
})
packages/jest-config/src/index.ts:202
▸ nextjs(extra?
): Promise
<Partial
<{ automock
: boolean
; bail
: number
| boolean
; cache
: boolean
; cacheDirectory
: string
; changedFilesWithAncestor
: boolean
; changedSince
: string
; ci
: boolean
; clearMocks
: boolean
; collectCoverage
: boolean
; collectCoverageFrom
: string
[] ; coverageDirectory
: string
; coveragePathIgnorePatterns
: string
[] ; coverageProvider
: CoverageProvider
; coverageReporters
: CoverageReporters
; coverageThreshold
: CoverageThreshold
; dependencyExtractor
: string
; detectLeaks
: boolean
; detectOpenHandles
: boolean
; displayName
: string
| DisplayName
; errorOnDeprecated
: boolean
; expand
: boolean
; extensionsToTreatAsEsm
: string
[] ; fakeTimers
: FakeTimers
; filter
: string
; findRelatedTests
: boolean
; forceCoverageMatch
: string
[] ; forceExit
: boolean
; globalSetup
: undefined
| null
| string
; globalTeardown
: undefined
| null
| string
; globals
: ConfigGlobals
; haste
: HasteConfig
; id
: string
; injectGlobals
: boolean
; json
: boolean
; lastCommit
: boolean
; listTests
: boolean
; logHeapUsage
: boolean
; maxConcurrency
: number
; maxWorkers
: string
| number
; moduleDirectories
: string
[] ; moduleFileExtensions
: string
[] ; moduleNameMapper
: { [key: string]
: string
| string
[]; } ; modulePathIgnorePatterns
: string
[] ; modulePaths
: string
[] ; noStackTrace
: boolean
; notify
: boolean
; notifyMode
: string
; onlyChanged
: boolean
; onlyFailures
: boolean
; openHandlesTimeout
: number
; outputFile
: string
; passWithNoTests
: boolean
; preset
: undefined
| null
| string
; prettierPath
: undefined
| null
| string
; projects
: (string
| InitialProjectOptions
)[] ; randomize
: boolean
; replname
: undefined
| null
| string
; reporters
: (string
| ReporterConfig
)[] ; resetMocks
: boolean
; resetModules
: boolean
; resolver
: undefined
| null
| string
; restoreMocks
: boolean
; rootDir
: string
; roots
: string
[] ; runTestsByPath
: boolean
; runner
: string
; runtime
: string
; sandboxInjectedGlobals
: string
[] ; setupFiles
: string
[] ; setupFilesAfterEnv
: string
[] ; showSeed
: boolean
; silent
: boolean
; skipFilter
: boolean
; skipNodeResolution
: boolean
; slowTestThreshold
: number
; snapshotFormat
: { callToJSON?
: boolean
; compareKeys?
: null
; escapeRegex?
: boolean
; escapeString?
: boolean
; highlight?
: boolean
; indent?
: number
; maxDepth?
: number
; maxWidth?
: number
; min?
: boolean
; printBasicPrototype?
: boolean
; printFunctionName?
: boolean
; theme?
: { comment?
: string
; content?
: string
; prop?
: string
; tag?
: string
; value?
: string
} } ; snapshotResolver
: string
; snapshotSerializers
: string
[] ; testEnvironment
: string
; testEnvironmentOptions
: Record
<string
, unknown
> ; testFailureExitCode
: string
| number
; testLocationInResults
: boolean
; testMatch
: string
[] ; testNamePattern
: string
; testPathIgnorePatterns
: string
[] ; testRegex
: string
| string
[] ; testResultsProcessor
: string
; testRunner
: string
; testSequencer
: string
; testTimeout
: number
; transform
: { [regex: string]
: string
| TransformerConfig
; } ; transformIgnorePatterns
: string
[] ; unmockedModulePathPatterns
: string
[] ; updateSnapshot
: boolean
; useStderr
: boolean
; verbose?
: boolean
; watch
: boolean
; watchAll
: boolean
; watchPathIgnorePatterns
: string
[] ; watchPlugins
: (string
| [string
, Record
<string
, unknown
>])[] ; watchman
: boolean
; workerIdleMemoryLimit
: string
| number
; workerThreads
: boolean
}>>
This configuration adds to the base config by wrapping the unit test project
by calling next/jest
. It also adds the jest-environment-jsdom
test
environment, and sets up @testing-library/jest-dom
.
Name | Type | Description |
---|---|---|
extra |
ExtraConfigs |
extensions and wrappers for the projects and global jest config |
Promise
<Partial
<{ automock
: boolean
; bail
: number
| boolean
; cache
: boolean
; cacheDirectory
: string
; changedFilesWithAncestor
: boolean
; changedSince
: string
; ci
: boolean
; clearMocks
: boolean
; collectCoverage
: boolean
; collectCoverageFrom
: string
[] ; coverageDirectory
: string
; coveragePathIgnorePatterns
: string
[] ; coverageProvider
: CoverageProvider
; coverageReporters
: CoverageReporters
; coverageThreshold
: CoverageThreshold
; dependencyExtractor
: string
; detectLeaks
: boolean
; detectOpenHandles
: boolean
; displayName
: string
| DisplayName
; errorOnDeprecated
: boolean
; expand
: boolean
; extensionsToTreatAsEsm
: string
[] ; fakeTimers
: FakeTimers
; filter
: string
; findRelatedTests
: boolean
; forceCoverageMatch
: string
[] ; forceExit
: boolean
; globalSetup
: undefined
| null
| string
; globalTeardown
: undefined
| null
| string
; globals
: ConfigGlobals
; haste
: HasteConfig
; id
: string
; injectGlobals
: boolean
; json
: boolean
; lastCommit
: boolean
; listTests
: boolean
; logHeapUsage
: boolean
; maxConcurrency
: number
; maxWorkers
: string
| number
; moduleDirectories
: string
[] ; moduleFileExtensions
: string
[] ; moduleNameMapper
: { [key: string]
: string
| string
[]; } ; modulePathIgnorePatterns
: string
[] ; modulePaths
: string
[] ; noStackTrace
: boolean
; notify
: boolean
; notifyMode
: string
; onlyChanged
: boolean
; onlyFailures
: boolean
; openHandlesTimeout
: number
; outputFile
: string
; passWithNoTests
: boolean
; preset
: undefined
| null
| string
; prettierPath
: undefined
| null
| string
; projects
: (string
| InitialProjectOptions
)[] ; randomize
: boolean
; replname
: undefined
| null
| string
; reporters
: (string
| ReporterConfig
)[] ; resetMocks
: boolean
; resetModules
: boolean
; resolver
: undefined
| null
| string
; restoreMocks
: boolean
; rootDir
: string
; roots
: string
[] ; runTestsByPath
: boolean
; runner
: string
; runtime
: string
; sandboxInjectedGlobals
: string
[] ; setupFiles
: string
[] ; setupFilesAfterEnv
: string
[] ; showSeed
: boolean
; silent
: boolean
; skipFilter
: boolean
; skipNodeResolution
: boolean
; slowTestThreshold
: number
; snapshotFormat
: { callToJSON?
: boolean
; compareKeys?
: null
; escapeRegex?
: boolean
; escapeString?
: boolean
; highlight?
: boolean
; indent?
: number
; maxDepth?
: number
; maxWidth?
: number
; min?
: boolean
; printBasicPrototype?
: boolean
; printFunctionName?
: boolean
; theme?
: { comment?
: string
; content?
: string
; prop?
: string
; tag?
: string
; value?
: string
} } ; snapshotResolver
: string
; snapshotSerializers
: string
[] ; testEnvironment
: string
; testEnvironmentOptions
: Record
<string
, unknown
> ; testFailureExitCode
: string
| number
; testLocationInResults
: boolean
; testMatch
: string
[] ; testNamePattern
: string
; testPathIgnorePatterns
: string
[] ; testRegex
: string
| string
[] ; testResultsProcessor
: string
; testRunner
: string
; testSequencer
: string
; testTimeout
: number
; transform
: { [regex: string]
: string
| TransformerConfig
; } ; transformIgnorePatterns
: string
[] ; unmockedModulePathPatterns
: string
[] ; updateSnapshot
: boolean
; useStderr
: boolean
; verbose?
: boolean
; watch
: boolean
; watchAll
: boolean
; watchPathIgnorePatterns
: string
[] ; watchPlugins
: (string
| [string
, Record
<string
, unknown
>])[] ; watchman
: boolean
; workerIdleMemoryLimit
: string
| number
; workerThreads
: boolean
}>>
the jest config
Example
// jest.config.js
export { nextjs as default } from "@cprussin/jest-config";
Example
// jest.config.js
import { nextjs } from "@cprussin/jest-config";
export default nextjs({
global: {
config: {
passWithnoTests: true
}
}
})