forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundling.test.ts
159 lines (143 loc) · 4.47 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
import { Code, Runtime } from '@aws-cdk/aws-lambda';
import { AssetHashType } from '@aws-cdk/core';
import { version as delayVersion } from 'delay/package.json';
import * as fs from 'fs';
import * as path from 'path';
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);
});
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', 'parcel build /asset-input/folder/entry.ts --target cdk-lambda --cache-dir /parcel-cache',
],
}),
});
// Correctly updates package.json
const call = writeFileSyncMock.mock.calls[0];
expect(call[0]).toMatch('package.json');
expect(JSON.parse(call[1])).toEqual(expect.objectContaining({
'cdk-lambda': '/asset-output/index.js',
'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 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',
'parcel build /asset-input/folder/entry.ts --target cdk-lambda && 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/),
]),
}),
});
});