From 7064a42175d95b0c553d48185f174b489957e3de Mon Sep 17 00:00:00 2001 From: Thomas Wirth Date: Fri, 13 Sep 2019 22:44:43 +0200 Subject: [PATCH] feat: restructure project with src/ and build/ To keep source files in a single place, most js files are now located in the src folder as well. The TypeScript compiler transpiles the ts files and copies the js files to the build folder. --- .gitignore | 1 + CHANGELOG.md | 5 ++++- README.md | 18 ++++++++++++------ __tests__/CoreJS-es6-reflect.test.ts | 6 +++--- __tests__/CoreJS-es7-reflect.test.ts | 6 +++--- __tests__/InlineFilesTransformer.test.ts | 2 +- __tests__/StripStylesTransformer.test.ts | 2 +- example/package.json | 6 +++--- index.js | 2 +- jest-preset.js | 10 +++++----- .../AngularNoNgAttributesSnapshotSerializer.js | 0 .../AngularSnapshotSerializer.js | 0 .../HTMLCommentSerializer.js | 0 src/InlineFilesTransformer.ts | 4 ++-- src/StripStylesTransformer.ts | 16 ++++++++++------ src/TransformUtils.ts | 2 +- setupJest.js => src/setupJest.ts | 2 +- tsconfig.json | 2 +- 18 files changed, 49 insertions(+), 35 deletions(-) rename AngularNoNgAttributesSnapshotSerializer.js => src/AngularNoNgAttributesSnapshotSerializer.js (100%) rename AngularSnapshotSerializer.js => src/AngularSnapshotSerializer.js (100%) rename HTMLCommentSerializer.js => src/HTMLCommentSerializer.js (100%) rename setupJest.js => src/setupJest.ts (97%) diff --git a/.gitignore b/.gitignore index c88ec1d91b..badf9a1b57 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules +/build/ InlineFilesTransformer.js StripStylesTransformer.js TransformUtils.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ddc4f9c06..2fc2e6afd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,11 @@ * (**BREAKING**): Refine ast-transformer behavior: only transform `styles`-assignments inside @Component ([#261](https://github.com/thymikee/jest-preset-angular/pull/261)) and TypeScript v2.9 `createStringLiteral` is polyfilled if an older version is used ([#272](https://github.com/thymikee/jest-preset-angular/issues/272)). +* (**BREAKING**): Restructure project with `src` and `build` folder ([#307](https://github.com/thymikee/jest-preset-angular/pull/307)). + #### Migration Guide -* If the `astTransformers` are referenced in a custom `jest` config, `[ 'jest-preset-angular/InlineFilesTransformer', 'jest-preset-angular/StripStylesTransformer']` have to be set instead. +* If the `astTransformers` are referenced in a custom `jest` config, `[ 'jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer']` have to be set instead. +* Serializers, transformers and `setupJest` have to be referenced from the `jest-preset-angular/build/`-folder in a custom config. Existing references have to be aligned. ### v7.1.0 diff --git a/README.md b/README.md index 7bc8a97e12..0f22d951b4 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,10 @@ module.exports = { 'ts-jest': { tsConfig: '/src/tsconfig.spec.json', stringifyContentPathRegex: '\\.html$', - astTransformers: [require.resolve('./InlineHtmlStripStylesTransformer')], + astTransformers: [ + require.resolve('./build/InlineFilesTransformer'), + require.resolve('./build/StripStylesTransformer') + ], }, }, transform: { @@ -69,8 +72,8 @@ module.exports = { }, transformIgnorePatterns: ['node_modules/(?!@ngrx)'], snapshotSerializers: [ - 'jest-preset-angular/AngularSnapshotSerializer.js', - 'jest-preset-angular/HTMLCommentSerializer.js', + 'jest-preset-angular/build/AngularSnapshotSerializer.js', + 'jest-preset-angular/build/HTMLCommentSerializer.js', ], }; ``` @@ -305,14 +308,17 @@ Override `globals` object in Jest config: "ts-jest": { "tsConfig": "/src/tsconfig.custom.json", "stringifyContentPathRegex": "\\.html$", - "astTransformers": ["jest-preset-angular/InlineHtmlStripStylesTransformer"], + "astTransformers": [ + "jest-preset-angular/build/InlineFilesTransformer", + "jest-preset-angular/build/StripStylesTransformer" + ], } } } } ``` -If you choose to overide `globals` in order to point at a specific tsconfig, you will need to add `"astTransformers": ["jest-preset-angular/InlineHtmlStripStylesTransformer"]` to the `globals.ts-jest` section too, otherwise you will get parse errors on any html templates. +If you choose to overide `globals` in order to point at a specific tsconfig, you will need to add the `astTransformers` to the `globals.ts-jest` section too, otherwise you will get parse errors on any html templates. ### Unexpected token [import|export|other] @@ -411,7 +417,7 @@ module.exports = function(api) { { "jest": { "transform": { - "^.+\\.(ts|html)$": "/node_modules/jest-preset-angular/preprocessor.js", + "^.+\\.(ts|html)$": "ts-jest", "^.+\\.js$": "babel-jest" }, } diff --git a/__tests__/CoreJS-es6-reflect.test.ts b/__tests__/CoreJS-es6-reflect.test.ts index 2cf22a9b1f..6b760f5e86 100644 --- a/__tests__/CoreJS-es6-reflect.test.ts +++ b/__tests__/CoreJS-es6-reflect.test.ts @@ -24,21 +24,21 @@ describe('importing ES6 reflect', () => { jest.mock('core-js/es6/reflect', () => { throw coreJs2Error }, { virtual: true }); jest.mock('core-js/es/reflect', () => { throw coreJs3Error }, { virtual: true }); - expect(() => require('../setupJest')).toThrow('core-js es6-reflect not found!'); + expect(() => require('../src/setupJest')).toThrow('core-js es6-reflect not found!'); }); it('should import from core-js@2', () => { jest.mock('core-js/es6/reflect', () => true, { virtual: true }); jest.mock('core-js/es/reflect', () => { throw coreJs3Error }, { virtual: true }); - expect(() => require('../setupJest')).not.toThrow(); + expect(() => require('../src/setupJest')).not.toThrow(); }); it('should import from core-js@3', () => { jest.mock('core-js/es6/reflect', () => { throw coreJs2Error }, { virtual: true }); jest.mock('core-js/es/reflect', () => true, { virtual: true }); - expect(() => require('../setupJest')).not.toThrow(); + expect(() => require('../src/setupJest')).not.toThrow(); }); }); diff --git a/__tests__/CoreJS-es7-reflect.test.ts b/__tests__/CoreJS-es7-reflect.test.ts index db24cfdc74..fb2491ded4 100644 --- a/__tests__/CoreJS-es7-reflect.test.ts +++ b/__tests__/CoreJS-es7-reflect.test.ts @@ -24,21 +24,21 @@ describe('importing ES7 reflect', () => { jest.mock('core-js/es7/reflect', () => { throw coreJs2Error }, { virtual: true }); jest.mock('core-js/proposals/reflect-metadata', () => { throw coreJs3Error }, { virtual: true }); - expect(() => require('../setupJest')).toThrow('core-js es7-reflect not found!'); + expect(() => require('../src/setupJest')).toThrow('core-js es7-reflect not found!'); }); it('should import from core-js@2', () => { jest.mock('core-js/es7/reflect', () => true, { virtual: true }); jest.mock('core-js/proposals/reflect-metadata', () => { throw coreJs3Error }, { virtual: true }); - expect(() => require('../setupJest')).not.toThrow(); + expect(() => require('../src/setupJest')).not.toThrow(); }); it('should import from core-js@3', () => { jest.mock('core-js/es7/reflect', () => { throw coreJs2Error }, { virtual: true }); jest.mock('core-js/proposals/reflect-metadata', () => true, { virtual: true }); - expect(() => require('../setupJest')).not.toThrow(); + expect(() => require('../src/setupJest')).not.toThrow(); }); }); diff --git a/__tests__/InlineFilesTransformer.test.ts b/__tests__/InlineFilesTransformer.test.ts index 2e45af7ba6..dabbaa6fb8 100644 --- a/__tests__/InlineFilesTransformer.test.ts +++ b/__tests__/InlineFilesTransformer.test.ts @@ -5,7 +5,7 @@ */ import * as tsc from 'typescript'; -import * as transformer from '../InlineFilesTransformer'; +import * as transformer from '../src/InlineFilesTransformer'; const CODE_WITH_TEMPLATE_URL = ` import { Component } from '@angular/core'; diff --git a/__tests__/StripStylesTransformer.test.ts b/__tests__/StripStylesTransformer.test.ts index 53d55ada6b..5ee1a32a84 100644 --- a/__tests__/StripStylesTransformer.test.ts +++ b/__tests__/StripStylesTransformer.test.ts @@ -5,7 +5,7 @@ */ import * as tsc from 'typescript'; -import * as transformer from '../StripStylesTransformer'; +import * as transformer from '../src/StripStylesTransformer'; const CODE_WITH_STYLES_AND_OTHER_ASSIGNMENTS = ` import { Component } from '@angular/core'; diff --git a/example/package.json b/example/package.json index 605c8fe006..9397d7576d 100644 --- a/example/package.json +++ b/example/package.json @@ -45,9 +45,9 @@ "jest": { "preset": "jest-preset-angular", "snapshotSerializers": [ - "jest-preset-angular/AngularNoNgAttributesSnapshotSerializer.js", - "jest-preset-angular/AngularSnapshotSerializer.js", - "jest-preset-angular/HTMLCommentSerializer.js" + "jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js", + "jest-preset-angular/build/AngularSnapshotSerializer.js", + "jest-preset-angular/build/HTMLCommentSerializer.js" ], "moduleNameMapper": { "\\.(jpg|jpeg|png)$": "/__mocks__/image.js", diff --git a/index.js b/index.js index b4b26e3e48..2376204c73 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./setupJest.js'); +module.exports = require('./build/setupJest.js'); diff --git a/jest-preset.js b/jest-preset.js index 777e808e99..dee450efa6 100644 --- a/jest-preset.js +++ b/jest-preset.js @@ -4,8 +4,8 @@ module.exports = { tsConfig: '/src/tsconfig.spec.json', stringifyContentPathRegex: '\\.html$', astTransformers: [ - require.resolve('./InlineFilesTransformer'), - require.resolve('./StripStylesTransformer'), + require.resolve('./build/InlineFilesTransformer'), + require.resolve('./build/StripStylesTransformer'), ], }, }, @@ -22,8 +22,8 @@ module.exports = { }, transformIgnorePatterns: ['node_modules/(?!@ngrx)'], snapshotSerializers: [ - // 'jest-preset-angular/AngularNoNgAttributesSnapshotSerializer.js', - 'jest-preset-angular/AngularSnapshotSerializer.js', - 'jest-preset-angular/HTMLCommentSerializer.js', + // 'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer.js', + 'jest-preset-angular/build/AngularSnapshotSerializer.js', + 'jest-preset-angular/build/HTMLCommentSerializer.js', ], }; diff --git a/AngularNoNgAttributesSnapshotSerializer.js b/src/AngularNoNgAttributesSnapshotSerializer.js similarity index 100% rename from AngularNoNgAttributesSnapshotSerializer.js rename to src/AngularNoNgAttributesSnapshotSerializer.js diff --git a/AngularSnapshotSerializer.js b/src/AngularSnapshotSerializer.js similarity index 100% rename from AngularSnapshotSerializer.js rename to src/AngularSnapshotSerializer.js diff --git a/HTMLCommentSerializer.js b/src/HTMLCommentSerializer.js similarity index 100% rename from HTMLCommentSerializer.js rename to src/HTMLCommentSerializer.js diff --git a/src/InlineFilesTransformer.ts b/src/InlineFilesTransformer.ts index 81aadc75c7..4534e1bcf3 100644 --- a/src/InlineFilesTransformer.ts +++ b/src/InlineFilesTransformer.ts @@ -86,8 +86,8 @@ export function factory(cs: ConfigSet) { ): node is PropertyAssignment { return ( ts.isPropertyAssignment(node) && - ts.isIdentifier(node.name) && - TRANSFORM_PROPS.includes(node.name.text) + ts.isIdentifier((node as PropertyAssignment).name) && + TRANSFORM_PROPS.includes(((node as PropertyAssignment).name as Identifier).text) ); } diff --git a/src/StripStylesTransformer.ts b/src/StripStylesTransformer.ts index 56830cef16..965ce50177 100644 --- a/src/StripStylesTransformer.ts +++ b/src/StripStylesTransformer.ts @@ -30,7 +30,9 @@ import { Visitor, Identifier, ClassDeclaration, - PropertyAssignment + PropertyAssignment, + CallExpression, + ObjectLiteralExpression } from 'typescript'; import { ConfigSet } from './TransformUtils'; @@ -84,11 +86,13 @@ export function factory(cs: ConfigSet) { return node.decorators .map(dec => dec.expression) .filter(ts.isCallExpression) - .filter(callExpr => ts.isCallExpression(callExpr) && ts.isIdentifier(callExpr.expression) && callExpr.expression.getText() === COMPONENT) - .reduce((acc, nxtCallExpr) => Array.prototype.concat.apply(acc, + .filter((callExpr: CallExpression) => + ts.isIdentifier(callExpr.expression) && (callExpr.expression as Identifier).getText() === COMPONENT + ) + .reduce((acc, nxtCallExpr: CallExpression) => Array.prototype.concat.apply(acc, nxtCallExpr.arguments .filter(ts.isObjectLiteralExpression) - .reduce((acc, nxtArg) => Array.prototype.concat.apply(acc, + .reduce((acc, nxtArg: ObjectLiteralExpression) => Array.prototype.concat.apply(acc, nxtArg.properties .filter(ts.isPropertyAssignment) .filter(propAss => @@ -105,7 +109,7 @@ export function factory(cs: ConfigSet) { * Clones the styles assignment and manipulates it. * @param node the property assignment to change */ - function transfromStylesAssignmentForJest(node: ClassDeclaration) { + function transformStylesAssignmentForJest(node: ClassDeclaration) { const mutableNode = ts.getMutableClone(node) const assignments = getInDecoratorPropertyAssignmentsToTransform(mutableNode) @@ -133,7 +137,7 @@ export function factory(cs: ConfigSet) { // this is an assignment which we want to transform if (isInDecoratorPropertyAssignmentToTransform(node)) { // get transformed node with changed properties - return transfromStylesAssignmentForJest(node); + return transformStylesAssignmentForJest(node); } else { // else look for assignments inside this node recursively return ts.visitEachChild(node, visitor, ctx); diff --git a/src/TransformUtils.ts b/src/TransformUtils.ts index 0ee7aa032f..be6b20a6cf 100644 --- a/src/TransformUtils.ts +++ b/src/TransformUtils.ts @@ -1,4 +1,4 @@ -import TS from 'typescript'; +import * as TS from 'typescript'; // replace original ts-jest ConfigSet with this simple interface, as it would require // jest-preset-angular to add several babel devDependencies to get the other types diff --git a/setupJest.js b/src/setupJest.ts similarity index 97% rename from setupJest.js rename to src/setupJest.ts index 21015390a4..7645385c26 100644 --- a/setupJest.js +++ b/src/setupJest.ts @@ -25,7 +25,7 @@ require('zone.js/dist/proxy.js'); require('zone.js/dist/sync-test'); require('zone.js/dist/async-test'); require('zone.js/dist/fake-async-test'); -require('./zone-patch'); +require('../zone-patch'); const getTestBed = require('@angular/core/testing').getTestBed; const BrowserDynamicTestingModule = require('@angular/platform-browser-dynamic/testing').BrowserDynamicTestingModule; diff --git a/tsconfig.json b/tsconfig.json index f43229ecd0..e23c9a3f78 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "es5", "module": "commonjs", - "outDir": ".", + "outDir": "./build", "strict": true, "lib": ["esnext"], "allowSyntheticDefaultImports": true,