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

Add option to define the system timezone for tests #1575

Closed
4 tasks done
mikeybinns opened this issue Jun 30, 2022 · 11 comments
Closed
4 tasks done

Add option to define the system timezone for tests #1575

mikeybinns opened this issue Jun 30, 2022 · 11 comments

Comments

@mikeybinns
Copy link

Clear and concise description of the problem

There is currently no way to set the timezone for a set of tests.
I'd like a way to be able to set the current timezone of the test system for use with the Date function for testing.

Suggested solution

Locally in tests.

beforeEach(() => {
    vi.setTimezone('UTC'); 
    // or
    vi.setTimezone('Europe/London'); 
    // or
    vi.setTimezone('GMT+05');
})

or globally:

export default defineConfig({
	test: {
		timezone:'UTC',
	},
});

Alternative

I've tried setting the timezone via .env and process.env.TZ, both seem to be pretty unreliable.

Additional context

This is an example of a test to run after setting the environment to ensure the timezone is set correctly.

describe("Test Env Checks", () => {
	it("should always be UTC", () => {
		expect(new Date().getTimezoneOffset()).toBe(0);
	});
});

or

describe("Test Env Checks", () => {
	it("should always be GMT+01", () => {
		expect(new Date().getTimezoneOffset()).toBe(-60);
	});
});

Validations

@sheremet-va
Copy link
Member

Run TZ=UTC vitest

@mikeybinns
Copy link
Author

Thanks, that solves for the global basis, would be nice if there was a way to change it for different tests, but I understand that's probably really hard to implement, so happy to call this closed 👍

@sheremet-va
Copy link
Member

I don't think this is possible to do dynamically within Vitest. Please, use TZ=UTC syntax when running Vitest.

@lnhrdt
Copy link

lnhrdt commented Feb 22, 2023

Run TZ=UTC vitest

Relying on the TZ env var to be set like this on the command line (or by the package.json script) didn't work very well for my team. In our development workflow we run our tests in a variety of ways including in our IDEs. While the IDEs pick up the vitest environment configuration just fine, they're not aware of how we've configured the test script in package.json. Getting this configuration right across workstations has been cumbersome for us and we thought if the configuration for this TZ env var could be done centrally (with the rest of our test environment's configuration) that would make much more sense because it would automatically be configured properly in every environment and on every workstation.

We tried setting the TZ env var in a setupFiles script but it didn't work (I suspect that's also what @mikeybinns tried). Trying to set it using process.env.TZ=... or using vi.stubEnv('TZ', '...') seemed to have no affect (i.e. the libraries we use that can be configured with the TZ env var were still defaulting to the system timezone).

However, we did find a working solution by setting the TZ env var in a globalSetup script. We had to use the process.env.TZ=... syntax because when we tried using vi.stubEnv('TZ', '...') the test runner said importing vitest wasn't allowed in globalSetup (nice helpful error message btw).

Here's our final solution in case it helps others:

// src/test-globals.ts
export const setup = () => {
  process.env.TZ = 'US/Eastern'
}
// vitest.config.ts
import {mergeConfig} from 'vite'
import {defineConfig} from 'vitest/config'
import viteConfig from './vite.config'

export default mergeConfig(viteConfig, defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    globalSetup: './src/test-globals.ts',
    setupFiles: './src/test-env.tsx',
  },
}))

@phawxby
Copy link

phawxby commented Apr 24, 2023

I'd also like to add that using TZ=UTC doesn't work well for windows users. Sure, you can use cross-env or some such but I'd rather not rely on a package that's essentially frozen. Being able to specify tz: 'UTC' in the config and have that implement the process above but much more simply would be very helpful.

@sheremet-va
Copy link
Member

Being able to specify tz: 'UTC' in the config and have that implement the process above but much more simply would be very helpful.

You can pass down env variables with test.env config option:

export default {
  test: {
    env: {
      TZ: 'UTC'
    }
  }
}

@donalmurtagh
Copy link

donalmurtagh commented May 5, 2023

You can pass down env variables with test.env config option:

This didn't work for me. I set the following in vite.config.js

export default defineConfig(({ mode }) => {
  const config = {
    test: {
      env: {
        TZ: 'UTC'
      }
    }
  }

  return config
})

Then ran the following test and it failed

describe('date utils tests', () => {

  it('timezone should return UTC', () => {
    // my local timezone 'Europe/Dublin' was returned instead
    expect(process.env.TZ).toBe('UTC')
  })
})

@mikeybinns
Copy link
Author

@donalmurtagh it won't work if you set the timezone in the config because by the time you reach the point where vitest reads it, it's too late.

You must set the env variable before you run the vitest command, e.g.

TZ=UTC vitest

@donalmurtagh
Copy link

@mikeybinns I was responding to this comment, which says you should be able to set it in the config

@mikeybinns
Copy link
Author

@donalmurtagh While they are correct that you can pass env variables this way, this still wouldn't set the correct timezone in vitest for your tests because vitest will already be set up as a process using the default timezone.

I'm guessing Vitest is ignoring your setting of the TZ variable because it's aware that it won't be correct and will only lead to confusion, but I don't know this for sure.

Have you tried setting other ENV variables with a different name? Or accessing the env variables via import.meta.env.VAR_NAME?

@donalmurtagh
Copy link

@mikeybinns

Have you tried setting other ENV variables with a different name? Or accessing the env variables via import.meta.env.VAR_NAME?

No, I tried this approach and it worked.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants