aws / aws/aws-cdk

aws-cdk-lib: jest.resetModules causes tests to fail starting with 2.94.0

Open
#27,312 11 comments 0 reactions 0 assignees View on GitHub
aws-cdk-lib bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When upgrading from `2.88.0` to `2.98.0` we get this error when running the same code:
> TypeError: Class extends value undefined is not a constructor or null

No code changes have been introduced. just package updates.
```
diff --git a/package-lock.json b/package-lock.json
index fe055dc..9ce311f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,9 +8,9 @@
"name": "@npm-shared-services/cdk-common-components",
"version": "0.54.0",
"dependencies": {
- "@aws-cdk/aws-apigatewayv2-alpha": "2.88.0-alpha.0",
- "@aws-cdk/aws-apigatewayv2-authorizers-alpha": "2.88.0-alpha.0",
- "@aws-cdk/aws-apigatewayv2-integrations-alpha": "2.88.0-alpha.0",
+ "@aws-cdk/aws-apigatewayv2-alpha": "2.98.0-alpha.0",
+ "@aws-cdk/aws-apigatewayv2-authorizers-alpha": "2.98.0-alpha.0",
+ "@aws-cdk/aws-apigatewayv2-integrations-alpha": "2.98.0-alpha.0",
"@aws-lambda-powertools/logger": "1.13.1",
"aws-lambda": "1.0.7",
"source-map-support": "^0.5.21"
@@ -21,8 +21,8 @@
"@types/aws-lambda": "8.10.122",
"@types/jest": "^29.5.5",
"@types/node": "20.7.0",
- "aws-cdk": "2.88.0",
- "aws-cdk-lib": "2.88.0",
+ "aws-cdk": "2.98.0",
+ "aws-cdk-lib": "2.98.0",
"constructs": "^10.2.70",
"cross-env": "7.0.3",
"eslint": "8.50.0",
```

### Expected Behavior

tests pass, deployment works

### Current Behavior

```
(node:75173) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
FAIL test/base-dns.test.ts
● testing stack › Domains stack

TypeError: Class extends value undefined is not a constructor or null

43 | )
44 |
> 45 | new CrossAccountZoneDelegationRecord(this, 'delegatecSubdomain', {
| ^
46 | delegatedZone: GuideZone,
47 | parentHostedZoneName: config.main_hosted_zone_name,
48 | delegationRole

at Object. (node_modules/aws-cdk-lib/core/lib/custom-resource-provider/cross-region-export-providers/export-writer-provider.js:1:717)
at Object. (node_modules/aws-cdk-lib/core/lib/private/refs.js:1:561)
at Object. (node_modules/aws-cdk-lib/core/lib/private/prepare-app.js:1:245)
at Object. (node_modules/aws-cdk-lib/core/lib/private/synthesis.js:1:333)
at Object. (node_modules/aws-cdk-lib/core/lib/app.js:1:425)
at Object. (node_modules/aws-cdk-lib/core/lib/annotations.js:1:260)
at Object. (node_modules/aws-cdk-lib/core/lib/stack.js:1:450)
at Object. (node_modules/aws-cdk-lib/core/lib/names.js:1:497)
at Object. (node_modules/aws-cdk-lib/core/lib/asset-staging.js:1:551)
at Object. (node_modules/aws-cdk-lib/core/lib/custom-resource-provider/custom-resource-provider.js:1:532)
at aws_cdk_lib_CustomResourceProviderRuntime (node_modules/aws-cdk-lib/.warnings.jsii.js:2:344450)
at Object.aws_cdk_lib_CustomResourceProviderProps (node_modules/aws-cdk-lib/.warnings.jsii.js:2:344155)
at Function.getOrCreateProvider (node_modules/aws-cdk-lib/core/lib/custom-resource-provider/custom-resource-provider.js:1:1966)
at new CrossAccountZoneDelegationRecord (node_modules/aws-cdk-lib/aws-route53/lib/record-set.js:1:11869)
at new DNSStack (lib/stacks/base-dns-stack.ts:45:5)
at Object. (test/base-dns.test.ts:37:19)

(node:75174) ExperimentalWarning: VM Modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
```

### Reproduction Steps

The code is redacted for obvious purposes. The code works fine with `2.88` and no changes have been added to the code or the context values (see diff above, that's all that changed)

Write a stack that include this:
```
...
const delegationRoleArn = Stack.of(this).formatArn({
region: '', // IAM is global in each partition
service: 'iam',
account: SHARED_SERVICES_ACCOUNT,
resource: 'role',
resourceName: CROSS_ACCOUNT_DELEGATION_ROLE_NAME
})

const delegationRole = Role.fromRoleArn(
this,
'DelegationRole',
delegationRoleArn
)

// NEW ROAMY DELEGATION
const roamyGuideZone = new PublicHostedZone(
this,
'GuideSubdomainHostedZone',
{
zoneName: config.subdomain_hosted_zone_name
}
)

new CrossAccountZoneDelegationRecord(this, 'delegateSubdomain', {
delegatedZone: GuideZone,
parentHostedZoneName: config.main_hosted_zone_name,
delegationRole
})
...
```
Write a test for it
```
import { expect as expectCDK, countResources } from '@aws-cdk/assert'
import { jest } from '@jest/globals'
import { App } from 'aws-cdk-lib'

import { DNSStack } from '../lib/stacks'

jest.useFakeTimers()

describe('testing stack', () => {
const OLD_ENV_STAGE = process.env.STAGE
const testingStage = 'testing'

beforeEach(() => {
jest.resetModules()
process.env.STAGE = testingStage
})

afterAll(() => {
process.env.STAGE = OLD_ENV_STAGE
})

test('Domains stack', () => {
const app = new App({
context: {
testing: {
main_hosted_zone_name: 'xxxxxxxx.guide',
subdomain_hosted_zone_name: 'staging.xxxxxxxx.guide',
sharing_subdomain_hosted_zone_name:
'staging.xxxxxxxx.guide',
sharing_subdomain_entry: 'sharing.staging.xxxxxxxx.guide'
}
}
})
// WHEN
const envEU = { account: '000000000000', region: 'eu-central-1' }

const stack = new DNSStack(app, 'MyTestDNSStack', {
env: envEU,
crossRegionReferences: true
})
// THEN
expectCDK(stack).to(countResources('AWS::Route53::HostedZone', 1))
})
})
```
Run test:
```
STAGE=testing NODE_OPTIONS=--experimental-vm-modules npx jest
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.98.0 (build b04f852)

### Framework Version

2.98.0

### Node.js Version

v20.2.0

### OS

mac arm

### Language

Typescript

### Language Version

5.2.2

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by running the command in test/base-dns.test.ts with the supplied Node and CDK versions, then inspect lib/stacks/base-dns-stack.ts around CrossAccountZoneDelegationRecord. Compare behavior with and without jest.resetModules and across the reported CDK versions. Done means the reproduced test passes while retaining the resetModules setup and deployment behavior is not regressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.