Reputation: 51
I would like to be able to set a timezone for all tests to run in globally as outlined in this post. But still setup my jest.config.ts to leverage, either directly or indirectly, jest-preset-angular/global-setup
.
It seems there is only one place in the jest.config.ts to set a globalSetup
.
This is what I have in my jest-global-setup.js file, which I currently have referenced in my globalSetup
but I don't believe this is correct:
module.exports = async () => {
process.env.TZ = 'UTC';
return require('jest-preset-angular/global-setup');
};
Upvotes: 3
Views: 519
Reputation: 4318
Let me share my jest.config.ts
file with some comments and workarounds that solve some of the common issues many people can encounter nowadays. Note the setupFilesAfterEnv
where you can put all your global mocks.
import presets from 'jest-preset-angular/presets';
import type { Config } from 'jest';
import { pathsToModuleNameMapper } from 'ts-jest';
import fs from 'fs';
const tsConfig = JSON.parse(fs.readFileSync('./tsconfig.json', 'utf-8'));
const esModules = ['ip-cidr', 'ip-address', 'camelcase', 'is-ip', 'ip-regex', 'super-regex', 'function-timeout', 'time-span', 'convert-hrtime', 'clone-regexp', 'is-regexp', '.*\\.mjs$'].join('|');
const jestConfig: Config = {
// This is where you import a preset
...presets.createCjsPreset({
}),
globals: {
ngJest: {
// Process all files in node_modules with esbuild to always have CJS module
// Typescript is broken here and gives ESM module
processWithEsbuild: ['**/node_modules/**/*.js']
}
},
roots: ['<rootDir>/src/'],
testMatch: ['**/+(*.)+(spec).+(ts)'],
// Still process ES modules that can be found in node_modules folder
transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
setupFilesAfterEnv: ['<rootDir>/src/test.ts'],
// Use local cache directory so CI can easily delete it
cacheDirectory: '<rootDir>/.cache',
// This is needed to support aliases from tsconfig.json
moduleNameMapper: Object.assign(pathsToModuleNameMapper(tsConfig.compilerOptions.paths, {
prefix: '<rootDir>/src/',
}) as any, {
// Replace some files with a simple mock
'\\.(svg|mp3)$': '<rootDir>/__mocks__/fileMock.js',
}),
};
export default jestConfig;
Upvotes: 0