-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Comments
Run |
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 👍 |
I don't think this is possible to do dynamically within Vitest. Please, use |
Relying on the TZ env var to be set like this on the command line (or by the We tried setting the TZ env var in a However, we did find a working solution by setting the TZ env var in a 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',
},
})) |
I'd also like to add that using |
You can pass down env variables with export default {
test: {
env: {
TZ: 'UTC'
}
}
} |
This didn't work for me. I set the following in 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')
})
}) |
@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.
|
@mikeybinns I was responding to this comment, which says you should be able to set it in the config |
@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 |
No, I tried this approach and it worked. |
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.
or globally:
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.
or
Validations
The text was updated successfully, but these errors were encountered: