forked from aws-amplify/amplify-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_amplify.test.ts
267 lines (238 loc) · 8.19 KB
/
create_amplify.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { execa } from 'execa';
import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
import { after, afterEach, before, beforeEach, describe, it } from 'node:test';
import assert from 'assert';
import { glob } from 'glob';
import { testConcurrencyLevel } from './test_concurrency.js';
import { findBaselineCdkVersion } from '../cdk_version_finder.js';
import { amplifyAtTag } from '../constants.js';
import { NpmProxyController } from '../npm_proxy_controller.js';
import { RetryPredicates, runWithRetry } from '../retry.js';
void describe(
'create-amplify script',
{ concurrency: testConcurrencyLevel },
() => {
let baselineCdkVersion: string;
const npmProxyController = new NpmProxyController();
before(async () => {
await npmProxyController.setUp();
// Prefixing with ~. Otherwise, npm is going to install desired version but
// declare dependency with ^ in package json. So just in case removing that
// ambiguity.
// Testing with latest CDK is covered in canary checks.
baselineCdkVersion = `~${await findBaselineCdkVersion()}`;
});
after(async () => {
await npmProxyController.tearDown();
});
const startDelayIntervalMS = 1000 * 20; // start tests 20 seconds apart to avoid file hot spots
const testCases = [
{
initialState: 'empty',
startDelayMS: startDelayIntervalMS * 0,
},
{
initialState: 'module',
startDelayMS: startDelayIntervalMS * 1,
},
{
initialState: 'commonjs',
startDelayMS: startDelayIntervalMS * 2,
},
];
testCases.forEach(({ initialState, startDelayMS }) => {
void describe('installs expected packages and scaffolds expected files', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'test-create-amplify')
);
});
afterEach(async () => {
await fs.rm(tempDir, { recursive: true });
});
void it(`starting from ${initialState} project`, async () => {
await new Promise((resolve) => setTimeout(resolve, startDelayMS));
if (initialState != 'empty') {
await fs.writeFile(
path.join(tempDir, 'package.json'),
JSON.stringify(
{
name: 'test_name',
version: '0.0.1',
type: initialState,
},
null,
2
)
);
}
await runWithRetry(async () => {
await execa(
'npm',
['create', amplifyAtTag, '--yes', '--', '--debug'],
{
cwd: tempDir,
stdio: 'inherit',
}
);
}, RetryPredicates.createAmplifyRetryPredicate);
// Override CDK installation with baseline version
await execa(
'npm',
[
'install',
'--save-dev',
`aws-cdk@${baselineCdkVersion}`,
`aws-cdk-lib@${baselineCdkVersion}`,
],
{
cwd: tempDir,
stdio: 'inherit',
}
);
const packageJsonPath = path.resolve(tempDir, 'package.json');
const packageJsonObject = JSON.parse(
await fs.readFile(packageJsonPath, 'utf-8')
);
assert.deepStrictEqual(
Object.keys(packageJsonObject.devDependencies).sort(),
[
'@aws-amplify/backend',
'@aws-amplify/backend-cli',
'aws-cdk',
'aws-cdk-lib',
'constructs',
'esbuild',
'tsx',
'typescript',
]
);
assert.strictEqual(
packageJsonObject.devDependencies['aws-cdk'],
baselineCdkVersion
);
assert.strictEqual(
packageJsonObject.devDependencies['aws-cdk-lib'],
baselineCdkVersion
);
assert.deepStrictEqual(
Object.keys(packageJsonObject.dependencies).sort(),
['aws-amplify']
);
const gitIgnorePath = path.resolve(tempDir, '.gitignore');
const gitIgnoreContent = (await fs.readFile(gitIgnorePath, 'utf-8'))
.split(os.EOL)
.filter((s) => s.trim());
assert.deepStrictEqual(gitIgnoreContent.sort(), [
'# amplify',
'.amplify',
'amplify_outputs*',
'amplifyconfiguration*',
'node_modules',
]);
const amplifyPathPrefix = path.join(tempDir, 'amplify');
// Read tsconfig.json content, remove all comments, and make assertions
const tsConfigPath = path.resolve(amplifyPathPrefix, 'tsconfig.json');
const tsConfigContent = (
await fs.readFile(tsConfigPath, 'utf-8')
).replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, '');
const tsConfigObject = JSON.parse(tsConfigContent);
assert.equal(tsConfigObject.compilerOptions.module, 'es2022');
assert.equal(
tsConfigObject.compilerOptions.moduleResolution,
'bundler'
);
assert.equal(tsConfigObject.compilerOptions.resolveJsonModule, true);
assert.deepStrictEqual(tsConfigObject.compilerOptions.paths, {
// The path here is coupled with backend-function's generated typedef file path
'$amplify/*': ['../.amplify/generated/*'],
});
const pathPrefix = path.join(tempDir, 'amplify');
const files = await glob(path.join(amplifyPathPrefix, '**', '*'), {
nodir: true,
windowsPathsNoEscape: true,
});
const expectedAmplifyFiles = [
path.join('auth', 'resource.ts'),
'backend.ts',
path.join('data', 'resource.ts'),
'package.json',
'tsconfig.json',
];
assert.deepStrictEqual(
files.sort(),
expectedAmplifyFiles.map((suffix) => path.join(pathPrefix, suffix))
);
// assert that project compiles successfully
await execa(
'npx',
[
'tsc',
'--noEmit',
'--skipLibCheck',
// pointing the project arg to the amplify backend directory will use the tsconfig present in that directory
'--project',
amplifyPathPrefix,
],
{
cwd: tempDir,
stdio: 'inherit',
}
);
// assert that project synthesizes successfully
await execa(
'npx',
[
'cdk',
'synth',
'--context',
`amplify-backend-namespace=123`,
'--context',
`amplify-backend-name=sandboxName`,
'--context',
`amplify-backend-type=sandbox`,
'--app',
"'npx tsx amplify/backend.ts'",
'--quiet',
],
{
cwd: tempDir,
stdio: 'inherit',
}
);
});
});
});
void describe('fails fast', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await fs.mkdtemp(
path.join(os.tmpdir(), 'test-create-amplify')
);
});
afterEach(async () => {
await fs.rm(tempDir, { recursive: true });
});
void it('if amplify path already exists', async () => {
const amplifyDirPath = path.join(tempDir, 'amplify');
await fs.mkdir(amplifyDirPath, { recursive: true });
const result = await execa('npm', ['create', amplifyAtTag, '--yes'], {
cwd: tempDir,
stdio: 'pipe',
reject: false,
});
assert.equal(result.exitCode, 1);
assert.ok(
result.stderr
.toLocaleString()
.includes(
'If you are trying to run an Amplify Gen 2 command inside an Amplify Gen 1 project we recommend creating the project in another directory'
)
);
});
});
}
);