|
1 | 1 | import { assertWorkspaceValidity } from './assert-workspace-validity';
|
| 2 | +import { output } from '../utils/output'; |
2 | 3 |
|
3 | 4 | describe('assertWorkspaceValidity', () => {
|
4 | 5 | let mockNxJson: any;
|
@@ -44,53 +45,85 @@ describe('assertWorkspaceValidity', () => {
|
44 | 45 | });
|
45 | 46 |
|
46 | 47 | it('should throw for a missing project in workspace.json', () => {
|
| 48 | + spyOn(output, 'error'); |
47 | 49 | delete mockWorkspaceJson.projects.app1;
|
48 |
| - try { |
49 |
| - assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
50 |
| - fail('Did not throw'); |
51 |
| - } catch (e) { |
52 |
| - expect(e.message).toContain('projects are missing in'); |
53 |
| - } |
| 50 | + |
| 51 | + const mockExit = jest |
| 52 | + .spyOn(process, 'exit') |
| 53 | + .mockImplementation(((code?: number) => {}) as any); |
| 54 | + assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
| 55 | + |
| 56 | + expect(output.error).toHaveBeenCalledWith({ |
| 57 | + title: 'Configuration Error', |
| 58 | + bodyLines: [ |
| 59 | + `workspace.json and nx.json are out of sync. The following projects are missing in workspace.json: app1`, |
| 60 | + ], |
| 61 | + }); |
| 62 | + expect(mockExit).toHaveBeenCalledWith(1); |
| 63 | + mockExit.mockRestore(); |
54 | 64 | });
|
55 | 65 |
|
56 | 66 | it('should throw for a missing project in nx.json', () => {
|
| 67 | + spyOn(output, 'error'); |
| 68 | + |
57 | 69 | delete mockNxJson.projects.app1;
|
58 |
| - try { |
59 |
| - assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
60 |
| - fail('Did not throw'); |
61 |
| - } catch (e) { |
62 |
| - expect(e.message).toContain('projects are missing in nx.json'); |
63 |
| - } |
| 70 | + |
| 71 | + const mockExit = jest |
| 72 | + .spyOn(process, 'exit') |
| 73 | + .mockImplementation(((code?: number) => {}) as any); |
| 74 | + assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
| 75 | + |
| 76 | + expect(mockExit).toHaveBeenCalledWith(1); |
| 77 | + expect(output.error).toHaveBeenCalledWith({ |
| 78 | + title: 'Configuration Error', |
| 79 | + bodyLines: [ |
| 80 | + `workspace.json and nx.json are out of sync. The following projects are missing in nx.json: app1`, |
| 81 | + ], |
| 82 | + }); |
| 83 | + mockExit.mockRestore(); |
64 | 84 | });
|
65 | 85 |
|
66 | 86 | it('should throw for an invalid top-level implicit dependency', () => {
|
| 87 | + spyOn(output, 'error'); |
67 | 88 | mockNxJson.implicitDependencies = {
|
68 | 89 | 'README.md': ['invalidproj'],
|
69 | 90 | };
|
70 |
| - try { |
71 |
| - assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
72 |
| - fail('Did not throw'); |
73 |
| - } catch (e) { |
74 |
| - expect(e.message).toContain( |
75 |
| - 'implicitDependencies specified in nx.json are invalid' |
76 |
| - ); |
77 |
| - expect(e.message).toContain(' README.md'); |
78 |
| - expect(e.message).toContain(' invalidproj'); |
79 |
| - } |
| 91 | + |
| 92 | + const mockExit = jest |
| 93 | + .spyOn(process, 'exit') |
| 94 | + .mockImplementation(((code?: number) => {}) as any); |
| 95 | + assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
| 96 | + |
| 97 | + expect(mockExit).toHaveBeenCalledWith(1); |
| 98 | + expect(output.error).toHaveBeenCalledWith({ |
| 99 | + title: 'Configuration Error', |
| 100 | + bodyLines: [ |
| 101 | + `The following implicitDependencies specified in nx.json are invalid: |
| 102 | + README.md |
| 103 | + invalidproj`, |
| 104 | + ], |
| 105 | + }); |
| 106 | + mockExit.mockRestore(); |
80 | 107 | });
|
81 | 108 |
|
82 | 109 | it('should throw for an invalid project-level implicit dependency', () => {
|
| 110 | + spyOn(output, 'error'); |
83 | 111 | mockNxJson.projects.app2.implicitDependencies = ['invalidproj'];
|
84 | 112 |
|
85 |
| - try { |
86 |
| - assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
87 |
| - fail('Did not throw'); |
88 |
| - } catch (e) { |
89 |
| - expect(e.message).toContain( |
90 |
| - 'implicitDependencies specified in nx.json are invalid' |
91 |
| - ); |
92 |
| - expect(e.message).toContain(' app2'); |
93 |
| - expect(e.message).toContain(' invalidproj'); |
94 |
| - } |
| 113 | + const mockExit = jest |
| 114 | + .spyOn(process, 'exit') |
| 115 | + .mockImplementation(((code?: number) => {}) as any); |
| 116 | + assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); |
| 117 | + |
| 118 | + expect(mockExit).toHaveBeenCalledWith(1); |
| 119 | + expect(output.error).toHaveBeenCalledWith({ |
| 120 | + title: 'Configuration Error', |
| 121 | + bodyLines: [ |
| 122 | + `The following implicitDependencies specified in nx.json are invalid: |
| 123 | + app2 |
| 124 | + invalidproj`, |
| 125 | + ], |
| 126 | + }); |
| 127 | + mockExit.mockRestore(); |
95 | 128 | });
|
96 | 129 | });
|
0 commit comments