Skip to content

Commit

Permalink
chore: Upgrade Jest (#12607)
Browse files Browse the repository at this point in the history
* chore: Upgrade Jest

* Address feedback

* Address feedback

* Fix issue with required uuid-validate module

---------

Co-authored-by: Aaron S <[email protected]>
  • Loading branch information
cshfang and stocaaro authored Nov 28, 2023
1 parent cf3a619 commit 8ca7c15
Show file tree
Hide file tree
Showing 252 changed files with 5,604 additions and 8,083 deletions.
24 changes: 24 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @type {import('jest').Config} */
module.exports = {
coveragePathIgnorePatterns: [
'/node_modules/',
'dist',
'__tests__',
],
setupFiles: ['../../jest.setup.js'],
testEnvironment: 'jsdom',
testRegex: '/__tests__/.*\\.(test|spec)\\.[jt]sx?$',
transform: {
'^.+\\.(js|jsx|ts|tsx)$': [
'ts-jest',
{
tsconfig: {
allowJs: true,
lib: ['dom', 'es2020'],
noImplicitAny: false,
types: ['jest', 'jsdom'],
},
},
],
},
};
13 changes: 13 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Suppress console messages printing during unit tests.
// Comment out log level as necessary (e.g. while debugging tests)
global.console = {
...console,
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};

// React Native global
global['__DEV__'] = true;
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"setup-dev": "yarn && yarn bootstrap && yarn link-all && yarn build",
"setup-dev:react-native": "node ./scripts/setup-dev-rn",
"bootstrap": "lerna bootstrap",
"test": "lerna run test --stream && yarn test:license && yarn test:github-actions && yarn test:tsc-compliance",
"test": "yarn test:no-datastore && yarn test:datastore && yarn test:license && yarn test:github-actions && yarn test:tsc-compliance",
"test:no-datastore": "lerna run test --stream --ignore @aws-amplify/datastore",
"test:datastore": "lerna run test --stream --scope @aws-amplify/datastore",
"test:size": "lerna run test:size --no-bail",
"test:duplicates": "./scripts/duplicates-yarn.sh",
"test:license": "license-check-and-add check -f license_config.json",
Expand Down Expand Up @@ -84,16 +86,15 @@
"@size-limit/file": "^8.1.0",
"@size-limit/webpack": "^8.1.0",
"@size-limit/webpack-why": "^8.1.0",
"@types/jest": "^24.0.18",
"@types/jest": "^29.5.8",
"@types/lodash": "4.14.182",
"@types/node": "^8.9.5",
"@types/puppeteer": "1.3.0",
"babel-jest": "^24.9.0",
"babel-loader": "^8.3.0",
"codecov": "^3.6.5",
"glob": "^10.3.10",
"jest": "^24.x.x",
"jest-config": "24.8.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"json-loader": "^0.5.7",
"lerna": "^7.4.2",
"license-check-and-add": "^4.0.5",
Expand All @@ -107,12 +108,12 @@
"rollup-plugin-typescript": "^1.0.0",
"size-limit": "^8.1.0",
"terser-webpack-plugin": "^5.3.6",
"ts-jest": "^24.x.x",
"ts-jest": "^29.1.1",
"ts-loader": "^9.4.3",
"tslint": "^5.7.0",
"tslint-config-airbnb": "^5.8.0",
"typedoc": "^0.17.0",
"typescript": "~3.8.3",
"typescript": "^4.3.5",
"typescript-coverage-report": "^0.6.4",
"uuid-validate": "^0.0.3",
"webpack": "^5.75.0",
Expand All @@ -127,9 +128,5 @@
"**/glob/minipass": "6.0.2",
"nx": "16.7.0",
"next": "13.5.6"
},
"jest": {
"resetMocks": true,
"verbose": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('generateServerClientUsingCookies', () => {
request: mockedReq,
response: mockedRes,
});
}).toThrowError();
}).toThrow();
});

it('should call createRunWithAmplifyServerContext to create runWithAmplifyServerContext function', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,6 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
request: undefined,
response: new ServerResponse({} as any),
} as any)
).toThrowError();
).toThrow();
});
});
11 changes: 11 additions & 0 deletions packages/adapter-nextjs/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
...require('../../jest.config'),
coverageThreshold: {
global: {
branches: 88,
functions: 90,
lines: 92,
statements: 93,
},
},
};
49 changes: 0 additions & 49 deletions packages/adapter-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,54 +71,5 @@
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
"test": "npm run lint && jest -w 1 --coverage",
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 90.31"
},
"jest": {
"coveragePathIgnorePatterns": [
"/node_modules/",
"dist",
"lib",
"lib-esm",
"__tests__/mocks"
],
"coverageThreshold": {
"global": {
"branches": 88,
"functions": 90,
"lines": 92,
"statements": 93
}
},
"globals": {
"ts-jest": {
"diagnostics": {
"pathRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$"
},
"tsConfig": {
"allowJs": true,
"types": [
"@types/jest"
]
}
}
},
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json",
"jsx"
],
"testEnvironment": "node",
"testPathIgnorePatterns": [
"xmlParser-fixture.ts",
"testUtils",
"cases",
"mocks"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
"testURL": "http://localhost/",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
}
}
}
2 changes: 1 addition & 1 deletion packages/analytics/__tests__/apis/disable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe('Pinpoint APIs: disable', () => {
it('should disable Analytics', () => {
disable();

expect(mockDisableAnalytics).toBeCalledTimes(1);
expect(mockDisableAnalytics).toHaveBeenCalledTimes(1);
});
});
2 changes: 1 addition & 1 deletion packages/analytics/__tests__/apis/enable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ describe('Pinpoint APIs: enable', () => {
it('should enable Analytics', () => {
enable();

expect(mockEnableAnalytics).toBeCalledTimes(1);
expect(mockEnableAnalytics).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';
import { flushEvents } from '../../../../src/providers/kinesis-firehose/apis';
import { ConsoleLogger } from '@aws-amplify/core';

Expand Down Expand Up @@ -60,6 +60,9 @@ describe('Analytics Kinesis Firehose API: flushEvents', () => {

flushEvents();
await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Error)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isAnalyticsEnabled, resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';
import { record } from '../../../../src/providers/kinesis-firehose';
import { ConsoleLogger } from '@aws-amplify/core';
import { RecordInput as KinesisFirehoseRecordInput } from '../../../../src/providers/kinesis-firehose/types';
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Analytics KinesisFirehose API: record', () => {
record(mockRecordInput);
await new Promise(process.nextTick);
expect(mockGetEventBuffer).toHaveBeenCalledTimes(1);
expect(mockAppend).toBeCalledWith(
expect(mockAppend).toHaveBeenCalledWith(
expect.objectContaining({
region: mockKinesisConfig.region,
streamName: mockRecordInput.streamName,
Expand All @@ -70,15 +70,18 @@ describe('Analytics KinesisFirehose API: record', () => {
record(mockRecordInput);

await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Error)
);
});

it('logs and skip the event recoding if Analytics plugin is not enabled', async () => {
mockIsAnalyticsEnabled.mockReturnValue(false);
record(mockRecordInput);
await new Promise(process.nextTick);
expect(loggerDebugSpy).toBeCalledWith(expect.any(String));
expect(mockGetEventBuffer).not.toBeCalled();
expect(mockAppend).not.toBeCalled();
expect(loggerDebugSpy).toHaveBeenCalledWith(expect.any(String));
expect(mockGetEventBuffer).not.toHaveBeenCalled();
expect(mockAppend).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mockBufferConfig,
mockCredentialConfig,
mockKinesisConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';

jest.mock('../../../../src/utils');

Expand All @@ -24,7 +24,7 @@ describe('KinesisFirehose Provider Util: getEventBuffer', () => {
...mockCredentialConfig,
});

expect(mockEventBuffer).toBeCalledWith(
expect(mockEventBuffer).toHaveBeenCalledWith(
mockBufferConfig,
expect.any(Function)
);
Expand Down Expand Up @@ -59,8 +59,8 @@ describe('KinesisFirehose Provider Util: getEventBuffer', () => {

await new Promise(process.nextTick);

expect(testBuffer1.flushAll).toBeCalledTimes(1);
expect(testBuffer1.release).toBeCalledTimes(1);
expect(testBuffer1.flushAll).toHaveBeenCalledTimes(1);
expect(testBuffer1.release).toHaveBeenCalledTimes(1);
expect(testBuffer1).not.toBe(testBuffer2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {

it('throws if region is missing', () => {
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: { ...providedConfig, region: undefined } },
Analytics: {
KinesisFirehose: { ...providedConfig, region: undefined as any },
},
});

expect(resolveConfig).toThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';
import { getEventBuffer } from '../../../../src/providers/kinesis/utils/getEventBuffer';
import { flushEvents } from '../../../../src/providers/kinesis/apis';
import { ConsoleLogger } from '@aws-amplify/core';
Expand Down Expand Up @@ -59,6 +59,9 @@ describe('Analytics Kinesis API: flushEvents', () => {

flushEvents();
await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Error)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isAnalyticsEnabled, resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';
import { record } from '../../../../src/providers/kinesis';
import { ConsoleLogger } from '@aws-amplify/core';
import { RecordInput as KinesisRecordInput } from '../../../../src/providers/kinesis/types';
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Analytics Kinesis API: record', () => {
record(mockRecordInput);
await new Promise(process.nextTick);
expect(mockGetEventBuffer).toHaveBeenCalledTimes(1);
expect(mockAppend).toBeCalledWith(
expect(mockAppend).toHaveBeenCalledWith(
expect.objectContaining({
region: mockKinesisConfig.region,
streamName: mockRecordInput.streamName,
Expand All @@ -71,15 +71,18 @@ describe('Analytics Kinesis API: record', () => {
record(mockRecordInput);

await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
expect(loggerWarnSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(Error)
);
});

it('logs and skip the event recoding if Analytics plugin is not enabled', async () => {
mockIsAnalyticsEnabled.mockReturnValue(false);
record(mockRecordInput);
await new Promise(process.nextTick);
expect(loggerDebugSpy).toBeCalledWith(expect.any(String));
expect(mockGetEventBuffer).not.toBeCalled();
expect(mockAppend).not.toBeCalled();
expect(loggerDebugSpy).toHaveBeenCalledWith(expect.any(String));
expect(mockGetEventBuffer).not.toHaveBeenCalled();
expect(mockAppend).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mockBufferConfig,
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
} from '../../../testUtils/mockConstants';

jest.mock('../../../../src/utils');

Expand All @@ -24,7 +24,7 @@ describe('Kinesis Provider Util: getEventBuffer', () => {
...mockCredentialConfig,
});

expect(mockEventBuffer).toBeCalledWith(
expect(mockEventBuffer).toHaveBeenCalledWith(
mockBufferConfig,
expect.any(Function)
);
Expand Down Expand Up @@ -58,8 +58,8 @@ describe('Kinesis Provider Util: getEventBuffer', () => {
});

await new Promise(process.nextTick);
expect(testBuffer1.flushAll).toBeCalledTimes(1);
expect(testBuffer1.release).toBeCalledTimes(1);
expect(testBuffer1.flushAll).toHaveBeenCalledTimes(1);
expect(testBuffer1.release).toHaveBeenCalledTimes(1);
expect(testBuffer1).not.toBe(testBuffer2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Analytics Kinesis Provider Util: resolveConfig', () => {

it('throws if region is missing', () => {
getConfigSpy.mockReturnValue({
Analytics: { Kinesis: { ...kinesisConfig, region: undefined } },
Analytics: { Kinesis: { ...kinesisConfig, region: undefined as any } },
});

expect(resolveConfig).toThrow();
Expand Down
Loading

0 comments on commit 8ca7c15

Please sign in to comment.