Skip to content

Commit 552922e

Browse files
committed
feat: add preset creator functions
1 parent 3c8f978 commit 552922e

File tree

9 files changed

+355
-55
lines changed

9 files changed

+355
-55
lines changed

presets/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ declare const _default: {
1818
snapshotSerializers: string[];
1919
};
2020
defaultTransformerOptions: import('ts-jest').TsJestTransformerOptions;
21+
createCjsPreset: typeof import('../build/presets').createCjsPreset;
22+
createEsmPreset: typeof import('../build/presets').createEsmPreset;
2123
};
2224
export default _default;

presets/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ module.exports = {
44
defaults: ngJestPresets.defaultPreset,
55
defaultsESM: ngJestPresets.defaultEsmPreset,
66
defaultTransformerOptions: ngJestPresets.defaultTransformerOptions,
7+
createCjsPreset: ngJestPresets.createCjsPreset,
8+
createEsmPreset: ngJestPresets.createEsmPreset,
79
};

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

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ exports[`Jest presets should have the correct types which come from \`ts-jest\`
2121
snapshotSerializers: string[];
2222
};
2323
defaultTransformerOptions: import('ts-jest').TsJestTransformerOptions;
24+
createCjsPreset: typeof import('../build/presets').createCjsPreset;
25+
createEsmPreset: typeof import('../build/presets').createEsmPreset;
2426
};
2527
export default _default;
2628
"
2729
`;
2830

29-
exports[`Jest presets should return the correct jest config 1`] = `
31+
exports[`Jest presets should return jest config with CJS preset creator function with options 1`] = `
3032
{
3133
"moduleFileExtensions": [
3234
"ts",
@@ -45,6 +47,7 @@ exports[`Jest presets should return the correct jest config 1`] = `
4547
"^.+\\.(ts|js|mjs|html|svg)$": [
4648
"jest-preset-angular",
4749
{
50+
"diagnostics": false,
4851
"stringifyContentPathRegex": "\\.(html|svg)$",
4952
"tsconfig": "<rootDir>/tsconfig.spec.json",
5053
},
@@ -56,7 +59,142 @@ exports[`Jest presets should return the correct jest config 1`] = `
5659
}
5760
`;
5861

59-
exports[`Jest presets should return the correct jest config 2`] = `
62+
exports[`Jest presets should return jest config with CJS preset creator function without options 1`] = `
63+
{
64+
"moduleFileExtensions": [
65+
"ts",
66+
"html",
67+
"js",
68+
"json",
69+
"mjs",
70+
],
71+
"snapshotSerializers": [
72+
"jest-preset-angular/build/serializers/html-comment",
73+
"jest-preset-angular/build/serializers/ng-snapshot",
74+
"jest-preset-angular/build/serializers/no-ng-attributes",
75+
],
76+
"testEnvironment": "jsdom",
77+
"transform": {
78+
"^.+\\.(ts|js|mjs|html|svg)$": [
79+
"jest-preset-angular",
80+
{
81+
"stringifyContentPathRegex": "\\.(html|svg)$",
82+
"tsconfig": "<rootDir>/tsconfig.spec.json",
83+
},
84+
],
85+
},
86+
"transformIgnorePatterns": [
87+
"node_modules/(?!.*\\.mjs$)",
88+
],
89+
}
90+
`;
91+
92+
exports[`Jest presets should return jest config with ESM preset creator function with options 1`] = `
93+
{
94+
"extensionsToTreatAsEsm": [
95+
".ts",
96+
],
97+
"moduleFileExtensions": [
98+
"ts",
99+
"html",
100+
"js",
101+
"json",
102+
"mjs",
103+
],
104+
"moduleNameMapper": {
105+
"tslib": "tslib/tslib.es6.js",
106+
},
107+
"snapshotSerializers": [
108+
"jest-preset-angular/build/serializers/html-comment",
109+
"jest-preset-angular/build/serializers/ng-snapshot",
110+
"jest-preset-angular/build/serializers/no-ng-attributes",
111+
],
112+
"testEnvironment": "jsdom",
113+
"transform": {
114+
"^.+\\.(ts|js|html|svg)$": [
115+
"jest-preset-angular",
116+
{
117+
"diagnostics": false,
118+
"stringifyContentPathRegex": "\\.(html|svg)$",
119+
"tsconfig": "<rootDir>/tsconfig.spec.json",
120+
"useESM": true,
121+
},
122+
],
123+
},
124+
"transformIgnorePatterns": [
125+
"node_modules/(?!tslib)",
126+
],
127+
}
128+
`;
129+
130+
exports[`Jest presets should return jest config with ESM preset creator function without options 1`] = `
131+
{
132+
"extensionsToTreatAsEsm": [
133+
".ts",
134+
],
135+
"moduleFileExtensions": [
136+
"ts",
137+
"html",
138+
"js",
139+
"json",
140+
"mjs",
141+
],
142+
"moduleNameMapper": {
143+
"tslib": "tslib/tslib.es6.js",
144+
},
145+
"snapshotSerializers": [
146+
"jest-preset-angular/build/serializers/html-comment",
147+
"jest-preset-angular/build/serializers/ng-snapshot",
148+
"jest-preset-angular/build/serializers/no-ng-attributes",
149+
],
150+
"testEnvironment": "jsdom",
151+
"transform": {
152+
"^.+\\.(ts|js|html|svg)$": [
153+
"jest-preset-angular",
154+
{
155+
"stringifyContentPathRegex": "\\.(html|svg)$",
156+
"tsconfig": "<rootDir>/tsconfig.spec.json",
157+
"useESM": true,
158+
},
159+
],
160+
},
161+
"transformIgnorePatterns": [
162+
"node_modules/(?!tslib)",
163+
],
164+
}
165+
`;
166+
167+
exports[`Jest presets should return the correct jest config with legacy preset config 1`] = `
168+
{
169+
"moduleFileExtensions": [
170+
"ts",
171+
"html",
172+
"js",
173+
"json",
174+
"mjs",
175+
],
176+
"snapshotSerializers": [
177+
"jest-preset-angular/build/serializers/html-comment",
178+
"jest-preset-angular/build/serializers/ng-snapshot",
179+
"jest-preset-angular/build/serializers/no-ng-attributes",
180+
],
181+
"testEnvironment": "jsdom",
182+
"transform": {
183+
"^.+\\.(ts|js|mjs|html|svg)$": [
184+
"jest-preset-angular",
185+
{
186+
"stringifyContentPathRegex": "\\.(html|svg)$",
187+
"tsconfig": "<rootDir>/tsconfig.spec.json",
188+
},
189+
],
190+
},
191+
"transformIgnorePatterns": [
192+
"node_modules/(?!.*\\.mjs$)",
193+
],
194+
}
195+
`;
196+
197+
exports[`Jest presets should return the correct jest config with legacy preset config 2`] = `
60198
{
61199
"extensionsToTreatAsEsm": [
62200
".ts",

src/presets/create-cjs-preset.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Config } from 'jest';
2+
import type { TsJestTransformerOptions } from 'ts-jest';
3+
4+
import { basePresetConfig } from './utils';
5+
6+
type CjsPresetType = typeof basePresetConfig & Required<Pick<Config, 'transformIgnorePatterns' | 'transform'>>;
7+
8+
type CjsPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;
9+
10+
export const createCjsPreset = (options: CjsPresetOptionsType = {}): CjsPresetType => {
11+
return {
12+
...basePresetConfig,
13+
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
14+
transform: {
15+
'^.+\\.(ts|js|mjs|html|svg)$': [
16+
'jest-preset-angular',
17+
{
18+
tsconfig: '<rootDir>/tsconfig.spec.json',
19+
stringifyContentPathRegex: '\\.(html|svg)$',
20+
...options,
21+
},
22+
],
23+
},
24+
};
25+
};

src/presets/create-esm-preset.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Config } from 'jest';
2+
import type { TsJestTransformerOptions } from 'ts-jest';
3+
4+
import { basePresetConfig } from './utils';
5+
6+
type EsmPresetType = typeof basePresetConfig &
7+
Required<Pick<Config, 'extensionsToTreatAsEsm' | 'moduleNameMapper' | 'transformIgnorePatterns' | 'transform'>>;
8+
9+
type EsmPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;
10+
11+
export const createEsmPreset = (options: EsmPresetOptionsType = {}): EsmPresetType => {
12+
return {
13+
...basePresetConfig,
14+
extensionsToTreatAsEsm: ['.ts'],
15+
moduleNameMapper: {
16+
tslib: 'tslib/tslib.es6.js',
17+
},
18+
transformIgnorePatterns: ['node_modules/(?!tslib)'],
19+
transform: {
20+
'^.+\\.(ts|js|html|svg)$': [
21+
'jest-preset-angular',
22+
{
23+
tsconfig: '<rootDir>/tsconfig.spec.json',
24+
stringifyContentPathRegex: '\\.(html|svg)$',
25+
useESM: true,
26+
...options,
27+
},
28+
],
29+
},
30+
};
31+
};

src/presets/index.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
import fs from 'fs';
22
import path from 'path';
33

4-
import { defaultPreset, defaultEsmPreset } from './';
4+
import { defaultPreset, defaultEsmPreset, createCjsPreset, createEsmPreset } from './';
55

66
describe('Jest presets', () => {
7-
test.each([defaultPreset, defaultEsmPreset])('should return the correct jest config', (preset) => {
8-
expect(preset).toMatchSnapshot();
7+
test.each([defaultPreset, defaultEsmPreset])(
8+
'should return the correct jest config with legacy preset config',
9+
(preset) => {
10+
expect(preset).toMatchSnapshot();
11+
},
12+
);
13+
14+
it('should return jest config with CJS preset creator function without options', () => {
15+
expect(createCjsPreset()).toMatchSnapshot();
16+
});
17+
18+
it('should return jest config with CJS preset creator function with options', () => {
19+
expect(
20+
createCjsPreset({
21+
diagnostics: false,
22+
}),
23+
).toMatchSnapshot();
24+
});
25+
26+
it('should return jest config with ESM preset creator function without options', () => {
27+
expect(createEsmPreset()).toMatchSnapshot();
28+
});
29+
30+
it('should return jest config with ESM preset creator function with options', () => {
31+
expect(
32+
createEsmPreset({
33+
diagnostics: false,
34+
}),
35+
).toMatchSnapshot();
936
});
1037

1138
test('should have the correct types which come from `ts-jest`', () => {

src/presets/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ const defaultEsmPreset = {
4040
};
4141

4242
export { defaultPreset, defaultEsmPreset, defaultTransformerOptions };
43+
export { createCjsPreset } from './create-cjs-preset';
44+
export { createEsmPreset } from './create-esm-preset';

src/presets/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Config } from 'jest';
2+
3+
import snapshotSerializers from '../serializers';
4+
5+
type BasePresetConfig = Required<Pick<Config, 'testEnvironment' | 'moduleFileExtensions' | 'snapshotSerializers'>>;
6+
7+
export const basePresetConfig: BasePresetConfig = {
8+
testEnvironment: 'jsdom',
9+
moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
10+
snapshotSerializers,
11+
};

0 commit comments

Comments
 (0)