Skip to content

Mock is not following through to node_modules #251

@Lilja-at-funnel

Description

@Lilja-at-funnel

I have a shared library that I have put some s3 list objects into:

// `@my-org/shared/dist/s3.js`
export const listObjects = async (s3Client: S3Client, bucket: string, prefix: string) => {
  const command = new ListObjectsV2Command({
    Bucket: bucket,
    Prefix: prefix,
  })

 return await s3Client.send(command) || []
}

I use it in src/main.ts:

import { listObjects } from '@my-org/shared/dist/s3'
import { ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3'

const s3Client = new S3Client({})

export const deleteConfigurations = async (id: string) => {
  const bucket = 'files'
  const prefix = `config/${id}/`
  console.log('Listing objects in bucket', bucket, 'with prefix', prefix)
  const resp = await s3Client.send(new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix }))
  console.log('Listed objects', resp)
  const objects = await listObjects(s3Client, bucket, prefix)
}

Here is my test case.

import { describe, it } from 'node:test'
import { deleteConfigurations} from '../src/main'
import { ListObjectsV2Command, S3Client } from '@aws-sdk/client-s3'
import { mockClient } from 'aws-sdk-client-mock'

const s3Client = mockClient(S3Client)

describe.only('listObjects', () => {
  it('#1', async () => {
    const sourceId = '123'
    s3Client.on(ListObjectsV2Command).resolves({
      Contents: [{ Key: `config/${sourceId}/file1` }, { Key: `config/${sourceId}/file2` }],
    })

    await deleteConfigurations(sourceId)
  })
})

Test output:

Listing objects in bucket files with prefix config/123/
Listed objects {
  Contents: [ { Key: 'config/123/file1' }, { Key: 'config/123/file2' } ]
}
    TypeError [Error]: Cannot read properties of undefined (reading 'Contents')
        at <anonymous> (/Users/lilja/code/selfservice/selfservice-shared/src/s3.ts:174:19)
        at Generator.next (<anonymous>)
        at fulfilled (/Users/lilja/code/selfservice/selfservice-shared/dist/s3.js:5:58)
▶ deleteConfigurations (4.153167ms)

ℹ 'only' and 'runOnly' require the --test-only command-line option.
(node:94450) NOTE: The AWS SDK for JavaScript (v2) is in maintenance mode.
 SDK releases are limited to address critical bug fixes and security issues only.

Please migrate your code to use AWS SDK for JavaScript (v3).
For more information, check the blog post at https://a.co/cUPnyil
(Use `node --trace-warnings ...` to show where the warning was created)

For some reason, the s3client mock is not following into the files in my node_modules. Am I missing something here?
The mock is working in src/main.ts but not for my disted/built JS file in @my-org/shared/dist/s3.js?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions