From e4ff0c0e19e5941e7e7db1da9b5c29e01d58ab58 Mon Sep 17 00:00:00 2001 From: Ahn Date: Fri, 12 Feb 2021 12:13:19 +0100 Subject: [PATCH] feat(presets): add type definition for `presets` entry point (#801) --- presets/index.d.ts | 7 ++ presets/index.js | 38 +++++----- .../__snapshots__/ng-jest-config.spec.ts.snap | 1 + .../__snapshots__/presets.spec.ts.snap | 73 +++++++++++++++++++ src/__tests__/presets.spec.ts | 11 +++ tsconfig.spec.json | 4 + 6 files changed, 113 insertions(+), 21 deletions(-) create mode 100644 presets/index.d.ts create mode 100644 src/__tests__/__snapshots__/presets.spec.ts.snap create mode 100644 src/__tests__/presets.spec.ts diff --git a/presets/index.d.ts b/presets/index.d.ts new file mode 100644 index 0000000000..fbc2c6aaca --- /dev/null +++ b/presets/index.d.ts @@ -0,0 +1,7 @@ +import type { TsJestPresets } from 'ts-jest/dist/types'; + +declare const _default: { + defaults: TsJestPresets; + defaultsESM: TsJestPresets; +}; +export = _default; diff --git a/presets/index.js b/presets/index.js index e645586955..5b01cb2e52 100644 --- a/presets/index.js +++ b/presets/index.js @@ -22,26 +22,22 @@ const basePreset = { } module.exports = { - get defaults() { - return basePreset; - }, - get defaultsESM() { - return { - ...basePreset, - extensionsToTreatAsEsm: ['.ts'], - globals: { - 'ts-jest': { - ...basePreset.globals['ts-jest'], - useESM: true, - }, - }, - moduleNameMapper: { - ...basePreset.moduleNameMapper, - 'tslib': '/node_modules/tslib/tslib.es6.js', + defaults: basePreset, + defaultsESM: { + ...basePreset, + extensionsToTreatAsEsm: ['.ts'], + globals: { + 'ts-jest': { + ...basePreset.globals['ts-jest'], + useESM: true, }, - transformIgnorePatterns: [ - 'node_modules/(?!tslib)', - ], - } - }, + }, + moduleNameMapper: { + ...basePreset.moduleNameMapper, + 'tslib': '/node_modules/tslib/tslib.es6.js', + }, + transformIgnorePatterns: [ + 'node_modules/(?!tslib)', + ], + } } diff --git a/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap b/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap index 2b68a82d95..a43aef74b0 100644 --- a/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap +++ b/src/__tests__/__snapshots__/ng-jest-config.spec.ts.snap @@ -2,6 +2,7 @@ exports[`NgJestConfig _resolveTsConfig should return config including Angular compiler config with tsconfig as a string from ts-jest option 1`] = ` Object { + "allowSyntheticDefaultImports": true, "annotationsAs": "decorators", "declaration": false, "enableIvy": true, diff --git a/src/__tests__/__snapshots__/presets.spec.ts.snap b/src/__tests__/__snapshots__/presets.spec.ts.snap new file mode 100644 index 0000000000..37c4e9e5e7 --- /dev/null +++ b/src/__tests__/__snapshots__/presets.spec.ts.snap @@ -0,0 +1,73 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Presets should return the correct ESM preset value: esm-preset 1`] = ` +Object { + "extensionsToTreatAsEsm": Array [ + ".ts", + ], + "globals": Object { + "ts-jest": Object { + "stringifyContentPathRegex": "\\\\.html$", + "tsconfig": "/tsconfig.spec.json", + "useESM": true, + }, + }, + "moduleFileExtensions": Array [ + "ts", + "html", + "js", + "json", + ], + "moduleNameMapper": Object { + "^app/(.*)$": "/src/app/$1", + "^assets/(.*)$": "/src/assets/$1", + "^environments/(.*)$": "/src/environments/$1", + "^src/(.*)$": "/src/$1", + "tslib": "/node_modules/tslib/tslib.es6.js", + }, + "snapshotSerializers": Array [ + "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": Object { + "^.+\\\\.(ts|js|html)$": "jest-preset-angular", + }, + "transformIgnorePatterns": Array [ + "node_modules/(?!tslib)", + ], +} +`; + +exports[`Presets should return the correct default preset value: default-preset 1`] = ` +Object { + "globals": Object { + "ts-jest": Object { + "stringifyContentPathRegex": "\\\\.html$", + "tsconfig": "/tsconfig.spec.json", + }, + }, + "moduleFileExtensions": Array [ + "ts", + "html", + "js", + "json", + ], + "moduleNameMapper": Object { + "^app/(.*)$": "/src/app/$1", + "^assets/(.*)$": "/src/assets/$1", + "^environments/(.*)$": "/src/environments/$1", + "^src/(.*)$": "/src/$1", + }, + "snapshotSerializers": Array [ + "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": Object { + "^.+\\\\.(ts|js|html)$": "jest-preset-angular", + }, +} +`; diff --git a/src/__tests__/presets.spec.ts b/src/__tests__/presets.spec.ts new file mode 100644 index 0000000000..546f22c0f1 --- /dev/null +++ b/src/__tests__/presets.spec.ts @@ -0,0 +1,11 @@ +import presets from '../../presets'; + +describe('Presets', () => { + test('should return the correct default preset value', () => { + expect(presets.defaults).toMatchSnapshot('default-preset'); + }); + + test('should return the correct ESM preset value', () => { + expect(presets.defaultsESM).toMatchSnapshot('esm-preset'); + }); +}); diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 71cce3ca8a..5b30143ad2 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -1,3 +1,7 @@ { "extends": "./tsconfig.json", + "compilerOptions": { + "allowSyntheticDefaultImports": true, + "esModuleInterop": true + } }