-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
executor.spec.ts
183 lines (160 loc) · 5.36 KB
/
executor.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
import { Tree, readJson, readProjectConfiguration } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { executorGenerator } from './executor';
import { pluginGenerator } from '../plugin/plugin';
import { libraryGenerator } from '@nrwl/js';
describe('NxPlugin Executor Generator', () => {
let tree: Tree;
let projectName: string;
beforeEach(async () => {
projectName = 'my-plugin';
tree = createTreeWithEmptyWorkspace(2);
await pluginGenerator(tree, {
name: projectName,
} as any);
});
it('should generate files', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
unitTestRunner: 'jest',
includeHasher: false,
});
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/schema.d.ts')
).toBeTruthy();
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/schema.json')
).toBeTruthy();
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/executor.ts')
).toBeTruthy();
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/executor.spec.ts')
).toBeTruthy();
});
it('should update executors.json', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
description: 'my-executor description',
unitTestRunner: 'jest',
includeHasher: false,
});
const executorJson = readJson(tree, 'libs/my-plugin/executors.json');
expect(executorJson.executors['my-executor'].implementation).toEqual(
'./src/executors/my-executor/executor'
);
expect(executorJson.executors['my-executor'].schema).toEqual(
'./src/executors/my-executor/schema.json'
);
expect(executorJson.executors['my-executor'].description).toEqual(
'my-executor description'
);
});
it('should generate default description', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
unitTestRunner: 'jest',
includeHasher: false,
});
const executorsJson = readJson(tree, 'libs/my-plugin/executors.json');
expect(executorsJson.executors['my-executor'].description).toEqual(
'my-executor executor'
);
});
it('should generate custom description', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
description: 'my-executor custom description',
unitTestRunner: 'jest',
includeHasher: false,
});
const executorsJson = readJson(tree, 'libs/my-plugin/executors.json');
expect(executorsJson.executors['my-executor'].description).toEqual(
'my-executor custom description'
);
});
it('should create executors.json if it is not present', async () => {
await libraryGenerator(tree, {
name: 'test-js-lib',
buildable: true,
});
const libConfig = readProjectConfiguration(tree, 'test-js-lib');
await executorGenerator(tree, {
project: 'test-js-lib',
includeHasher: false,
name: 'test-executor',
unitTestRunner: 'jest',
});
expect(() => tree.exists(`${libConfig.root}/executors.json`)).not.toThrow();
expect(readJson(tree, `${libConfig.root}/package.json`).executors).toBe(
'executors.json'
);
});
describe('--unitTestRunner', () => {
describe('none', () => {
it('should not generate unit test files', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
description: 'my-executor description',
unitTestRunner: 'none',
includeHasher: true,
});
expect(
tree.exists(
'libs/my-plugin/src/executors/my-executor/executor.spec.ts'
)
).toBeFalsy();
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/hasher.spec.ts')
).toBeFalsy();
});
});
});
describe('--includeHasher', () => {
it('should generate hasher files', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
includeHasher: true,
unitTestRunner: 'jest',
});
expect(
tree.exists('libs/my-plugin/src/executors/my-executor/hasher.spec.ts')
).toBeTruthy();
expect(
tree
.read('libs/my-plugin/src/executors/my-executor/hasher.ts')
.toString()
).toMatchInlineSnapshot(`
"import { CustomHasher } from '@nrwl/devkit';
/**
* This is a boilerplate custom hasher that matches
* the default Nx hasher. If you need to extend the behavior,
* you can consume workspace details from the context.
*/
export const myExecutorHasher: CustomHasher = async (task, context) => {
return context.hasher.hashTaskWithDepsAndContext(task)
};
export default myExecutorHasher;
"
`);
});
it('should update executors.json', async () => {
await executorGenerator(tree, {
project: projectName,
name: 'my-executor',
includeHasher: true,
unitTestRunner: 'jest',
});
const executorsJson = readJson(tree, 'libs/my-plugin/executors.json');
expect(executorsJson.executors['my-executor'].hasher).toEqual(
'./src/executors/my-executor/hasher'
);
});
});
});