Skip to content
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

fix!: remove @aws-sdk types and client-s3 from peer dependencies #57

Merged
merged 8 commits into from
Jun 13, 2022
Merged
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ jobs:
- run: yarn run test-types
- run: yarn run size

- name: bump version
run: yarn run release --skip.commit --skip.tag

- name: run e2e tests
run: |
yarn run verdaccio &
yarn run local-publish
yarn run test-e2e

codecov:
name: Codecov
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ a helper function `mockLibStorageUpload()`
that will configure required S3Client command mocks:

```typescript
import {mockLibStorageUpload} from 'aws-sdk-client-mock';
import {mockLibStorageUpload} from 'aws-sdk-client-mock/libStorage';
import {Upload} from '@aws-sdk/lib-storage';
import {S3Client} from '@aws-sdk/client-s3';

Expand All @@ -281,6 +281,9 @@ s3Upload.on('httpUploadProgress', (progress) => {
await s3Upload.done();
```

When using with Jest, it requires Jest v28.0.0 or higher,
[supporting package exports](https://github.com/facebook/jest/issues/9771).

You can call `mockLibStorageUpload()` without providing an S3Client mock.
In that case, the client mock will be created and returned from the function.
However, you still need to have `@aws-sdk/client-s3` installed as a dependency.
Expand Down
28 changes: 22 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"pretest": "rimraf coverage/",
"test": "jest --coverage",
"test-types": "tsd",
"test-e2e": "ts-node test-e2e/run-e2e-test.ts",
"test-e2e": "ts-node test-e2e/simple/run.ts & ts-node test-e2e/libStorage/run.ts",
"lint": "eslint .",
"typedoc": "typedoc",
"build:cjs": "tsc -p tsconfig.json",
Expand All @@ -36,9 +36,29 @@
"verdaccio": "rimraf verdaccio-storage && verdaccio -c verdaccio.yml",
"local-publish": "npm publish --registry http://localhost:4873/"
},
"exports": {
".": {
"import": "./dist/es/index.js",
"require": "./dist/cjs/index.js"
},
"./libStorage": {
"import": "./dist/es/libStorage.js",
"require": "./dist/cjs/libStorage.js"
}
},
"typesVersions": {
"*": {
"*": [
"dist/types/index.d.ts"
],
"libStorage": [
"dist/types/libStorage.d.ts"
]
}
},
"module": "dist/es/index.js",
"main": "dist/cjs/index.js",
"types": "dist/types/index.d.ts",
"module": "dist/es/index.js",
"files": [
"dist",
"src"
Expand All @@ -48,10 +68,6 @@
"sinon": "^11.1.1",
"tslib": "^2.1.0"
},
"peerDependencies": {
"@aws-sdk/client-s3": "^3.0.0",
"@aws-sdk/types": "^3.0.0"
},
"devDependencies": {
"@aws-sdk/client-dynamodb": "3.49.0",
"@aws-sdk/client-s3": "3.49.0",
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './mockClient';
export * from './awsClientStub';
export * from './libStorage';
17 changes: 2 additions & 15 deletions src/libStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AwsClientStub} from './awsClientStub';
import type {S3Client as S3ClientType} from '@aws-sdk/client-s3';
import {CreateMultipartUploadCommand, S3Client as S3ClientType, UploadPartCommand} from '@aws-sdk/client-s3';
import {mockClient} from './mockClient';

/**
Expand All @@ -10,21 +10,8 @@ import {mockClient} from './mockClient';
* @param s3Mock S3Client mock created with {@link mockClient} function
*/
export const mockLibStorageUpload = (s3Mock?: AwsClientStub<S3ClientType>): AwsClientStub<S3ClientType> => {
/*
* Not all library consumers also use @aws-sdk/client-s3. Importing it in a standard TS way
* would cause errors for them, as the module would be tried to found and import.
* Instead, we require classes from it dynamically in the scope of the function.
*
* Another solution would be to not export this function in the index.ts
* and instead have consumers to import it from 'aws-sdk-client-mock/libStorage'.
* This however turned out to be complicated to achieve in terms of exposing modules properly
* for CommonJS and ES modules.
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-assignment
const {CreateMultipartUploadCommand, S3Client, UploadPartCommand} = require('@aws-sdk/client-s3');

if (!s3Mock) {
s3Mock = mockClient(S3Client);
s3Mock = mockClient(S3ClientType);
}

s3Mock.on(CreateMultipartUploadCommand).resolves({UploadId: '1'});
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions test-e2e/libStorage/libStorage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {mockClient} from 'aws-sdk-client-mock';
import {mockLibStorageUpload} from 'aws-sdk-client-mock/libStorage';
import {S3Client} from '@aws-sdk/client-s3';
import {Progress, Upload} from '@aws-sdk/lib-storage';

it('mocks S3 Client Upload', async () => {
const s3Mock = mockClient(S3Client);
mockLibStorageUpload(s3Mock);

const s3Upload = new Upload({
client: new S3Client({}),
partSize: 5 * 1024 * 1024, // 5 MB
params: {
Bucket: 'mock',
Key: 'test',
Body: 'x'.repeat(6 * 1024 * 1024), // 6 MB
},
});

const uploadProgress: Progress[] = [];
s3Upload.on('httpUploadProgress', (progress) => {
uploadProgress.push(progress);
});

await s3Upload.done();

expect(uploadProgress).toHaveLength(2);
});
15 changes: 15 additions & 0 deletions test-e2e/libStorage/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {execSync} from 'child_process';
import * as packageJson from '../../package.json';

const libVersion = packageJson.version;

const exec = (cmd: string) => execSync(cmd, {
cwd: __dirname,
stdio: 'inherit',
});

// use npm because when running this script from yarn and using yarn
// it did not accept private registry parameter set in other way than in a project-level .yarnrc file
exec('npm init -y');
exec(`npm install --registry=http://localhost:4873 aws-sdk-client-mock@${libVersion} @aws-sdk/client-s3@latest @aws-sdk/lib-storage@latest jest typescript ts-jest`);
exec('npx jest');
5 changes: 5 additions & 0 deletions test-e2e/simple/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
resetMocks: true,
testEnvironment: 'node',
};
2 changes: 1 addition & 1 deletion test-e2e/run-e2e-test.ts → test-e2e/simple/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {execSync} from 'child_process';
import * as packageJson from '../package.json';
import * as packageJson from '../../package.json';

const libVersion = packageJson.version;

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion test/libStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Progress, Upload} from '@aws-sdk/lib-storage';
import {S3Client} from '@aws-sdk/client-s3';
import {AwsClientStub, mockClient, mockLibStorageUpload} from '../src';
import {AwsClientStub, mockClient} from '../src';
import {mockLibStorageUpload} from '../src/libStorage';

let s3Mock: AwsClientStub<S3Client>;

Expand Down