> ## Documentation Index
> Fetch the complete documentation index at: https://momentic.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# javascript

> Run arbitrary JavaScript inside the test runtime.

The simplified form takes a single string of JavaScript. Use `code` for
multi-line scripts. Variables on `env` are available without templating:
`env.MY_VAR` rather than `{{ env.MY_VAR }}`.

## Parameters

| Parameter     | Type                  | Required | Description                                                           |
| ------------- | --------------------- | -------- | --------------------------------------------------------------------- |
| `code`        | `string`              | Yes      | JavaScript source.                                                    |
| `environment` | `"node" \| "browser"` | No       | Where to run the script. Defaults to `node`.                          |
| `fragment`    | `boolean`             | No       | Treat `code` as a fragment (no implicit `return`).                    |
| `timeout`     | `number`              | No       | Maximum time in milliseconds.                                         |
| `saveAs`      | `string`              | No       | Name of the variable to write this step's return value to.            |
| `retries`     | `number`              | No       | Number of times to retry the step on failure before failing the test. |
| `skipped`     | `boolean`             | No       | Skip this step at execution time.                                     |

## Shorthand

JavaScript expression as a single string. `env.NAME` refers to a variable
directly. Do not wrap it in `{{ }}`.

```yaml theme={null}
- javascript: setVariable('USERNAME', 'testuser')
```

## Examples

```yaml theme={null}
- javascript:
    code: return faker.person.fullName()
    saveAs: NAME # available later as env.NAME
```

```yaml theme={null}
- javascript:
    code: return `random-module-name-${Date.now()}`
    saveAs: MODULE_NAME
```

```yaml theme={null}
- javascript:
    code: |-
      assert(env.TODOS.length === 3)
      for (const todo of env.TODOS) {
        assert("request" in todo)
        assert("response" in todo)
      }
```

```yaml theme={null}
- javascript:
    code: |-
      const permission = await navigator.permissions.query({ name: 'camera' })
      if (permission.state !== "granted") {
        throw new Error(`Expected camera permission, got ${permission.state}`)
      }
    environment: browser # run in the page context instead of node
```

## Related

* [Javascript](/integrations/javascript)
* [Variables](/core-concepts/variables)
* [Test format](/core-concepts/test-format)
