Repository: bahmutov/cypress-map
Pull request #49 (2023-04-13 21:18:23)

feat: add cy.asEnv query command
### asEnv

Saves current subject in `Cypress.env` object. Note: Cypress.env object is reset before the spec run, but the changed values are passed from test to test. Thus you can easily pass a value from the first test to the second.

```js
it('saves value in this test', () => {
  cy.wrap('hello, world').asEnv('greeting')
})

it('saved value is available in this test', () => {
  expect(Cypress.env('greeting'), 'greeting').to.equal('hello, world')
})
```

Do you really want to make the tests dependent on each other?


Patch:
diff --git a/README.md b/README.md
index 9ab105b..633edca 100644
--- a/README.md
+++ b/README.md
@@ -243,6 +243,22 @@ cy.get('#items li').at(-1).its('innerText').should('equal', 'fifth')
 
 See [at.cy.js](./cypress/e2e/at.cy.js)
 
+### asEnv
+
+Saves current subject in `Cypress.env` object. Note: Cypress.env object is reset before the spec run, but the changed values are passed from test to test. Thus you can easily pass a value from the first test to the second.
+
+```js
+it('saves value in this test', () => {
+  cy.wrap('hello, world').asEnv('greeting')
+})
+
+it('saved value is available in this test', () => {
+  expect(Cypress.env('greeting'), 'greeting').to.equal('hello, world')
+})
+```
+
+Do you really want to make the tests dependent on each other?
+
 ### table
 
 📝 to learn more about `cy.table` command, read the blog post [Test HTML Tables Using cy.table Query Command](https://glebbahmutov.com/blog/cy-table/).
diff --git a/commands/as-env.js b/commands/as-env.js
new file mode 100644
index 0000000..32104f4
--- /dev/null
+++ b/commands/as-env.js
@@ -0,0 +1,22 @@
+/// <reference types="cypress" />
+
+const { registerQuery } = require('./utils')
+
+registerQuery('asEnv', (name) => {
+  if (typeof name !== 'string') {
+    throw new Error(`Invalid cy.asEnv name ${index}`)
+  }
+  if (!name) {
+    throw new Error(`Empty cy.asEnv name`)
+  }
+
+  const log = Cypress.log({ name: 'asEnv', message: name })
+
+  return (subject) => {
+    if (Cypress._.isNil(subject)) {
+      throw new Error('No subject to save cy.asEnv')
+    }
+    Cypress.env(name, subject)
+    return subject
+  }
+})
diff --git a/commands/index.d.ts b/commands/index.d.ts
index 0dfdfa6..89ce5cb 100644
--- a/commands/index.d.ts
+++ b/commands/index.d.ts
@@ -204,5 +204,15 @@ declare namespace Cypress {
      *    .should('deep.include', { ... })
      */
     toPlainObject(): Chainable<Object>
+
+    /**
+     * Saves current subject in `Cypress.env` object.
+     * Note: Cypress.env object is reset before the spec run,
+     * but the changed values are passed from test to test.
+     * @see https://github.com/bahmutov/cypress-map
+     * @example
+     *  cy.wrap('hello').asEnv('greeting')
+     */
+    asEnv(name: string): Chainable<any>
   }
 }
diff --git a/commands/index.js b/commands/index.js
index 66e1951..c00be25 100644
--- a/commands/index.js
+++ b/commands/index.js
@@ -17,3 +17,4 @@ require('./partial')
 require('./map-chain')
 require('./find-one')
 require('./to-plain-object')
+require('./as-env')


Test patch:
diff --git a/cypress/e2e/as-env.cy.js b/cypress/e2e/as-env.cy.js
new file mode 100644
index 0000000..a6687a1
--- /dev/null
+++ b/cypress/e2e/as-env.cy.js
@@ -0,0 +1,41 @@
+/// <reference types="cypress" />
+// @ts-check
+
+import '../../commands'
+
+it('does not have saved value yet', () => {
+  expect(Cypress.env('greeting'), 'greeting is undefined').to.be
+    .undefined
+})
+
+it('saves value in this test', () => {
+  cy.wrap('hello, world')
+    .asEnv('greeting')
+    // the value is yielded to the next command
+    .should('equal', 'hello, world')
+})
+
+it('saved value is available in this test', () => {
+  expect(Cypress.env('greeting'), 'greeting').to.equal('hello, world')
+})
+
+it('saved value is available in this test too', () => {
+  expect(Cypress.env('greeting'), 'greeting').to.equal('hello, world')
+})
+
+it('retries like all queries', () => {
+  const person = {}
+  setTimeout(() => {
+    person.name = 'Joe'
+  }, 1000)
+  cy.wrap(person)
+    .asEnv('person')
+    .its('name')
+    .should('equal', 'Joe')
+    .then(() => {
+      expect(Cypress.env('person'), 'person').to.have.property(
+        'name',
+        'Joe',
+      )
+    })
+})