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

test(@nestjs/config) reset env correctly #1665

Merged

Conversation

bejewel-kyoungmin
Copy link
Contributor

@bejewel-kyoungmin bejewel-kyoungmin commented Mar 22, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

I made a similar test file in config repo simply.

describe('Test Case', () => {
  let envBackup;
  beforeAll(() => {
    envBackup = process.env;
  });

  describe('Test Case 1', () => {
    it('run test 1', () => {
      process.env['a'] = "8000";
      expect(process.env.a).toBe("8000");
    });
  });

  describe('Test Case 2', () => {
    it('run test 2', () => {
      expect(process.env.a).toBe("8000");
    });
  });

  afterEach(() => {
    process.env = envBackup;
  });
});

In this case we expect run test 2 to be failed. We guess the process.env will be reset in afterEach.
but it succeed.

Because envBackup variable is reference of process.env. If you change either envBackup or process.env, both of them will be updated. It doesn't be reset.
The first test case affects second test case. in second test case process.env.a is not undefined.

What is the new behavior?

Fortunately test cases in config repo have developed independently. All test cases pass.
But I wanna remove a potential risk.
I changed they don't refer each other.
And I hope to add more test cases after merging. : )

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

process.env does not reset correctly.
assgin copy value of origin process.env in beforeAll
and reset process.env with value of envBackup
@kamilmysliwiec kamilmysliwiec merged commit 6b68c92 into nestjs:master Mar 22, 2024
1 check passed
@kamilmysliwiec
Copy link
Member

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants