-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vite-plugin-angular): add Vitest plugin to transform async/await …
…in Angular packages (#760)
- Loading branch information
1 parent
9bd6609
commit 8024c49
Showing
24 changed files
with
1,430 additions
and
374 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 |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
.prettierignore | ||
.docusaurus/ | ||
pnpm-lock.yaml | ||
/.nx/cache | ||
/.nx/cache | ||
.angular |
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,36 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts"], | ||
"extends": [ | ||
"plugin:@nx/angular", | ||
"plugin:@angular-eslint/template/process-inline-templates" | ||
], | ||
"rules": { | ||
"@angular-eslint/directive-selector": [ | ||
"error", | ||
{ | ||
"type": "attribute", | ||
"prefix": "lib", | ||
"style": "camelCase" | ||
} | ||
], | ||
"@angular-eslint/component-selector": [ | ||
"error", | ||
{ | ||
"type": "element", | ||
"prefix": "lib", | ||
"style": "kebab-case" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["*.html"], | ||
"extends": ["plugin:@nx/angular-template"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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,3 @@ | ||
# libs/card | ||
|
||
This library was generated with [Nx](https://nx.dev). |
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,21 @@ | ||
{ | ||
"name": "card", | ||
"$schema": "../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/card/src", | ||
"prefix": "lib", | ||
"tags": [], | ||
"projectType": "library", | ||
"targets": { | ||
"test": { | ||
"executor": "@nx/vite:test" | ||
}, | ||
"lint": { | ||
"executor": "@nx/eslint:lint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/card/**/*.ts", "libs/card/**/*.html"] | ||
} | ||
} | ||
}, | ||
"implicitDependencies": ["vite-plugin-angular"] | ||
} |
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 @@ | ||
export * from './lib/card/card.component'; |
18 changes: 18 additions & 0 deletions
18
libs/card/src/lib/card/__snapshots__/card.component.spec.ts.snap
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,18 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`CardComponent > should create the app 1`] = ` | ||
<lib-card> | ||
card-works | ||
<button | ||
_ngcontent-a-c1042141038="" | ||
color="primary" | ||
mat-button="" | ||
> | ||
Render card | ||
</button> | ||
<!--bindings={ | ||
"ng-reflect-ng-if": "false" | ||
}--> | ||
</lib-card> | ||
`; |
Empty file.
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,3 @@ | ||
{{ title }} | ||
<button mat-button color="primary" (click)="render = true">Render card</button> | ||
<mat-card *ngIf="render"> Some card </mat-card> |
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,52 @@ | ||
// @vitest-environment happy-dom | ||
|
||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { MatButtonHarness } from '@angular/material/button/testing'; | ||
import { MatCardHarness } from '@angular/material/card/testing'; | ||
|
||
import { CardComponent } from './card.component'; | ||
|
||
describe('CardComponent', () => { | ||
let loader: HarnessLoader; | ||
let fixture: ComponentFixture<CardComponent>; | ||
let component: CardComponent; | ||
|
||
beforeEach(() => | ||
TestBed.configureTestingModule({ | ||
imports: [CardComponent], | ||
}) | ||
); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(CardComponent); | ||
component = fixture.componentInstance; | ||
loader = TestbedHarnessEnvironment.loader(fixture); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should show a card once we click on the button', async () => { | ||
const button = await loader.getHarness(MatButtonHarness); | ||
|
||
const card = await loader.getHarnessOrNull(MatCardHarness); | ||
expect(card).toBeNull(); | ||
|
||
await button.click(); | ||
|
||
const cardAfterClick = await loader.getHarnessOrNull(MatCardHarness); | ||
expect(cardAfterClick).not.toBeNull(); | ||
}); | ||
|
||
it('should create the app', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it(`should have as title 'vitetest'`, () => { | ||
expect(component.title).toEqual('card-works'); | ||
}); | ||
|
||
it('should create the app', () => { | ||
expect(fixture).toMatchSnapshot(); | ||
}); | ||
}); |
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,15 @@ | ||
import { Component } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { MatCardModule } from '@angular/material/card'; | ||
|
||
@Component({ | ||
selector: 'lib-card', | ||
standalone: true, | ||
imports: [CommonModule, MatCardModule], | ||
templateUrl: './card.component.html', | ||
styleUrls: ['./card.component.css'], | ||
}) | ||
export class CardComponent { | ||
title = 'card-works'; | ||
render = false; | ||
} |
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,16 @@ | ||
import '@analogjs/vite-plugin-angular/setup-vitest'; | ||
import '@angular/compiler'; | ||
|
||
/** | ||
* Initialize TestBed for all tests inside of content | ||
*/ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting, | ||
} from '@angular/platform-browser-dynamic/testing'; | ||
|
||
TestBed.initTestEnvironment( | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting() | ||
); |
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,26 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2022", | ||
"useDefineForClassFields": false, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
} | ||
], | ||
"extends": "../../tsconfig.base.json", | ||
"angularCompilerOptions": { | ||
"enableI18nLegacyMessageIdFormat": false, | ||
"strictInjectionParameters": true, | ||
"strictInputAccessModifiers": true, | ||
"strictTemplates": true | ||
} | ||
} |
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,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"declaration": true, | ||
"declarationMap": true, | ||
"inlineSources": true, | ||
"types": [] | ||
}, | ||
"exclude": ["src/**/*.spec.ts", "vite.config.ts"], | ||
"include": ["src/**/*.ts"] | ||
} |
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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../../dist/out-tsc", | ||
"types": ["node", "vitest/globals"], | ||
"target": "es2016" | ||
}, | ||
"files": ["src/test-setup.ts"], | ||
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"] | ||
} |
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,23 @@ | ||
/// <reference types="vitest" /> | ||
|
||
import { defineConfig } from 'vite'; | ||
import angular from '@analogjs/vite-plugin-angular'; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig(({ mode }) => { | ||
return { | ||
plugins: [angular()], | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
setupFiles: ['src/test-setup.ts'], | ||
include: ['**/*.spec.ts'], | ||
cache: { | ||
dir: `../../node_modules/.vitest`, | ||
}, | ||
}, | ||
define: { | ||
'import.meta.vitest': mode !== 'production', | ||
}, | ||
}; | ||
}); |
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
34 changes: 34 additions & 0 deletions
34
packages/vite-plugin-angular/src/lib/angular-vitest-plugin.ts
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,34 @@ | ||
import { Plugin, transformWithEsbuild } from 'vite'; | ||
|
||
export function angularVitestPlugin(): Plugin { | ||
return { | ||
name: '@analogjs/vitest-angular-esm-plugin', | ||
apply: 'serve', | ||
enforce: 'post', | ||
config() { | ||
return { | ||
ssr: { | ||
noExternal: [/fesm2022/], | ||
}, | ||
}; | ||
}, | ||
async transform(_code, id) { | ||
if (/fesm2022/.test(id) && _code.includes('async (')) { | ||
const { code, map } = await transformWithEsbuild(_code, id, { | ||
loader: 'js', | ||
format: 'esm', | ||
target: 'es2016', | ||
sourcemap: true, | ||
sourcefile: id, | ||
}); | ||
|
||
return { | ||
code, | ||
map, | ||
}; | ||
} | ||
|
||
return undefined; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.