|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import * as releaseConfig from '../config/index'; |
| 10 | +import {ReleaseBuildCommandModule} from './cli'; |
| 11 | +import * as index from './index'; |
| 12 | + |
| 13 | +describe('ng-dev release build', () => { |
| 14 | + let npmPackages: string[]; |
| 15 | + let buildPackages: jasmine.Spy; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + npmPackages = ['@angular/pkg1', '@angular/pkg2']; |
| 19 | + buildPackages = jasmine.createSpy('buildPackages').and.resolveTo([ |
| 20 | + {name: '@angular/pkg1', outputPath: 'dist/pkg1'}, |
| 21 | + {name: '@angular/pkg2', outputPath: 'dist/pkg2'}, |
| 22 | + ]); |
| 23 | + |
| 24 | + // We cannot test the worker process, so we fake the worker function and |
| 25 | + // directly call the package build function. |
| 26 | + spyOn(index, 'buildReleaseOutput').and.callFake(() => buildPackages()); |
| 27 | + // We need to stub out the `process.exit` function during tests as the CLI |
| 28 | + // handler calls those in case of failures. |
| 29 | + spyOn(process, 'exit'); |
| 30 | + }); |
| 31 | + |
| 32 | + /** Invokes the build command handler. */ |
| 33 | + async function invokeBuild({json}: {json?: boolean} = {}) { |
| 34 | + spyOn(releaseConfig, 'getReleaseConfig') |
| 35 | + .and.returnValue({npmPackages, buildPackages, generateReleaseNotesForHead: async () => {}}); |
| 36 | + await ReleaseBuildCommandModule.handler({json: !!json, $0: '', _: []}); |
| 37 | + } |
| 38 | + |
| 39 | + it('should invoke configured build packages function', async () => { |
| 40 | + await invokeBuild(); |
| 41 | + expect(buildPackages).toHaveBeenCalledTimes(1); |
| 42 | + expect(process.exit).toHaveBeenCalledTimes(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should print built packages as JSON if `--json` is specified', async () => { |
| 46 | + const writeSpy = spyOn(process.stdout, 'write'); |
| 47 | + await invokeBuild({json: true}); |
| 48 | + |
| 49 | + expect(buildPackages).toHaveBeenCalledTimes(1); |
| 50 | + expect(writeSpy).toHaveBeenCalledTimes(1); |
| 51 | + |
| 52 | + const jsonText = writeSpy.calls.mostRecent().args[0] as string; |
| 53 | + const parsed = JSON.parse(jsonText); |
| 54 | + |
| 55 | + expect(parsed).toEqual([ |
| 56 | + {name: '@angular/pkg1', outputPath: 'dist/pkg1'}, |
| 57 | + {name: '@angular/pkg2', outputPath: 'dist/pkg2'} |
| 58 | + ]); |
| 59 | + expect(process.exit).toHaveBeenCalledTimes(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should error if package has not been built', async () => { |
| 63 | + // Set up a NPM package that is not built. |
| 64 | + npmPackages.push('@angular/non-existent'); |
| 65 | + |
| 66 | + spyOn(console, 'error'); |
| 67 | + await invokeBuild(); |
| 68 | + |
| 69 | + expect(console.error).toHaveBeenCalledTimes(2); |
| 70 | + expect(console.error) |
| 71 | + .toHaveBeenCalledWith( |
| 72 | + jasmine.stringMatching(`Release output missing for the following packages`)); |
| 73 | + expect(console.error).toHaveBeenCalledWith(jasmine.stringMatching(`- @angular/non-existent`)); |
| 74 | + expect(process.exit).toHaveBeenCalledTimes(1); |
| 75 | + expect(process.exit).toHaveBeenCalledWith(1); |
| 76 | + }); |
| 77 | +}); |
0 commit comments