Skip to content

Commit

Permalink
feat: restructure project with src/ and build/
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wtho committed Sep 17, 2019
1 parent e50d4c9 commit d60bc4f
Show file tree
Hide file tree
Showing 26 changed files with 44 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
/build/
InlineFilesTransformer.js
StripStylesTransformer.js
TransformUtils.js
Expand Down
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/example
/__tests__
/src
circle.yml
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ module.exports = {
'ts-jest': {
tsConfig: '<rootDir>/src/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [require.resolve('./InlineHtmlStripStylesTransformer')],
astTransformers: [
require.resolve('./build/InlineFilesTransformer'),
require.resolve('./build/StripStylesTransformer')
],
},
},
transform: {
Expand All @@ -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',
],
};
```
Expand Down Expand Up @@ -305,14 +308,17 @@ Override `globals` object in Jest config:
"ts-jest": {
"tsConfig": "<rootDir>/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]

Expand Down Expand Up @@ -411,7 +417,7 @@ module.exports = function(api) {
{
"jest": {
"transform": {
"^.+\\.(ts|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js",
"^.+\\.(ts|html)$": "ts-jest",
"^.+\\.js$": "babel-jest"
},
}
Expand Down
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)$": "<rootDir>/__mocks__/image.js",
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./setupJest.js');
module.exports = require('./build/setupJest.js');
10 changes: 5 additions & 5 deletions jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module.exports = {
tsConfig: '<rootDir>/src/tsconfig.spec.json',
stringifyContentPathRegex: '\\.html$',
astTransformers: [
require.resolve('./InlineFilesTransformer'),
require.resolve('./StripStylesTransformer'),
require.resolve('./build/InlineFilesTransformer'),
require.resolve('./build/StripStylesTransformer'),
],
},
},
Expand All @@ -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',
],
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"tsConfig": "<rootDir>/tsconfig.spec.json"
}
},
"rootDir": "__tests__"
"rootDir": "src/__tests__"
},
"scripts": {
"build": "tsc",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/InlineFilesTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down
16 changes: 10 additions & 6 deletions src/StripStylesTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import {
Visitor,
Identifier,
ClassDeclaration,
PropertyAssignment
PropertyAssignment,
CallExpression,
ObjectLiteralExpression
} from 'typescript';
import { ConfigSet } from './TransformUtils';

Expand Down Expand Up @@ -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 =>
Expand All @@ -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)

Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/TransformUtils.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('importing ES6 reflect', () => {
jest.mock('zone.js/dist/sync-test', () => true, { virtual: true });
jest.mock('zone.js/dist/async-test', () => true, { virtual: true });
jest.mock('zone.js/dist/fake-async-test', () => true, { virtual: true });
jest.mock('../zone-patch', () => true, { virtual: true });
jest.mock('./zone-patch', () => true, { virtual: true });

jest.mock('core-js/es7/reflect', () => true, { virtual: true });
jest.mock('core-js/proposals/reflect-metadata', () => true, { virtual: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('importing ES7 reflect', () => {
jest.mock('zone.js/dist/sync-test', () => true, { virtual: true });
jest.mock('zone.js/dist/async-test', () => true, { virtual: true });
jest.mock('zone.js/dist/fake-async-test', () => true, { virtual: true });
jest.mock('../zone-patch', () => true, { virtual: true });
jest.mock('./zone-patch', () => true, { virtual: true });

jest.mock('core-js/es6/reflect', () => true, { virtual: true });
jest.mock('core-js/es/reflect', () => true, { virtual: true });
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": ".",
"outDir": "./build",
"strict": true,
"lib": ["esnext"],
"allowSyntheticDefaultImports": true,
Expand All @@ -11,5 +11,5 @@
"types": ["node"]
},
"include": ["src"],
"exclude": []
"exclude": ["**/__tests__"]
}

0 comments on commit d60bc4f

Please sign in to comment.