|
| 1 | +import { writeFile } from '../../../utils/fs'; |
| 2 | +import { ng } from '../../../utils/process'; |
| 3 | +import { updateJsonFile } from '../../../utils/project'; |
| 4 | + |
| 5 | +export default async function () { |
| 6 | + await ng('generate', 'library', 'my-lib'); |
| 7 | + |
| 8 | + // Enable partial compilation mode (linker) for the library |
| 9 | + await updateJsonFile('projects/my-lib/tsconfig.lib.json', config => { |
| 10 | + const { angularCompilerOptions = {} } = config; |
| 11 | + angularCompilerOptions.compilationMode = 'partial'; |
| 12 | + config.angularCompilerOptions = angularCompilerOptions; |
| 13 | + }); |
| 14 | + // Enable ivy for production as well |
| 15 | + await updateJsonFile('projects/my-lib/tsconfig.lib.prod.json', config => { |
| 16 | + const { angularCompilerOptions = {} } = config; |
| 17 | + angularCompilerOptions.enableIvy = true; |
| 18 | + config.angularCompilerOptions = angularCompilerOptions; |
| 19 | + }); |
| 20 | + |
| 21 | + await writeFile('./src/app/app.module.ts', ` |
| 22 | + import { BrowserModule } from '@angular/platform-browser'; |
| 23 | + import { NgModule } from '@angular/core'; |
| 24 | + import { MyLibModule } from 'my-lib'; |
| 25 | +
|
| 26 | + import { AppComponent } from './app.component'; |
| 27 | +
|
| 28 | + @NgModule({ |
| 29 | + declarations: [ |
| 30 | + AppComponent |
| 31 | + ], |
| 32 | + imports: [ |
| 33 | + BrowserModule, |
| 34 | + MyLibModule, |
| 35 | + ], |
| 36 | + providers: [], |
| 37 | + bootstrap: [AppComponent] |
| 38 | + }) |
| 39 | + export class AppModule { } |
| 40 | + `); |
| 41 | + |
| 42 | + await writeFile('./src/app/app.component.ts', ` |
| 43 | + import { Component } from '@angular/core'; |
| 44 | + import { MyLibService } from 'my-lib'; |
| 45 | +
|
| 46 | + @Component({ |
| 47 | + selector: 'app-root', |
| 48 | + template: '<lib-my-lib></lib-my-lib>' |
| 49 | + }) |
| 50 | + export class AppComponent { |
| 51 | + title = 'app'; |
| 52 | +
|
| 53 | + constructor(myLibService: MyLibService) { |
| 54 | + console.log(myLibService); |
| 55 | + } |
| 56 | + } |
| 57 | + `); |
| 58 | + |
| 59 | + await writeFile('e2e/src/app.e2e-spec.ts', ` |
| 60 | + import { browser, logging, element, by } from 'protractor'; |
| 61 | + import { AppPage } from './app.po'; |
| 62 | +
|
| 63 | + describe('workspace-project App', () => { |
| 64 | + let page: AppPage; |
| 65 | +
|
| 66 | + beforeEach(() => { |
| 67 | + page = new AppPage(); |
| 68 | + }); |
| 69 | +
|
| 70 | + it('should display text from library component', async () => { |
| 71 | + await page.navigateTo(); |
| 72 | + expect(await element(by.css('lib-my-lib p')).getText()).toEqual('my-lib works!'); |
| 73 | + }); |
| 74 | +
|
| 75 | + afterEach(async () => { |
| 76 | + // Assert that there are no errors emitted from the browser |
| 77 | + const logs = await browser.manage().logs().get(logging.Type.BROWSER); |
| 78 | + expect(logs).not.toContain(jasmine.objectContaining({ |
| 79 | + level: logging.Level.SEVERE, |
| 80 | + })); |
| 81 | + }); |
| 82 | + }); |
| 83 | + `); |
| 84 | + |
| 85 | + await runLibraryTests(); |
| 86 | + await runLibraryTests(true); |
| 87 | +} |
| 88 | + |
| 89 | +async function runLibraryTests(prodMode = false): Promise<void> { |
| 90 | + const args = ['build', 'my-lib']; |
| 91 | + if (prodMode) { |
| 92 | + args.push('--prod'); |
| 93 | + } |
| 94 | + |
| 95 | + await ng(...args); |
| 96 | + |
| 97 | + // Check that the tests succeeds both with named project, unnamed (should test app), and prod. |
| 98 | + await ng('e2e'); |
| 99 | + await ng('e2e', 'test-project', '--devServerTarget=test-project:serve:production'); |
| 100 | +} |
0 commit comments