Skip to content

Commit

Permalink
feat: add preset creator functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Nov 27, 2024
1 parent 3c8f978 commit 552922e
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 55 deletions.
2 changes: 2 additions & 0 deletions presets/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ declare const _default: {
snapshotSerializers: string[];
};
defaultTransformerOptions: import('ts-jest').TsJestTransformerOptions;
createCjsPreset: typeof import('../build/presets').createCjsPreset;
createEsmPreset: typeof import('../build/presets').createEsmPreset;
};
export default _default;
2 changes: 2 additions & 0 deletions presets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ module.exports = {
defaults: ngJestPresets.defaultPreset,
defaultsESM: ngJestPresets.defaultEsmPreset,
defaultTransformerOptions: ngJestPresets.defaultTransformerOptions,
createCjsPreset: ngJestPresets.createCjsPreset,
createEsmPreset: ngJestPresets.createEsmPreset,
};
142 changes: 140 additions & 2 deletions src/presets/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ exports[`Jest presets should have the correct types which come from \`ts-jest\`
snapshotSerializers: string[];
};
defaultTransformerOptions: import('ts-jest').TsJestTransformerOptions;
createCjsPreset: typeof import('../build/presets').createCjsPreset;
createEsmPreset: typeof import('../build/presets').createEsmPreset;
};
export default _default;
"
`;

exports[`Jest presets should return the correct jest config 1`] = `
exports[`Jest presets should return jest config with CJS preset creator function with options 1`] = `
{
"moduleFileExtensions": [
"ts",
Expand All @@ -45,6 +47,7 @@ exports[`Jest presets should return the correct jest config 1`] = `
"^.+\\.(ts|js|mjs|html|svg)$": [
"jest-preset-angular",
{
"diagnostics": false,
"stringifyContentPathRegex": "\\.(html|svg)$",
"tsconfig": "<rootDir>/tsconfig.spec.json",
},
Expand All @@ -56,7 +59,142 @@ exports[`Jest presets should return the correct jest config 1`] = `
}
`;

exports[`Jest presets should return the correct jest config 2`] = `
exports[`Jest presets should return jest config with CJS preset creator function without options 1`] = `
{
"moduleFileExtensions": [
"ts",
"html",
"js",
"json",
"mjs",
],
"snapshotSerializers": [
"jest-preset-angular/build/serializers/html-comment",
"jest-preset-angular/build/serializers/ng-snapshot",
"jest-preset-angular/build/serializers/no-ng-attributes",
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(ts|js|mjs|html|svg)$": [
"jest-preset-angular",
{
"stringifyContentPathRegex": "\\.(html|svg)$",
"tsconfig": "<rootDir>/tsconfig.spec.json",
},
],
},
"transformIgnorePatterns": [
"node_modules/(?!.*\\.mjs$)",
],
}
`;

exports[`Jest presets should return jest config with ESM preset creator function with options 1`] = `
{
"extensionsToTreatAsEsm": [
".ts",
],
"moduleFileExtensions": [
"ts",
"html",
"js",
"json",
"mjs",
],
"moduleNameMapper": {
"tslib": "tslib/tslib.es6.js",
},
"snapshotSerializers": [
"jest-preset-angular/build/serializers/html-comment",
"jest-preset-angular/build/serializers/ng-snapshot",
"jest-preset-angular/build/serializers/no-ng-attributes",
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(ts|js|html|svg)$": [
"jest-preset-angular",
{
"diagnostics": false,
"stringifyContentPathRegex": "\\.(html|svg)$",
"tsconfig": "<rootDir>/tsconfig.spec.json",
"useESM": true,
},
],
},
"transformIgnorePatterns": [
"node_modules/(?!tslib)",
],
}
`;

exports[`Jest presets should return jest config with ESM preset creator function without options 1`] = `
{
"extensionsToTreatAsEsm": [
".ts",
],
"moduleFileExtensions": [
"ts",
"html",
"js",
"json",
"mjs",
],
"moduleNameMapper": {
"tslib": "tslib/tslib.es6.js",
},
"snapshotSerializers": [
"jest-preset-angular/build/serializers/html-comment",
"jest-preset-angular/build/serializers/ng-snapshot",
"jest-preset-angular/build/serializers/no-ng-attributes",
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(ts|js|html|svg)$": [
"jest-preset-angular",
{
"stringifyContentPathRegex": "\\.(html|svg)$",
"tsconfig": "<rootDir>/tsconfig.spec.json",
"useESM": true,
},
],
},
"transformIgnorePatterns": [
"node_modules/(?!tslib)",
],
}
`;

exports[`Jest presets should return the correct jest config with legacy preset config 1`] = `
{
"moduleFileExtensions": [
"ts",
"html",
"js",
"json",
"mjs",
],
"snapshotSerializers": [
"jest-preset-angular/build/serializers/html-comment",
"jest-preset-angular/build/serializers/ng-snapshot",
"jest-preset-angular/build/serializers/no-ng-attributes",
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(ts|js|mjs|html|svg)$": [
"jest-preset-angular",
{
"stringifyContentPathRegex": "\\.(html|svg)$",
"tsconfig": "<rootDir>/tsconfig.spec.json",
},
],
},
"transformIgnorePatterns": [
"node_modules/(?!.*\\.mjs$)",
],
}
`;

exports[`Jest presets should return the correct jest config with legacy preset config 2`] = `
{
"extensionsToTreatAsEsm": [
".ts",
Expand Down
25 changes: 25 additions & 0 deletions src/presets/create-cjs-preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Config } from 'jest';
import type { TsJestTransformerOptions } from 'ts-jest';

import { basePresetConfig } from './utils';

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

type CjsPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;

export const createCjsPreset = (options: CjsPresetOptionsType = {}): CjsPresetType => {
return {
...basePresetConfig,
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
transform: {
'^.+\\.(ts|js|mjs|html|svg)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
...options,
},
],
},
};
};
31 changes: 31 additions & 0 deletions src/presets/create-esm-preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Config } from 'jest';
import type { TsJestTransformerOptions } from 'ts-jest';

import { basePresetConfig } from './utils';

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

type EsmPresetOptionsType = Omit<TsJestTransformerOptions, 'useESM' | 'stringifyContentPathRegex' | 'compiler'>;

export const createEsmPreset = (options: EsmPresetOptionsType = {}): EsmPresetType => {
return {
...basePresetConfig,
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
tslib: 'tslib/tslib.es6.js',
},
transformIgnorePatterns: ['node_modules/(?!tslib)'],
transform: {
'^.+\\.(ts|js|html|svg)$': [
'jest-preset-angular',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
useESM: true,
...options,
},
],
},
};
};
33 changes: 30 additions & 3 deletions src/presets/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import fs from 'fs';
import path from 'path';

import { defaultPreset, defaultEsmPreset } from './';
import { defaultPreset, defaultEsmPreset, createCjsPreset, createEsmPreset } from './';

describe('Jest presets', () => {
test.each([defaultPreset, defaultEsmPreset])('should return the correct jest config', (preset) => {
expect(preset).toMatchSnapshot();
test.each([defaultPreset, defaultEsmPreset])(
'should return the correct jest config with legacy preset config',
(preset) => {
expect(preset).toMatchSnapshot();
},
);

it('should return jest config with CJS preset creator function without options', () => {
expect(createCjsPreset()).toMatchSnapshot();
});

it('should return jest config with CJS preset creator function with options', () => {
expect(
createCjsPreset({
diagnostics: false,
}),
).toMatchSnapshot();
});

it('should return jest config with ESM preset creator function without options', () => {
expect(createEsmPreset()).toMatchSnapshot();
});

it('should return jest config with ESM preset creator function with options', () => {
expect(
createEsmPreset({
diagnostics: false,
}),
).toMatchSnapshot();
});

test('should have the correct types which come from `ts-jest`', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ const defaultEsmPreset = {
};

export { defaultPreset, defaultEsmPreset, defaultTransformerOptions };
export { createCjsPreset } from './create-cjs-preset';
export { createEsmPreset } from './create-esm-preset';
11 changes: 11 additions & 0 deletions src/presets/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from 'jest';

import snapshotSerializers from '../serializers';

type BasePresetConfig = Required<Pick<Config, 'testEnvironment' | 'moduleFileExtensions' | 'snapshotSerializers'>>;

export const basePresetConfig: BasePresetConfig = {
testEnvironment: 'jsdom',
moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
snapshotSerializers,
};
Loading

0 comments on commit 552922e

Please sign in to comment.