-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(breaking): Implement astTransformer compatible with ts-jest 23.10 (
#204) Closes #195 and closes #201 ### Breaking Change This is a breaking change as Configuration Interface changes ([see ts-jest v23.10 docs](https://kulshekhar.github.io/ts-jest/user/config/#ts-jest-options)) This PR is built upon @huafu's #201, kudos to him for setting up everything and pointing out what was still to be done. Transformer and test are also heavily inspired by his examples in ts-jest. Also see the PR for more information. ### Implementation Details Like the Regex, all the transformer does is transform * `templateUrl: <PATH>` to `template: require(<PATH>)` * `styles: <ANYTHING>` to `styles: []` * `styleUrls <ANYTHING>` to `styleUrls: []` For more information see the comments in the [transformer file](https://github.com/wtho/jest-preset-angular/blob/55bcfdb993d1dc511611553995ff566d64ea6944/src/InlineHtmlStripStylesTransformer.ts) or #201. ### Edge Cases I had to make some decisions on how strict the astTransformer is, I decided for this, but I am open to discussion: **Works** ```ts import { Component as Cmp } from '@angular/core'; @cmp({ templateUrl: './some-path.html' }) class AngularComp { } ``` Caveat - this also gets transformed ```ts @SomeOtherDecorator({ templateUrl: './some-path.html' }) class SomeClass { } ``` **Does not work** ```ts const componentArgs = { templateUrl: './some-path.html' } @component(componentArgs) class AngularComp { } ``` ### Additional Notes * Run unit tests for the transformer with `npm test` * The transformer was written in TypeScript. A `prepare`-script for was added, which compiles it to JS before publishing and after `npm install`. * `preprocessor.js` and its unit tests were removed.
- Loading branch information
Showing
24 changed files
with
1,428 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
InlineHtmlStripStylesTransformer.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/example | ||
/__tests__ | ||
/src | ||
circle.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Code is inspired by | ||
* https://github.com/kulshekhar/ts-jest/blob/25e1c63dd3797793b0f46fa52fdee580b46f66ae/src/transformers/hoist-jest.spec.ts | ||
* | ||
*/ | ||
|
||
import * as tsc from 'typescript' | ||
import * as transformer from '../InlineHtmlStripStylesTransformer' | ||
|
||
const CODE_WITH_TEMPLATE_URL = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
templateUrl: './page.html' | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
const CODE_WITH_NON_RELATIVE_TEMPLATE_URL = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
templateUrl: 'page.html' | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
const CODE_WITH_STYLE_URLS = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
styleUrls: [ | ||
'./fancy-styles.css', | ||
'./basic-styles.scss' | ||
] | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
const CODE_WITH_STYLES = ` | ||
import { Component } from '@angular/core'; | ||
@Component({ | ||
styles: [ | ||
'body: { display: none }', | ||
'html: { background-color: red }' | ||
] | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
const CODE_WITH_ALL_DECORATOR_PROPERTIES = ` | ||
import { Component } from '@angular/core'; | ||
@SomeDecorator({ | ||
value: 'test' | ||
}) | ||
@Component({ | ||
templateUrl: './page.html', | ||
styleUrls: [ | ||
'./fancy-styles.css', | ||
'./basic-styles.scss' | ||
], | ||
styles: [ | ||
'body: { display: none }', | ||
'html: { background-color: red }' | ||
], | ||
unaffectedProperty: 'whatever' | ||
}) | ||
@SomeOtherDecorator({ | ||
prop: 'ok' | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
const CODE_WITH_CUSTOM_DECORATOR = ` | ||
import { Component as CustomDecoratorName } from '@angular/core'; | ||
@CustomDecoratorName({ | ||
templateUrl: './page.html' | ||
}) | ||
export class AngularComponent { | ||
} | ||
` | ||
|
||
|
||
|
||
const createFactory = () => { | ||
return transformer.factory({ compilerModule: tsc } as any) | ||
} | ||
const transpile = (source: string) => tsc.transpileModule(source, { transformers: { before: [createFactory()] } }) | ||
|
||
describe('inlining template and stripping styles', () => { | ||
it('should have correct signature', () => { | ||
expect(transformer.name).toBe('angular-component-inline-template-strip-styles') | ||
expect(typeof transformer.version).toBe('number') | ||
expect(transformer.version).toBeGreaterThan(0) | ||
expect(typeof transformer.factory).toBe('function') | ||
}) | ||
|
||
it('should strip styleUrl assignment', () => { | ||
const out = transpile(CODE_WITH_STYLE_URLS) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
|
||
it('should strip styles assignment', () => { | ||
const out = transpile(CODE_WITH_STYLES) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
|
||
it('should inline templateUrl assignment', () => { | ||
const out = transpile(CODE_WITH_TEMPLATE_URL) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
|
||
it('should inline non-relative templateUrl assignment and make it relative', () => { | ||
const out = transpile(CODE_WITH_NON_RELATIVE_TEMPLATE_URL) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle all transformable decorator assignments', () => { | ||
const out = transpile(CODE_WITH_ALL_DECORATOR_PROPERTIES) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle all decorator assignments in differently named decorators', () => { | ||
const out = transpile(CODE_WITH_CUSTOM_DECORATOR) | ||
|
||
expect(out.outputText).toMatchSnapshot() | ||
}) | ||
}) |
Oops, something went wrong.