Skip to content

Commit f7a38ac

Browse files
committed
Use locally built serverless package instead of pulling from npm in e2e tests
1 parent c4e92d4 commit f7a38ac

File tree

1 file changed

+39
-3
lines changed
  • dev-packages/e2e-tests/test-applications/aws-serverless/src

1 file changed

+39
-3
lines changed

dev-packages/e2e-tests/test-applications/aws-serverless/src/stack.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const LAMBDA_FUNCTIONS_WITH_LAYER_DIR = './src/lambda-functions-layer';
1212
const LAMBDA_FUNCTIONS_WITH_NPM_DIR = './src/lambda-functions-npm';
1313
const LAMBDA_FUNCTION_TIMEOUT = 10;
1414
const LAYER_DIR = './node_modules/@sentry/aws-serverless/';
15+
const PACKAGES_DIR = path.resolve(__dirname, '../../../../../packages');
1516
const DEFAULT_NODE_VERSION = '22';
1617
export const SAM_PORT = 3001;
1718

@@ -67,10 +68,45 @@ export class LocalLambdaStack extends Stack {
6768
const functionName = `${addLayer ? 'Layer' : 'Npm'}${lambdaDir}`;
6869

6970
if (!addLayer) {
71+
const lambdaPath = path.resolve(functionsDir, lambdaDir);
72+
const packageLockPath = path.join(lambdaPath, 'package-lock.json');
73+
const nodeModulesPath = path.join(lambdaPath, 'node_modules');
74+
75+
// Point the dependency at the locally built packages so tests use the current workspace bits
76+
// We need to link all @sentry/* packages that are dependencies of aws-serverless
77+
// because otherwise npm will try to install them from the registry, where the current version is not yet published
78+
const packagesToLink = ['aws-serverless', 'node', 'core', 'node-core', 'opentelemetry'];
79+
const dependencies: Record<string, string> = {};
80+
81+
for (const pkgName of packagesToLink) {
82+
const pkgDir = path.join(PACKAGES_DIR, pkgName);
83+
if (!fs.existsSync(pkgDir)) {
84+
throw new Error(
85+
`[LocalLambdaStack] Workspace package ${pkgName} not found at ${pkgDir}. Did you run the build?`,
86+
);
87+
}
88+
const relativePath = path.relative(lambdaPath, pkgDir);
89+
dependencies[`@sentry/${pkgName}`] = `file:${relativePath.replace(/\\/g, '/')}`;
90+
}
91+
7092
console.log(`[LocalLambdaStack] Install dependencies for ${functionName}`);
71-
const packageJson = { dependencies: { '@sentry/aws-serverless': '* || latest' } };
72-
fs.writeFileSync(path.join(functionsDir, lambdaDir, 'package.json'), JSON.stringify(packageJson, null, 2));
73-
execFileSync('npm', ['install', '--prefix', path.join(functionsDir, lambdaDir)], { stdio: 'inherit' });
93+
94+
if (fs.existsSync(packageLockPath)) {
95+
// Prevent stale lock files from pinning the published package version
96+
fs.rmSync(packageLockPath);
97+
}
98+
99+
if (fs.existsSync(nodeModulesPath)) {
100+
// Ensure we reinstall from the workspace instead of reusing cached dependencies
101+
fs.rmSync(nodeModulesPath, { recursive: true, force: true });
102+
}
103+
104+
const packageJson = {
105+
dependencies,
106+
};
107+
108+
fs.writeFileSync(path.join(lambdaPath, 'package.json'), JSON.stringify(packageJson, null, 2));
109+
execFileSync('npm', ['install', '--prefix', lambdaPath], { stdio: 'inherit' });
74110
}
75111

76112
new CfnResource(this, functionName, {

0 commit comments

Comments
 (0)