Skip to content

Commit

Permalink
make typing for attach and log functions correct (#2238)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjgoss committed Feb 12, 2023
1 parent 93ee7ee commit 35d6743
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Please see [CONTRIBUTING.md](https://github.com/cucumber/cucumber/blob/master/CO
## [Unreleased]
### Fixed
- Exit correctly when there's a Gherkin parse failure [#2233](https://github.com/cucumber/cucumber-js/pull/2233)
- Refer to correct example line in JSON formatter ([#2236](https://github.com/cucumber/cucumber-js/pull/2236))
- Refer to correct example line in JSON formatter ([#2236](https://github.com/cucumber/cucumber-js/pull/2236))
- Expose correct overload types for `this.attach` function ([#2238](https://github.com/cucumber/cucumber-js/pull/2238))

## [8.11.0] - 2023-02-10
### Added
Expand Down
23 changes: 17 additions & 6 deletions src/runtime/attachment_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@ export interface IAttachment {
}

export type IAttachFunction = (attachment: IAttachment) => void
export type ICreateAttachment = (
data: Buffer | Readable | string,
mediaType?: string,
callback?: () => void
) => void | Promise<void>
export type ICreateLog = (text: string) => void | Promise<void>

export type ICreateStringAttachment = (data: string, mediaType?: string) => void
export type ICreateBufferAttachment = (data: Buffer, mediaType: string) => void
export type ICreateStreamAttachment = (
data: Readable,
mediaType: string
) => Promise<void>
export type ICreateStreamAttachmentWithCallback = (
data: Readable,
mediaType: string,
callback: () => void
) => void
export type ICreateAttachment = ICreateStringAttachment &
ICreateBufferAttachment &
ICreateStreamAttachment &
ICreateStreamAttachmentWithCallback
export type ICreateLog = (text: string) => void

export default class AttachmentManager {
private readonly onAttachment: IAttachFunction
Expand Down
21 changes: 21 additions & 0 deletions test-d/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { After } from '../'
import { expectError, expectType } from 'tsd'
import { PassThrough } from 'stream'

After(async function () {
// log
expectType<void>(this.log('things'))
// string
expectType<void>(this.attach('stuff'))
expectType<void>(this.attach('{}', 'application/json'))
// buffer
expectType<void>(this.attach(Buffer.from('{}'), 'application/json'))
// stream
expectType<Promise<void>>(this.attach(new PassThrough(), 'application/json'))
expectType<void>(
this.attach(new PassThrough(), 'application/json', () => undefined)
)
// buffer and stream flavours must specify media type
expectError(this.attach(Buffer.from('{}')))
expectError(this.attach(new PassThrough()))
})

0 comments on commit 35d6743

Please sign in to comment.