Skip to content

Commit 80c95e8

Browse files
committed
feat: allow to customize testEnvironment via preset creator functions
1 parent e6d5545 commit 80c95e8

File tree

6 files changed

+124
-70
lines changed

6 files changed

+124
-70
lines changed

src/presets/__snapshots__/index.spec.ts.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`Jest presets should return jest config with CJS preset creator function with options 1`] = `
3+
exports[`Jest presets should return jest config with preset creator functions with options: for createCjsPreset 1`] = `
44
{
55
"moduleFileExtensions": [
66
"ts",
@@ -31,75 +31,75 @@ exports[`Jest presets should return jest config with CJS preset creator function
3131
}
3232
`;
3333

34-
exports[`Jest presets should return jest config with CJS preset creator function without options 1`] = `
34+
exports[`Jest presets should return jest config with preset creator functions with options: for createEsmPreset 1`] = `
3535
{
36+
"extensionsToTreatAsEsm": [
37+
".ts",
38+
],
3639
"moduleFileExtensions": [
3740
"ts",
3841
"html",
3942
"js",
4043
"json",
4144
"mjs",
4245
],
46+
"moduleNameMapper": {
47+
"tslib": "tslib/tslib.es6.js",
48+
},
4349
"snapshotSerializers": [
4450
"jest-preset-angular/build/serializers/html-comment",
4551
"jest-preset-angular/build/serializers/ng-snapshot",
4652
"jest-preset-angular/build/serializers/no-ng-attributes",
4753
],
4854
"testEnvironment": "jsdom",
4955
"transform": {
50-
"^.+\\.(ts|js|mjs|html|svg)$": [
56+
"^.+\\.(ts|js|html|svg)$": [
5157
"jest-preset-angular",
5258
{
59+
"diagnostics": false,
5360
"stringifyContentPathRegex": "\\.(html|svg)$",
5461
"tsconfig": "<rootDir>/tsconfig.spec.json",
62+
"useESM": true,
5563
},
5664
],
5765
},
5866
"transformIgnorePatterns": [
59-
"node_modules/(?!(.*\\.mjs$|@angular/common/locales/.*\\.js$))",
67+
"node_modules/(?!tslib)",
6068
],
6169
}
6270
`;
6371

64-
exports[`Jest presets should return jest config with ESM preset creator function with options 1`] = `
72+
exports[`Jest presets should return jest config with preset creator functions: for createCjsPreset 1`] = `
6573
{
66-
"extensionsToTreatAsEsm": [
67-
".ts",
68-
],
6974
"moduleFileExtensions": [
7075
"ts",
7176
"html",
7277
"js",
7378
"json",
7479
"mjs",
7580
],
76-
"moduleNameMapper": {
77-
"tslib": "tslib/tslib.es6.js",
78-
},
7981
"snapshotSerializers": [
8082
"jest-preset-angular/build/serializers/html-comment",
8183
"jest-preset-angular/build/serializers/ng-snapshot",
8284
"jest-preset-angular/build/serializers/no-ng-attributes",
8385
],
8486
"testEnvironment": "jsdom",
8587
"transform": {
86-
"^.+\\.(ts|js|html|svg)$": [
88+
"^.+\\.(ts|js|mjs|html|svg)$": [
8789
"jest-preset-angular",
8890
{
89-
"diagnostics": false,
9091
"stringifyContentPathRegex": "\\.(html|svg)$",
9192
"tsconfig": "<rootDir>/tsconfig.spec.json",
92-
"useESM": true,
9393
},
9494
],
9595
},
9696
"transformIgnorePatterns": [
97-
"node_modules/(?!tslib)",
97+
"node_modules/(?!(.*\\.mjs$|@angular/common/locales/.*\\.js$))",
9898
],
9999
}
100100
`;
101101

102-
exports[`Jest presets should return jest config with ESM preset creator function without options 1`] = `
102+
exports[`Jest presets should return jest config with preset creator functions: for createEsmPreset 1`] = `
103103
{
104104
"extensionsToTreatAsEsm": [
105105
".ts",

src/presets/create-cjs-preset.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import type { Config } from 'jest';
22
import type { TsJestTransformerOptions } from 'ts-jest';
33

4-
import { basePresetConfig } from './utils';
4+
import { basePresetConfig, type JSDOMEnvironment } from './utils';
55

66
type CjsPresetType = typeof basePresetConfig & Required<Pick<Config, 'transformIgnorePatterns' | 'transform'>>;
77

8-
type CjsPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;
8+
type CjsPresetOptionsType = {
9+
tsconfig?: TsJestTransformerOptions['tsconfig'];
10+
astTransformers?: TsJestTransformerOptions['astTransformers'];
11+
babelConfig?: TsJestTransformerOptions['babelConfig'];
12+
diagnostics?: TsJestTransformerOptions['diagnostics'];
13+
testEnvironment?: JSDOMEnvironment;
14+
};
915

1016
export const createCjsPreset = (options: CjsPresetOptionsType = {}): CjsPresetType => {
17+
const { testEnvironment, ...rest } = options;
18+
1119
return {
1220
...basePresetConfig,
21+
testEnvironment: testEnvironment ?? basePresetConfig.testEnvironment,
1322
transformIgnorePatterns: ['node_modules/(?!(.*\\.mjs$|@angular/common/locales/.*\\.js$))'],
1423
transform: {
1524
'^.+\\.(ts|js|mjs|html|svg)$': [
1625
'jest-preset-angular',
1726
{
1827
tsconfig: '<rootDir>/tsconfig.spec.json',
1928
stringifyContentPathRegex: '\\.(html|svg)$',
20-
...options,
29+
...rest,
2130
},
2231
],
2332
},

src/presets/create-esm-preset.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import type { Config } from 'jest';
22
import type { TsJestTransformerOptions } from 'ts-jest';
33

4-
import { basePresetConfig } from './utils';
4+
import { basePresetConfig, type JSDOMEnvironment } from './utils';
55

66
type EsmPresetType = typeof basePresetConfig &
77
Required<Pick<Config, 'extensionsToTreatAsEsm' | 'moduleNameMapper' | 'transformIgnorePatterns' | 'transform'>>;
88

9-
type EsmPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;
9+
type EsmPresetOptionsType = {
10+
tsconfig?: TsJestTransformerOptions['tsconfig'];
11+
astTransformers?: TsJestTransformerOptions['astTransformers'];
12+
babelConfig?: TsJestTransformerOptions['babelConfig'];
13+
diagnostics?: TsJestTransformerOptions['diagnostics'];
14+
testEnvironment?: JSDOMEnvironment;
15+
};
1016

1117
export const createEsmPreset = (options: EsmPresetOptionsType = {}): EsmPresetType => {
18+
const { testEnvironment, ...rest } = options;
19+
1220
return {
1321
...basePresetConfig,
22+
testEnvironment: testEnvironment ?? basePresetConfig.testEnvironment,
1423
extensionsToTreatAsEsm: ['.ts'],
1524
moduleNameMapper: {
1625
tslib: 'tslib/tslib.es6.js',
@@ -23,7 +32,7 @@ export const createEsmPreset = (options: EsmPresetOptionsType = {}): EsmPresetTy
2332
tsconfig: '<rootDir>/tsconfig.spec.json',
2433
stringifyContentPathRegex: '\\.(html|svg)$',
2534
useESM: true,
26-
...options,
35+
...rest,
2736
},
2837
],
2938
},

src/presets/index.spec.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
import presets, { createCjsPreset, createEsmPreset } from '../../presets';
22

33
describe('Jest presets', () => {
4-
it('should return jest config with CJS preset creator function without options', () => {
5-
expect(createCjsPreset()).toMatchSnapshot();
4+
it('should return jest config with preset creator functions', () => {
5+
expect(createCjsPreset()).toMatchSnapshot('for createCjsPreset');
6+
expect(createEsmPreset()).toMatchSnapshot('for createEsmPreset');
67
});
78

8-
it('should return jest config with CJS preset creator function with options', () => {
9+
it('should return jest config with preset creator functions with options', () => {
910
expect(
1011
createCjsPreset({
1112
diagnostics: false,
1213
}),
13-
).toMatchSnapshot();
14-
});
15-
16-
it('should return jest config with ESM preset creator function without options', () => {
17-
expect(createEsmPreset()).toMatchSnapshot();
18-
});
19-
20-
it('should return jest config with ESM preset creator function with options', () => {
14+
).toMatchSnapshot('for createCjsPreset');
2115
expect(
2216
createEsmPreset({
2317
diagnostics: false,
2418
}),
25-
).toMatchSnapshot();
19+
).toMatchSnapshot('for createEsmPreset');
2620
});
2721

2822
it('should allow default export', () => {
@@ -31,4 +25,25 @@ describe('Jest presets', () => {
3125
createEsmPreset,
3226
});
3327
});
28+
29+
it('should allow to customize testEnvironment with preset creator functions', () => {
30+
expect(
31+
createCjsPreset({
32+
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env',
33+
}),
34+
).toEqual(
35+
expect.objectContaining({
36+
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env',
37+
}),
38+
);
39+
expect(
40+
createEsmPreset({
41+
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env',
42+
}),
43+
).toEqual(
44+
expect.objectContaining({
45+
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env',
46+
}),
47+
);
48+
});
3449
});

src/presets/utils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import type { Config } from 'jest';
22

33
import snapshotSerializers from '../serializers';
44

5-
type BasePresetConfig = Required<Pick<Config, 'testEnvironment' | 'moduleFileExtensions' | 'snapshotSerializers'>>;
5+
export type JSDOMEnvironment = 'jsdom' | 'jest-preset-angular/environments/jest-jsdom-env';
6+
7+
type BasePresetConfig = {
8+
testEnvironment: JSDOMEnvironment;
9+
moduleFileExtensions: Config['moduleFileExtensions'];
10+
snapshotSerializers: Config['snapshotSerializers'];
11+
};
612

713
export const basePresetConfig: BasePresetConfig = {
814
testEnvironment: 'jsdom',

website/docs/getting-started/presets.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,32 @@ Create a configuration to process JavaScript/TypeScript/HTML/SVG files (`ts|js|m
3333
- `isolatedModules`: see more at [isolatedModules options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/isolatedModules)
3434
- `astTransformers`: see more at [astTransformers options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers)
3535
- `diagnostics`: see more at [diagnostics options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/diagnostics)
36+
- `testEnvironment`: either `jsdom` or `jest-preset-angular/environments/jest-jsdom-env`. Defaults to `jsdom`.
3637

37-
#### Returns
38+
#### Default returned value
3839

3940
An object contains Jest config:
4041

41-
```ts
42-
type CjsPresetTransformerOptions = {
43-
tsconfig: string;
44-
stringifyContentPathRegex: string;
45-
};
46-
47-
type CjsPresetType = {
48-
testEnvironment: string;
49-
moduleFileExtensions: Array<string>;
50-
snapshotSerializers: Array<string>;
51-
transformIgnorePatterns: Array<string>;
52-
transform: {
53-
'^.+\\.(ts|js|mjs|html|svg)$': ['jest-preset-angular', CjsPresetTransformerOptions];
54-
};
55-
};
42+
```json
43+
{
44+
"moduleFileExtensions": ["ts", "html", "js", "json", "mjs"],
45+
"snapshotSerializers": [
46+
"jest-preset-angular/build/serializers/html-comment",
47+
"jest-preset-angular/build/serializers/ng-snapshot",
48+
"jest-preset-angular/build/serializers/no-ng-attributes"
49+
],
50+
"testEnvironment": "jsdom",
51+
"transform": {
52+
"^.+\\.(ts|js|mjs|html|svg)$": [
53+
"jest-preset-angular",
54+
{
55+
"stringifyContentPathRegex": "\\.(html|svg)$",
56+
"tsconfig": "<rootDir>/tsconfig.spec.json"
57+
}
58+
]
59+
},
60+
"transformIgnorePatterns": ["node_modules/(?!(.*\\.mjs$|@angular/common/locales/.*\\.js$))"]
61+
}
5662
```
5763

5864
#### Example:
@@ -81,28 +87,37 @@ Create a configuration to process JavaScript/TypeScript/HTML/SVG files (`ts|js|h
8187
- `isolatedModules`: see more at [isolatedModules options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/isolatedModules)
8288
- `astTransformers`: see more at [astTransformers options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/astTransformers)
8389
- `diagnostics`: see more at [diagnostics options page](https://kulshekhar.github.io/ts-jest/docs/getting-started/options/diagnostics)
90+
- `testEnvironment`: either `jsdom` or `jest-preset-angular/environments/jest-jsdom-env`. Defaults to `jsdom`.
8491

85-
#### Returns
92+
#### Default returned value
8693

8794
An object contains Jest config:
8895

89-
```ts
90-
type EsmPresetTransformerOptions = {
91-
tsconfig: string;
92-
stringifyContentPathRegex: string;
93-
useEsm: true;
94-
};
95-
96-
type EsmPresetType = {
97-
testEnvironment: string;
98-
moduleFileExtensions: Array<string>;
99-
snapshotSerializers: Array<string>;
100-
extensionsToTreatAsEsm: Array<string>;
101-
transformIgnorePatterns: Array<string>;
102-
transform: {
103-
'^.+\\.(ts|js|html|svg)$': ['jest-preset-angular', EsmPresetTransformerOptions];
104-
};
105-
};
96+
```json
97+
{
98+
"extensionsToTreatAsEsm": [".ts"],
99+
"moduleFileExtensions": ["ts", "html", "js", "json", "mjs"],
100+
"moduleNameMapper": {
101+
"tslib": "tslib/tslib.es6.js"
102+
},
103+
"snapshotSerializers": [
104+
"jest-preset-angular/build/serializers/html-comment",
105+
"jest-preset-angular/build/serializers/ng-snapshot",
106+
"jest-preset-angular/build/serializers/no-ng-attributes"
107+
],
108+
"testEnvironment": "jsdom",
109+
"transform": {
110+
"^.+\\.(ts|js|html|svg)$": [
111+
"jest-preset-angular",
112+
{
113+
"stringifyContentPathRegex": "\\.(html|svg)$",
114+
"tsconfig": "<rootDir>/tsconfig.spec.json",
115+
"useESM": true
116+
}
117+
]
118+
},
119+
"transformIgnorePatterns": ["node_modules/(?!tslib)"]
120+
}
106121
```
107122

108123
#### Example:

0 commit comments

Comments
 (0)