From aa23c9d3ee685e65d4ba340634d0f06d68ffea7d 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 + .npmignore | 1 - CHANGELOG.md | 5 ++++- README.md | 18 ++++++++++++------ example/package.json | 6 +++--- index.js | 2 +- jest-preset.js | 10 +++++----- package.json | 2 +- .../AngularNoNgAttributesSnapshotSerializer.js | 0 .../AngularSnapshotSerializer.js | 0 {__tests__ => src}/CoreJS-es6-reflect.test.ts | 6 +++--- {__tests__ => src}/CoreJS-es7-reflect.test.ts | 6 +++--- .../HTMLCommentSerializer.js | 0 .../InlineFilesTransformer.test.ts | 2 +- src/InlineFilesTransformer.ts | 4 ++-- .../StripStylesTransformer.test.ts | 2 +- src/StripStylesTransformer.ts | 16 ++++++++++------ src/TransformUtils.ts | 2 +- .../InlineFilesTransformer.test.ts.snap | 0 .../StripStylesTransformer.test.ts.snap | 0 setupJest.js => src/setupJest.ts | 0 {zone-patch => src/zone-patch}/LICENSE | 0 {zone-patch => src/zone-patch}/README.md | 0 {zone-patch => src/zone-patch}/index.js | 0 tsconfig.json | 4 ++-- .../tsconfig.spec.json => tsconfig.spec.json | 0 26 files changed, 50 insertions(+), 37 deletions(-) rename AngularNoNgAttributesSnapshotSerializer.js => src/AngularNoNgAttributesSnapshotSerializer.js (100%) rename AngularSnapshotSerializer.js => src/AngularSnapshotSerializer.js (100%) rename {__tests__ => src}/CoreJS-es6-reflect.test.ts (90%) rename {__tests__ => src}/CoreJS-es7-reflect.test.ts (90%) rename HTMLCommentSerializer.js => src/HTMLCommentSerializer.js (100%) rename {__tests__ => src}/InlineFilesTransformer.test.ts (98%) rename {__tests__ => src}/StripStylesTransformer.test.ts (96%) rename {__tests__ => src}/__snapshots__/InlineFilesTransformer.test.ts.snap (100%) rename {__tests__ => src}/__snapshots__/StripStylesTransformer.test.ts.snap (100%) rename setupJest.js => src/setupJest.ts (100%) rename {zone-patch => src/zone-patch}/LICENSE (100%) rename {zone-patch => src/zone-patch}/README.md (100%) rename {zone-patch => src/zone-patch}/index.js (100%) rename __tests__/tsconfig.spec.json => tsconfig.spec.json (100%) 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/.npmignore b/.npmignore index 7f3b9ec41f..7dac93e035 100644 --- a/.npmignore +++ b/.npmignore @@ -1,4 +1,3 @@ /example -/__tests__ /src circle.yml 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/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/package.json b/package.json index 810505d4f2..44a5d7a5e6 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "tsConfig": "/tsconfig.spec.json" } }, - "rootDir": "__tests__" + "testPathIgnorePatterns": ["/node_modules/", "/example/"] }, "scripts": { "build": "tsc", 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/__tests__/CoreJS-es6-reflect.test.ts b/src/CoreJS-es6-reflect.test.ts similarity index 90% rename from __tests__/CoreJS-es6-reflect.test.ts rename to src/CoreJS-es6-reflect.test.ts index 2cf22a9b1f..9d61885576 100644 --- a/__tests__/CoreJS-es6-reflect.test.ts +++ b/src/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('./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('./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('./setupJest')).not.toThrow(); }); }); diff --git a/__tests__/CoreJS-es7-reflect.test.ts b/src/CoreJS-es7-reflect.test.ts similarity index 90% rename from __tests__/CoreJS-es7-reflect.test.ts rename to src/CoreJS-es7-reflect.test.ts index db24cfdc74..66407aaee2 100644 --- a/__tests__/CoreJS-es7-reflect.test.ts +++ b/src/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('./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('./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('./setupJest')).not.toThrow(); }); }); diff --git a/HTMLCommentSerializer.js b/src/HTMLCommentSerializer.js similarity index 100% rename from HTMLCommentSerializer.js rename to src/HTMLCommentSerializer.js diff --git a/__tests__/InlineFilesTransformer.test.ts b/src/InlineFilesTransformer.test.ts similarity index 98% rename from __tests__/InlineFilesTransformer.test.ts rename to src/InlineFilesTransformer.test.ts index 2e45af7ba6..9b162c0ad1 100644 --- a/__tests__/InlineFilesTransformer.test.ts +++ b/src/InlineFilesTransformer.test.ts @@ -5,7 +5,7 @@ */ import * as tsc from 'typescript'; -import * as transformer from '../InlineFilesTransformer'; +import * as transformer from './InlineFilesTransformer'; const CODE_WITH_TEMPLATE_URL = ` import { Component } from '@angular/core'; 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/__tests__/StripStylesTransformer.test.ts b/src/StripStylesTransformer.test.ts similarity index 96% rename from __tests__/StripStylesTransformer.test.ts rename to src/StripStylesTransformer.test.ts index 53d55ada6b..f602a1d5f1 100644 --- a/__tests__/StripStylesTransformer.test.ts +++ b/src/StripStylesTransformer.test.ts @@ -5,7 +5,7 @@ */ import * as tsc from 'typescript'; -import * as transformer from '../StripStylesTransformer'; +import * as transformer from './StripStylesTransformer'; const CODE_WITH_STYLES_AND_OTHER_ASSIGNMENTS = ` import { Component } from '@angular/core'; 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/__tests__/__snapshots__/InlineFilesTransformer.test.ts.snap b/src/__snapshots__/InlineFilesTransformer.test.ts.snap similarity index 100% rename from __tests__/__snapshots__/InlineFilesTransformer.test.ts.snap rename to src/__snapshots__/InlineFilesTransformer.test.ts.snap diff --git a/__tests__/__snapshots__/StripStylesTransformer.test.ts.snap b/src/__snapshots__/StripStylesTransformer.test.ts.snap similarity index 100% rename from __tests__/__snapshots__/StripStylesTransformer.test.ts.snap rename to src/__snapshots__/StripStylesTransformer.test.ts.snap diff --git a/setupJest.js b/src/setupJest.ts similarity index 100% rename from setupJest.js rename to src/setupJest.ts diff --git a/zone-patch/LICENSE b/src/zone-patch/LICENSE similarity index 100% rename from zone-patch/LICENSE rename to src/zone-patch/LICENSE diff --git a/zone-patch/README.md b/src/zone-patch/README.md similarity index 100% rename from zone-patch/README.md rename to src/zone-patch/README.md diff --git a/zone-patch/index.js b/src/zone-patch/index.js similarity index 100% rename from zone-patch/index.js rename to src/zone-patch/index.js diff --git a/tsconfig.json b/tsconfig.json index f43229ecd0..c304baa1c5 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, @@ -11,5 +11,5 @@ "types": ["node"] }, "include": ["src"], - "exclude": [] + "exclude": ["**/*.test.ts"] } \ No newline at end of file diff --git a/__tests__/tsconfig.spec.json b/tsconfig.spec.json similarity index 100% rename from __tests__/tsconfig.spec.json rename to tsconfig.spec.json