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 14, 2019
1 parent e50d4c9 commit 7064a42
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 35 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
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 __tests__/CoreJS-es6-reflect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});
6 changes: 3 additions & 3 deletions __tests__/CoreJS-es7-reflect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

});
2 changes: 1 addition & 1 deletion __tests__/InlineFilesTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion __tests__/StripStylesTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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',
],
};
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
2 changes: 1 addition & 1 deletion setupJest.js → src/setupJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit 7064a42

Please sign in to comment.