Repository navigation
Expand file tree
/
Copy pathjest.setup.js
More file actions
81 lines (67 loc) · 2.28 KB
/
Copy pathjest.setup.js
File metadata and controls
81 lines (67 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* eslint-disable global-require */
// eslint-disable-next-line no-unused-vars
/* global jest, beforeAll, afterAll, describe */
// Ensure NODE_ENV is set for babel-preset-react-app before any modules load
process.env.NODE_ENV = process.env.NODE_ENV || 'test';
// Add any global setup for Jest tests here
// Use require (not static import) so NODE_ENV is already set when core-js loads
require('core-js/stable');
// Setup Intl polyfill
global.IntlPolyfill = require('intl');
require('intl/locale-data/jsonp/en.js');
require('intl-pluralrules');
// Setup i18next
const i18next = require('i18next');
const _ = require('lodash');
if (_.get(i18next, 'options.returnEmptyString') === undefined) {
i18next.init({
returnEmptyString: false,
nsSeparator: '|',
interpolation: { escapeValue: false },
});
}
// Setup Jest DOM matchers
require('@testing-library/jest-dom');
// Polyfill webpack's require.context for Jest
if (typeof require.context === 'undefined') {
const fs = require('node:fs');
const path = require('path');
require.context = function (base, deep, filter) {
const baseDir = path.resolve(__dirname, base.replace(/^\//, ''));
const files = [];
function readDir(dir) {
if (!fs.existsSync(dir)) return;
fs.readdirSync(dir).forEach((file) => {
const fullPath = path.join(dir, file);
if (fs.statSync(fullPath).isDirectory() && deep) {
readDir(fullPath);
} else {
const relativePath = `./${path.relative(baseDir, fullPath).replace(/\\/g, '/')}`;
if (filter.test(relativePath)) {
files.push(relativePath);
}
}
});
}
readDir(baseDir);
const context = function (key) {
return require(path.join(baseDir, key));
};
context.keys = function () {
return files;
};
return context;
};
}
// Make chai and sinon available globally (matching Karma/Mocha globals)
global.chai = require('chai');
global.sinon = require('sinon');
global.assert = global.chai.assert;
global.expect = global.chai.expect;
// Map Mocha lifecycle hooks to Jest equivalents
global.before = beforeAll;
global.after = afterAll;
global.context = describe;
// Define global variables that might be expected by app code
// eslint-disable-next-line no-underscore-dangle
global.__DEV__ = true;