Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to override NODE_ENV #638

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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