-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aws-cdk-lib: jest.resetModules causes tests to fail starting with 2.94.0 #27312
Comments
+1 also seeing this when doing |
You're right, thanks for the reproduction steps. I've been able to reproduce this and see that it is breaking going from I noticed that if you remove the May be related to this change #26854 |
I see that there was a Jest-related change in This is mystery. I'm looking through the set of changes and I can't even find anything else that looks suspicious 🤔 |
To be honest I'm unsure about the purpose of As per the documentation, it's going to affect all subsequent // These run at module load time, will not be affected by 'resetModules()'
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', () => {
beforeEach(() => {
jest.resetModules()
// Every require() or import performed AFTER this point will get a new copy of the module
})
}); What might work is doing the following: 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'
let expectCDK: typeof import('@aws-cdk/assert')['expect'];
let countResources: typeof import('@aws-cdk/assert')['countResources'];
let App: typeof import('aws-cdk-lib)['App'];
let DNSStack: typeof import('../lib/stacks')['DNSStack'];
beforeEach(() => {
jest.resetModules();
expectCDK = require('@aws-cdk/assert').expect;
countResources = require('@aws-cdk/assert').countResources;
App = require('aws-cdk-lib').App;
DNSStack = require('../lib/stacks').DNSStack;
}); To make sure that the |
By the way, the code that is failing looks to be: const constructs_1=require("constructs");
class ExportWriter extends constructs_1.Construct
^^^^ value undefined is not a constructor or null So it looks like |
Thanks for the suggestion. I don't think that would work with ESM modules. So I think this results in:
I also think your import statement needs changing because:
and
Furthermore, in order for this to work, in case you are using eslint, you would need to add:
|
@peterwoodworth, for this particular case removing |
In that case, I think you would write: beforeEach(async () => {
jest.resetModules();
expectCDK = (await import('@aws-cdk/assert')).expect;
countResources = (await import('@aws-cdk/assert')).countResources;
App = (await import('aws-cdk-lib')).App;
DNSStack = (await import('../lib/stacks')).DNSStack;
});
My mistake, I meant to remove the top-level import { jest } from '@jest/globals'
let expectCDK: typeof import('@aws-cdk/assert')['expect'];
let countResources: typeof import('@aws-cdk/assert')['countResources'];
let App: typeof import('aws-cdk-lib)['App'];
let DNSStack: typeof import('../lib/stacks')['DNSStack'];
beforeEach(async () => {
jest.resetModules();
expectCDK = (await import('@aws-cdk/assert')).expect;
countResources = (await import('@aws-cdk/assert')).countResources;
App = (await import('aws-cdk-lib')).App;
DNSStack = (await import('../lib/stacks')).DNSStack;
});
I'm happy to keep this open as a bug for people to collect insights on. I have no idea what would be causing this issue though, and no idea of how to start tracking it. It would help if you could come up with a minimal reproducing example. |
Confirming this work around does work.
For some more insight, the reason we use |
@The-Zona-Zoo, thanks, that's helpful. Are you referring to CDK code that is caching those environment variables into global variables somewhere? Again, a reproducing example would be helpful. |
@rix0rrr While I can't post the full example, it's essentially something to the effect of: let App: (typeof import('aws-cdk-lib/core'))['App'];
let Template: (typeof import('aws-cdk-lib/assertions'))['Template'];
describe('SomeCdkStack', () => {
const previousEnv = { ...process.env };
beforeEach(async () => {
if (process.env.USER) {
delete process.env.USER;
jest.resetModules();
}
App = (await import('aws-cdk-lib/core')).App;
Template = (await import('aws-cdk-lib/assertions')).Template;
});
afterEach(() => {
process.env = previousEnv;
});
}); This example doesn't work with importing The stack being tested in question has a statement similar to |
Describe the bug
When upgrading from
2.88.0
to2.98.0
we get this error when running the same code:No code changes have been introduced. just package updates.
Expected Behavior
tests pass, deployment works
Current Behavior
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:
Write a test for it
Run test:
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
The text was updated successfully, but these errors were encountered: