/
convert-nx-executor.spec.ts
106 lines (94 loc) · 3.18 KB
/
convert-nx-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
// When plugins from root nx.json load through ts-jest, they can cause transpile errors such as `@nx/playwright/plugin.d.ts` containing an unexpected "export" keyword.
// Mock `loadNxPlugins` function to prevent them from loading.
jest.mock('nx/src/utils/nx-plugin', () => ({
loadNxPlugins: () => Promise.resolve([]),
}));
import { convertNxExecutor } from './convert-nx-executor';
describe('Convert Nx Executor', () => {
it('should convertNxExecutor to builder correctly and produce the same output', async () => {
// ARRANGE
const { schema } = require('@angular-devkit/core');
const {
TestingArchitectHost,
} = require('@angular-devkit/architect/testing');
const { Architect } = require('@angular-devkit/architect');
const registry = new schema.CoreSchemaRegistry();
registry.addPostTransform(schema.transforms.addUndefinedDefaults);
const testArchitectHost = new TestingArchitectHost();
const architect = new Architect(testArchitectHost, registry);
const convertedExecutor = convertNxExecutor(echoExecutor);
const realBuilder = require('@angular-devkit/architect').createBuilder(
echo
);
testArchitectHost.addBuilder('nx:test', convertedExecutor);
testArchitectHost.addBuilder('ng:test', realBuilder);
const consoleSpy = jest.spyOn(console, 'log');
// ACT
const convertedRun = await architect.scheduleBuilder('nx:test', {
name: 'test',
});
const realRun = await architect.scheduleBuilder('ng:test', {
name: 'test',
});
const convertedRunResult = await convertedRun.result;
const realRunResult = await realRun.result;
// ASSERT
expect(convertedRunResult).toMatchInlineSnapshot(`
{
"error": undefined,
"info": {
"builderName": "nx:test",
"description": "Testing only builder.",
"optionSchema": {
"type": "object",
},
},
"success": true,
"target": {
"configuration": undefined,
"project": undefined,
"target": undefined,
},
}
`);
expect(realRunResult).toMatchInlineSnapshot(`
{
"error": undefined,
"info": {
"builderName": "ng:test",
"description": "Testing only builder.",
"optionSchema": {
"type": "object",
},
},
"success": true,
"target": {
"configuration": undefined,
"project": undefined,
"target": undefined,
},
}
`);
expect(convertedRunResult.success).toEqual(realRunResult.success);
expect(consoleSpy).toHaveBeenCalledTimes(2);
expect(consoleSpy).toHaveBeenNthCalledWith(1, 'Executor ran', {
name: 'test',
});
expect(consoleSpy).toHaveBeenNthCalledWith(2, 'Executor ran', {
name: 'test',
});
expect(convertedExecutor.toString()).toEqual(realBuilder.toString());
expect(convertedExecutor.handler.toString()).toEqual(
realBuilder.handler.toString()
);
});
});
function echo(options: { name: string }) {
console.log('Executor ran', options);
return {
success: true,
};
}
async function echoExecutor(options: { name: string }) {
return echo(options);
}