forked from nrwl/nx
/
convert-to-nx-project.spec.ts
229 lines (180 loc) · 6.09 KB
/
convert-to-nx-project.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as devkit from '@nrwl/devkit';
import {
ProjectConfiguration,
readJson,
readProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { getRelativeProjectJsonSchemaPath } from 'nx/src/generators/utils/project-configuration';
import { libraryGenerator } from '../library/library';
import convertToNxProject, {
SCHEMA_OPTIONS_ARE_MUTUALLY_EXCLUSIVE,
} from './convert-to-nx-project';
import { getProjectConfigurationPath } from './utils/get-project-configuration-path';
import enquirer = require('enquirer');
jest.mock('fs-extra', () => ({
...jest.requireActual<any>('fs-extra'),
readJsonSync: () => ({}),
}));
jest.mock('enquirer', () => ({
prompt: () => ({
project: 'lib',
}),
}));
describe('convert-to-nx-project', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('should throw if project && all are both specified', async () => {
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
const p = convertToNxProject(tree, { all: true, project: 'lib' });
await expect(p).rejects.toMatch(SCHEMA_OPTIONS_ARE_MUTUALLY_EXCLUSIVE);
});
it('should prompt for a project if neither project nor all are specified', async () => {
const spy = jest.spyOn(enquirer, 'prompt');
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
const p = await convertToNxProject(tree, {});
expect(spy).toHaveBeenCalledTimes(1);
});
it('should not prompt for a project if all is specified', async () => {
const spy = jest.spyOn(enquirer, 'prompt');
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
const p = await convertToNxProject(tree, { all: true });
expect(spy).toHaveBeenCalledTimes(0);
});
it('should extract single project configuration to project.json', async () => {
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
const config = readProjectConfiguration(tree, 'lib');
await convertToNxProject(tree, { project: 'lib' });
const newConfigFile = await readJson(
tree,
getProjectConfigurationPath(config)
);
expect(newConfigFile.$schema).toBe(
getRelativeProjectJsonSchemaPath(tree, config)
);
delete config.root;
delete newConfigFile.$schema;
expect(config).toEqual(newConfigFile);
});
it('should extract all project configurations to project.json', async () => {
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
await libraryGenerator(tree, {
name: 'lib2',
standaloneConfig: false,
});
const configs = ['lib', 'lib2'].map((x) =>
readProjectConfiguration(tree, x)
);
await convertToNxProject(tree, { all: true });
for (const config of configs) {
const newConfigFile = await readJson(
tree,
getProjectConfigurationPath(config)
);
expect(newConfigFile.$schema).toBe(
getRelativeProjectJsonSchemaPath(tree, config)
);
delete config.root;
delete newConfigFile.$schema;
expect(config).toEqual(newConfigFile);
}
});
it('should include tags in project.json', async () => {
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
tags: 'scope:test',
standaloneConfig: false,
});
const config = readProjectConfiguration(tree, 'lib');
await convertToNxProject(tree, { all: true });
const newConfigFile = await readJson<ProjectConfiguration>(
tree,
getProjectConfigurationPath(config)
);
expect(newConfigFile.tags).toEqual(['scope:test']);
});
it('should set workspace.json to point to the root directory', async () => {
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
const config = readProjectConfiguration(tree, 'lib');
await convertToNxProject(tree, { project: 'lib' });
const json = readJson(tree, 'workspace.json');
expect(json.projects.lib).toEqual(config.root);
});
it('should error in v1 schema with workspace.json', async () => {
const tree = createTreeWithEmptyV1Workspace();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
});
try {
await convertToNxProject(tree, { project: 'lib' });
} catch (ex) {
expect(ex).toBeDefined();
}
expect.assertions(1);
});
it('should format files by default', async () => {
jest.spyOn(devkit, 'formatFiles');
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
skipFormat: true,
});
await convertToNxProject(tree, { project: 'lib' });
expect(devkit.formatFiles).toHaveBeenCalledTimes(1);
});
it('should format files when passing skipFormat false', async () => {
jest.spyOn(devkit, 'formatFiles');
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
skipFormat: true,
});
await convertToNxProject(tree, { project: 'lib', skipFormat: false });
expect(devkit.formatFiles).toHaveBeenCalledTimes(1);
});
it('should not format files when passing skipFormat true ', async () => {
jest.spyOn(devkit, 'formatFiles');
const tree = createTreeWithWorkspaceFile();
await libraryGenerator(tree, {
name: 'lib',
standaloneConfig: false,
skipFormat: true,
});
convertToNxProject(tree, { project: 'lib', skipFormat: true });
expect(devkit.formatFiles).toHaveBeenCalledTimes(0);
});
});
function createTreeWithWorkspaceFile() {
const tree = createTreeWithEmptyV1Workspace();
tree.write('workspace.json', JSON.stringify({ version: 2, projects: {} }));
return tree;
}