Skip to content

Commit

Permalink
feat: add ability to override NODE_ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Oct 17, 2024
1 parent 9d45756 commit 02a5708
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ See the [V4 Migration Guide](./MIGRATION.md) if you are migrating from v3 or old

- `print` - Print everything that goes to stdout and stderr.
- `stripAnsi` - Strip ansi codes from everything that goes to stdout and stderr. Defaults to true.
- `testNodeEnv` - Sets the `NODE_ENV` value when capturing output. Defaults to `'test'`.

See the [tests](./test/capture-output.test.ts) for example usage.

Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const debug = makeDebug('oclif-test')
type CaptureOptions = {
print?: boolean
stripAnsi?: boolean
testNodeEnv?: string
}

type CaptureResult<T> = {
Expand Down Expand Up @@ -77,6 +78,7 @@ function splitString(str: string): string[] {
export async function captureOutput<T>(fn: () => Promise<unknown>, opts?: CaptureOptions): Promise<CaptureResult<T>> {
const print = opts?.print ?? false
const stripAnsi = opts?.stripAnsi ?? true
const testNodeEnv = opts?.testNodeEnv || 'test'

const originals = {
NODE_ENV: process.env.NODE_ENV,
Expand Down Expand Up @@ -112,7 +114,7 @@ export async function captureOutput<T>(fn: () => Promise<unknown>, opts?: Captur

process.stdout.write = mock('stdout')
process.stderr.write = mock('stderr')
process.env.NODE_ENV = 'test'
process.env.NODE_ENV = testNodeEnv

try {
const result = await fn()
Expand Down

0 comments on commit 02a5708

Please sign in to comment.