trivikr / vitest-codemod

Codemod scripts to migrate your JavaScript unit tests to vitest
MIT License
23 stars 2 forks source link

[Feature]: Add transformation for replaceProperty API for process.env #126

Open trivikr opened 1 year ago

trivikr commented 1 year ago

Self-service

Template name

jest

Input code

test("replaceProperty", () => {
  expect(process.env.TEST_ENV_KEY_1).toBeUndefined();
  expect(process.env.TEST_ENV_KEY_2).toBeUndefined();

  jest.replaceProperty(process, 'env', {
    TEST_ENV_KEY_1: "test1",
    TEST_ENV_KEY_2: "test2"
  });  
  expect(process.env.TEST_ENV_KEY_1).toBe("test1");
  expect(process.env.TEST_ENV_KEY_2).toBe("test2");
  jest.restoreAllMocks();

  expect(process.env.TEST_ENV_KEY_1).toBeUndefined();
  expect(process.env.TEST_ENV_KEY_2).toBeUndefined();
});

Expected Output

test("replaceProperty", () => {
  expect(process.env.TEST_ENV_KEY_1).toBeUndefined();
  expect(process.env.TEST_ENV_KEY_2).toBeUndefined();

  vi.stubEnv("TEST_ENV_KEY_1", "test1");
  vi.stubEnv("TEST_ENV_KEY_2", "test2");
  expect(process.env.TEST_ENV_KEY_1).toBe("test1");
  expect(process.env.TEST_ENV_KEY_2).toBe("test2");
  vi.restoreAllMocks();
  vi.unstubAllEnvs();

  expect(process.env.TEST_ENV_KEY_1).toBeUndefined();
  expect(process.env.TEST_ENV_KEY_2).toBeUndefined();
});

Additional context

Refs: https://github.com/vitest-dev/vitest/issues/2831