-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
bundling.test.ts
196 lines (176 loc) · 5.78 KB
/
bundling.test.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
import * as fs from 'fs';
import * as path from 'path';
import { Code, Runtime } from '@aws-cdk/aws-lambda';
import { AssetHashType, BundlingDockerImage } from '@aws-cdk/core';
import { version as delayVersion } from 'delay/package.json';
import { Bundling } from '../lib/bundling';
import * as util from '../lib/util';
jest.mock('@aws-cdk/aws-lambda');
const writeFileSyncMock = jest.spyOn(fs, 'writeFileSync').mockReturnValue();
const existsSyncOriginal = fs.existsSync;
const existsSyncMock = jest.spyOn(fs, 'existsSync');
const originalFindUp = util.findUp;
const findUpMock = jest.spyOn(util, 'findUp').mockImplementation((name: string, directory) => {
if (name === 'package.json') {
return path.join(__dirname, '..');
}
return originalFindUp(name, directory);
});
const fromAssetMock = jest.spyOn(BundlingDockerImage, 'fromAsset');
beforeEach(() => {
jest.clearAllMocks();
});
test('Parcel bundling', () => {
Bundling.parcel({
entry: '/project/folder/entry.ts',
runtime: Runtime.NODEJS_12_X,
cacheDir: '/cache-dir',
projectRoot: '/project',
parcelEnvironment: {
KEY: 'value',
},
});
// Correctly bundles with parcel
expect(Code.fromAsset).toHaveBeenCalledWith('/project', {
assetHashType: AssetHashType.BUNDLE,
bundling: expect.objectContaining({
environment: {
KEY: 'value',
},
volumes: [{ containerPath: '/parcel-cache', hostPath: '/cache-dir' }],
workingDirectory: '/asset-input/folder',
command: [
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist --cache-dir /parcel-cache && mv /asset-output/entry.js /asset-output/index.js',
],
}),
});
// Correctly updates package.json
const call = writeFileSyncMock.mock.calls[0];
expect(call[0]).toMatch('package.json');
expect(JSON.parse(call[1])).toEqual(expect.objectContaining({
targets: {
'cdk-lambda': {
context: 'node',
includeNodeModules: {
'aws-sdk': false,
},
sourceMap: false,
minify: false,
engines: {
node: '>= 12',
},
},
},
}));
// Searches for the package.json starting in the directory of the entry file
expect(findUpMock).toHaveBeenCalledWith('package.json', '/project/folder');
});
test('Parcel bundling with handler named index.ts', () => {
Bundling.parcel({
entry: '/project/folder/index.ts',
runtime: Runtime.NODEJS_12_X,
projectRoot: '/project',
});
// Correctly bundles with parcel
expect(Code.fromAsset).toHaveBeenCalledWith('/project', {
assetHashType: AssetHashType.BUNDLE,
bundling: expect.objectContaining({
command: [
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/index.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist',
],
}),
});
});
test('Parcel with Windows paths', () => {
Bundling.parcel({
entry: 'C:\\my-project\\lib\\entry.ts',
runtime: Runtime.NODEJS_12_X,
cacheDir: '/cache-dir',
projectRoot: 'C:\\my-project',
});
expect(Code.fromAsset).toHaveBeenCalledWith('C:\\my-project', expect.objectContaining({
bundling: expect.objectContaining({
command: expect.arrayContaining([
expect.stringContaining('/lib/entry.ts'),
]),
}),
}));
});
test('Parcel bundling with externals and dependencies', () => {
Bundling.parcel({
entry: '/project/folder/entry.ts',
runtime: Runtime.NODEJS_12_X,
projectRoot: '/project',
externalModules: ['abc'],
nodeModules: ['delay'],
});
// Correctly bundles with parcel
expect(Code.fromAsset).toHaveBeenCalledWith('/project', {
assetHashType: AssetHashType.BUNDLE,
bundling: expect.objectContaining({
command: [
'bash', '-c',
'$(node -p "require.resolve(\'parcel\')") build /asset-input/folder/entry.ts --target cdk-lambda --dist-dir /asset-output --no-autoinstall --no-scope-hoist && mv /asset-output/entry.js /asset-output/index.js && mv /asset-input/.package.json /asset-output/package.json && cd /asset-output && npm install',
],
}),
});
// Correctly updates package.json
const call = writeFileSyncMock.mock.calls[0];
expect(call[0]).toMatch('package.json');
expect(JSON.parse(call[1])).toEqual(expect.objectContaining({
targets: expect.objectContaining({
'cdk-lambda': expect.objectContaining({
includeNodeModules: {
delay: false,
abc: false,
},
}),
}),
}));
// Correctly writes dummy package.json
expect(writeFileSyncMock).toHaveBeenCalledWith('/project/.package.json', JSON.stringify({
dependencies: {
delay: delayVersion,
},
}));
});
test('Detects yarn.lock', () => {
existsSyncMock.mockImplementation((p: fs.PathLike) => {
if (/yarn.lock/.test(p.toString())) {
return true;
}
return existsSyncOriginal(p);
});
Bundling.parcel({
entry: '/project/folder/entry.ts',
runtime: Runtime.NODEJS_12_X,
projectRoot: '/project',
nodeModules: ['delay'],
});
// Correctly bundles with parcel
expect(Code.fromAsset).toHaveBeenCalledWith('/project', {
assetHashType: AssetHashType.BUNDLE,
bundling: expect.objectContaining({
command: expect.arrayContaining([
expect.stringMatching(/yarn\.lock.+yarn install/),
]),
}),
});
});
test('with build args', () => {
Bundling.parcel({
entry: '/project/folder/entry.ts',
runtime: Runtime.NODEJS_12_X,
projectRoot: '/project',
buildArgs: {
HELLO: 'WORLD',
},
});
expect(fromAssetMock).toHaveBeenCalledWith(expect.stringMatching(/parcel$/), expect.objectContaining({
buildArgs: expect.objectContaining({
HELLO: 'WORLD',
}),
}));
});