Skip to content

chore: migrate to vitest v3 #3561

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

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"source-map-support": "^0.5.21",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"vitest": "^2.0.5"
"vitest": "^3.0.5"
},
"dependencies": {
"@aws-lambda-powertools/batch": "^2.13.1",
Expand Down
2 changes: 1 addition & 1 deletion layers/tests/unit/layer-publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ describe('Class: LayerPublisherStack', () => {
Name: '/layers/powertools-layer-arn',
Type: 'String',
});
});
}, 120000);
});
595 changes: 302 additions & 293 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@biomejs/biome": "^1.9.4",
"@types/aws-lambda": "^8.10.147",
"@types/node": "^22.12.0",
"@vitest/coverage-v8": "^2.1.8",
"@vitest/coverage-v8": "^3.0.5",
"husky": "^9.1.7",
"lerna": "8.1.2",
"lint-staged": "^15.4.3",
Expand All @@ -65,7 +65,7 @@
"typedoc-plugin-missing-exports": "^3.1.0",
"typedoc-plugin-zod": "^1.3.1",
"typescript": "^5.7.3",
"vitest": "^2.0.5"
"vitest": "^3.0.5"
},
"lint-staged": {
"*.{js,ts}": "biome check --write",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ describe('Class: DynamoDBPersistenceLayer', () => {

afterEach(() => {
vi.clearAllMocks();
vi.resetAllMocks();
client.reset();
});

Expand Down Expand Up @@ -306,9 +305,9 @@ describe('Class: DynamoDBPersistenceLayer', () => {

it('puts record in DynamoDB table when using payload validation', async () => {
// Prepare
vi.spyOn(persistenceLayer, 'isPayloadValidationEnabled').mockReturnValue(
true
);
const persistenceLayerSpy = vi
.spyOn(persistenceLayer, 'isPayloadValidationEnabled')
.mockReturnValue(true);
const status = IdempotencyRecordStatus.EXPIRED;
const expiryTimestamp = 0;
const record = new IdempotencyRecord({
Expand Down Expand Up @@ -344,6 +343,7 @@ describe('Class: DynamoDBPersistenceLayer', () => {
ConditionExpression:
'attribute_not_exists(#id) OR #expiry < :now OR (#status = :inprogress AND attribute_exists(#in_progress_expiry) AND #in_progress_expiry < :now_in_millis)',
});
persistenceLayerSpy.mockRestore();
});

it('throws when called with a record that fails any condition', async () => {
Expand All @@ -353,6 +353,7 @@ describe('Class: DynamoDBPersistenceLayer', () => {
status: IdempotencyRecordStatus.EXPIRED,
expiryTimestamp: 0,
});
const expiration = Date.now();
client.on(PutItemCommand).rejects(
new ConditionalCheckFailedException({
$metadata: {
Expand All @@ -363,7 +364,7 @@ describe('Class: DynamoDBPersistenceLayer', () => {
Item: {
id: { S: 'test-key' },
status: { S: 'INPROGRESS' },
expiration: { N: Date.now().toString() },
expiration: { N: expiration.toString() },
},
})
);
Expand All @@ -373,9 +374,9 @@ describe('Class: DynamoDBPersistenceLayer', () => {
new IdempotencyItemAlreadyExistsError(
`Failed to put record for already existing idempotency key: ${record.idempotencyKey}`,
new IdempotencyRecord({
idempotencyKey: record.idempotencyKey,
status: IdempotencyRecordStatus.EXPIRED,
expiryTimestamp: Date.now() / 1000 - 1,
idempotencyKey: 'test-key',
status: IdempotencyRecordStatus.INPROGRESS,
expiryTimestamp: expiration,
})
)
);
Expand Down Expand Up @@ -575,10 +576,9 @@ describe('Class: DynamoDBPersistenceLayer', () => {

it('uses the payload hash in the expression when payload validation is enabled', async () => {
// Prepare
vi.spyOn(
persistenceLayer,
'isPayloadValidationEnabled'
).mockImplementation(() => true);
const persistenceLayerSpy = vi
.spyOn(persistenceLayer, 'isPayloadValidationEnabled')
.mockImplementation(() => true);
const expiryTimestamp = Date.now();
const record = new IdempotencyRecord({
idempotencyKey: dummyKey,
Expand Down Expand Up @@ -612,6 +612,7 @@ describe('Class: DynamoDBPersistenceLayer', () => {
':validation_key': record.payloadHash,
}),
});
persistenceLayerSpy.mockRestore();
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Formatters', () => {
process.env = { ...ENVIRONMENT_VARIABLES };
const mockDate = new Date(1466424490000);
vi.useFakeTimers().setSystemTime(mockDate);
vi.resetAllMocks();
vi.clearAllMocks();
unformattedAttributes.timestamp = mockDate;
});

Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/initializeLogger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Log levels', () => {

beforeEach(() => {
process.env = { ...ENVIRONMENT_VARIABLES, POWERTOOLS_DEV: 'true' };
vi.resetAllMocks();
vi.clearAllMocks();
});

it('uses the default service name when none is provided', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/injectLambdaContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Inject Lambda Context', () => {
...ENVIRONMENT_VARIABLES,
POWERTOOLS_DEV: 'true',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('adds the context to log messages when the feature is enabled', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/logEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Log event', () => {
POWERTOOLS_LOGGER_LOG_EVENT: 'true',
POWERTOOLS_DEV: 'true',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('logs the event with the correct log level and message', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/logLevels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Log levels', () => {

beforeEach(() => {
process.env = { ...ENVIRONMENT_VARIABLES, POWERTOOLS_DEV: 'true' };
vi.resetAllMocks();
vi.clearAllMocks();
});

it('sets the correct log level when initialized with a log level', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/tests/unit/workingWithkeys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Working with keys', () => {
...ENVIRONMENT_VARIABLES,
POWERTOOLS_DEV: 'true',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it.each([
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/coldStartMetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('ColdStart metric', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('emits a cold start metric', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/creatingMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Creating metrics', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('creates a compliant CloudWatch EMF metric', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/customTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Setting custom timestamp', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
vi.useFakeTimers().setSystemTime(new Date());
});

Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/dimensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('Working with dimensions', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('adds default dimensions to the metric via constructor', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/initializeMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('Initialize Metrics', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('uses the default service name when none is provided', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/logMetrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('captures the cold start metric on the first invocation', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/unit/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Working with metadata', () => {
POWERTOOLS_DEV: 'true',
POWERTOOLS_METRICS_DISABLED: 'false',
};
vi.resetAllMocks();
vi.clearAllMocks();
});

it('adds metadata to the metric', () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/parameters/tests/unit/AppConfigProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ describe('Class: AppConfigProvider', () => {
const provider = new AppConfigProvider(options);

// Assess
expect(provider.client.config.region()).resolves.toEqual('eu-south-2');
await expect(provider.client.config.region()).resolves.toEqual(
'eu-south-2'
);
expect(addUserAgentMiddleware).toHaveBeenCalled();
});

Expand Down Expand Up @@ -96,7 +98,6 @@ describe('Class: AppConfigProvider', () => {
environment: 'MyAppProdEnv',
awsSdkV3Client: awsSdkV3Client as AppConfigDataClient,
};
const consoleWarnSpy = vi.spyOn(console, 'warn');

// Act
const provider = new AppConfigProvider(options);
Expand All @@ -107,7 +108,7 @@ describe('Class: AppConfigProvider', () => {
serviceId: 'AppConfigData',
})
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
expect(console.warn).toHaveBeenNthCalledWith(
1,
'awsSdkV3Client is not an AWS SDK v3 client, using default client'
);
Expand Down
7 changes: 4 additions & 3 deletions packages/parameters/tests/unit/DynamoDBProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('Class: DynamoDBProvider', () => {
const provider = new DynamoDBProvider(options);

// Assess
expect(provider.client.config.region()).resolves.toEqual('eu-south-2');
await expect(provider.client.config.region()).resolves.toEqual(
'eu-south-2'
);
expect(addUserAgentMiddleware).toHaveBeenCalled();
});

Expand Down Expand Up @@ -92,7 +94,6 @@ describe('Class: DynamoDBProvider', () => {
tableName: 'test-table',
awsSdkV3Client: awsSdkV3Client as DynamoDBClient,
};
const consoleWarnSpy = vi.spyOn(console, 'warn');

// Act
const provider = new DynamoDBProvider(options);
Expand All @@ -103,7 +104,7 @@ describe('Class: DynamoDBProvider', () => {
serviceId: 'DynamoDB',
})
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
expect(console.warn).toHaveBeenNthCalledWith(
1,
'awsSdkV3Client is not an AWS SDK v3 client, using default client'
);
Expand Down
7 changes: 4 additions & 3 deletions packages/parameters/tests/unit/SSMProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ describe('Class: SSMProvider', () => {
const provider = new SSMProvider(options);

// Assess
expect(provider.client.config.region()).resolves.toEqual('eu-south-2');
await expect(provider.client.config.region()).resolves.toEqual(
'eu-south-2'
);
expect(addUserAgentMiddleware).toHaveBeenCalled();
});

Expand Down Expand Up @@ -96,7 +98,6 @@ describe('Class: SSMProvider', () => {
const options: SSMProviderOptions = {
awsSdkV3Client: awsSdkV3Client as SSMClient,
};
const consoleWarnSpy = vi.spyOn(console, 'warn');

// Act
const provider = new SSMProvider(options);
Expand All @@ -107,7 +108,7 @@ describe('Class: SSMProvider', () => {
serviceId: 'SSM',
})
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
expect(console.warn).toHaveBeenNthCalledWith(
1,
'awsSdkV3Client is not an AWS SDK v3 client, using default client'
);
Expand Down
7 changes: 4 additions & 3 deletions packages/parameters/tests/unit/SecretsProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ describe('Class: SecretsProvider', () => {
const provider = new SecretsProvider(options);

// Assess
expect(provider.client.config.region()).resolves.toEqual('eu-south-2');
await expect(provider.client.config.region()).resolves.toEqual(
'eu-south-2'
);
expect(addUserAgentMiddleware).toHaveBeenCalled();
});

Expand Down Expand Up @@ -83,7 +85,6 @@ describe('Class: SecretsProvider', () => {
const options: SecretsProviderOptions = {
awsSdkV3Client: awsSdkV3Client as SecretsManagerClient,
};
const consoleWarnSpy = vi.spyOn(console, 'warn');

// Act
const provider = new SecretsProvider(options);
Expand All @@ -94,7 +95,7 @@ describe('Class: SecretsProvider', () => {
serviceId: 'Secrets Manager',
})
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
expect(console.warn).toHaveBeenNthCalledWith(
1,
'awsSdkV3Client is not an AWS SDK v3 client, using default client'
);
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/tests/unit/envelopes/apigw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('Envelope: API Gateway REST', () => {
const result = ApiGatewayEnvelope.safeParse(event, schema);

// Assess
expect(result).toEqual({
expect(result).be.deep.equal({
success: false,
error: new ParseError('Failed to parse API Gateway body', {
cause: new ZodError([
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/tests/unit/envelopes/apigwv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('Envelope: API Gateway HTTP', () => {
const result = ApiGatewayV2Envelope.safeParse(event, schema);

// Assess
expect(result).toEqual({
expect(result).be.deep.equal({
success: false,
error: new ParseError('Failed to parse API Gateway HTTP body', {
cause: new ZodError([
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/tests/unit/envelopes/cloudwatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Envelope: CloudWatch', () => {
);

// Assess
expect(result).toStrictEqual({
expect(result).be.deep.equal({
success: false,
error: new ParseError(
'Failed to parse CloudWatch Log message at index 0',
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('Envelope: CloudWatch', () => {
);

// Assess
expect(result).toStrictEqual({
expect(result).be.deep.equal({
success: false,
error: new ParseError(
'Failed to parse CloudWatch Log messages at indexes 0, 1, 2',
Expand Down
Loading