Skip to content

Commit 40fb7bc

Browse files
committed
Add Cypress.Buffer
1 parent ba2d830 commit 40fb7bc

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

cli/types/cypress-eventemitter.d.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ interface NodeEventEmitter {
2828
eventNames(): Array<string | symbol>
2929
}
3030

31-
// The Buffer type is automatically imported for the browser by webpack
32-
// and we use it for dealing with binary data, especially around the
31+
// We use the Buffer class for dealing with binary data, especially around the
3332
// selectFile interface.
34-
type BufferType = import("buffer/").Buffer
33+
type BufferModule = import("buffer/")

cli/types/cypress.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ declare namespace Cypress {
231231
* Cypress.Blob.method()
232232
*/
233233
Blob: BlobUtil.BlobUtilStatic
234+
/**
235+
* Cypress automatically includes a Buffer library and exposes it as Cypress.Buffer.
236+
*
237+
* @see https://on.cypress.io/buffer
238+
* @see https://github.com/feross/buffer
239+
* @example
240+
* Cypress.Blob.method()
241+
*/
242+
Buffer: BufferModule
234243
/**
235244
* Cypress automatically includes minimatch and exposes it as Cypress.minimatch.
236245
*
@@ -668,7 +677,7 @@ declare namespace Cypress {
668677
* @param {FileReference} files - The file(s) to select or drag onto this element.
669678
* @see https://on.cypress.io/selectfile
670679
* @example
671-
* cy.get('input[type=file]').selectFile(Buffer.from('text'))
680+
* cy.get('input[type=file]').selectFile(Cypress.Buffer.from('text'))
672681
* cy.get('input[type=file]').selectFile({
673682
* fileName: 'users.json',
674683
* fileContents: [{name: 'John Doe'}]
@@ -5666,10 +5675,11 @@ declare namespace Cypress {
56665675
stderr: string
56675676
}
56685677

5669-
type FileReference = string | BufferType | FileReferenceObject
5678+
type FileReference = string | BufferModule.Buffer | FileReferenceObject
56705679
interface FileReferenceObject {
56715680
/*
5672-
* Buffers and strings will be used as-is. All other types will have `JSON.stringify()` applied.
5681+
* Buffers will be used as-is, while strings will be interpreted as an alias or a file path.
5682+
* All other types will have `Buffer.from(JSON.stringify())` applied.
56735683
*/
56745684
contents: any
56755685
fileName?: string

packages/driver/cypress/integration/commands/actions/selectFile_spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function getFileContents (subject) {
2020
describe('src/cy/commands/actions/selectFile', () => {
2121
beforeEach(() => {
2222
cy.visit('/fixtures/files-form.html')
23-
cy.wrap(Buffer.from('foo')).as('foo')
23+
cy.wrap(Cypress.Buffer.from('foo')).as('foo')
2424
})
2525

2626
context('#selectFile', () => {
@@ -50,10 +50,10 @@ describe('src/cy/commands/actions/selectFile', () => {
5050
contents: '@foo',
5151
fileName: 'foo.txt',
5252
}, {
53-
contents: Buffer.from('{"a":"bar"}'),
53+
contents: Cypress.Buffer.from('{"a":"bar"}'),
5454
fileName: 'bar.json',
5555
},
56-
Buffer.from('baz'),
56+
Cypress.Buffer.from('baz'),
5757
])
5858

5959
cy.get('#multiple')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('Cypress.Buffer', () => {
2+
it('matches the type returned from readFile', () => {
3+
cy.readFile('cypress/fixtures/app.js', null).then((buffer) => {
4+
expect(Cypress.Buffer.isBuffer(buffer)).to.be.true
5+
})
6+
})
7+
})

packages/driver/src/cypress.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ $Cypress.prototype.SelectorPlayground = $SelectorPlayground
692692
$Cypress.prototype.utils = $utils
693693
$Cypress.prototype._ = _
694694
$Cypress.prototype.Blob = blobUtil
695+
$Cypress.prototype.Buffer = Buffer
695696
$Cypress.prototype.Promise = Promise
696697
$Cypress.prototype.minimatch = minimatch
697698
$Cypress.prototype.sinon = sinon

0 commit comments

Comments
 (0)