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