-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathjest.setup.ts
More file actions
100 lines (89 loc) · 1.89 KB
/
jest.setup.ts
File metadata and controls
100 lines (89 loc) · 1.89 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// we are mocking the cwd for the tests, since webpack needs absolute paths
// and we don't want them in tests
process.cwd = () => '__jest__';
jest.mock('cosmiconfig', () => ({
cosmiconfigSync(moduleName) {
return {
search() {
// no-op in tests
return null;
},
};
},
}));
const getValueMock = jest.fn();
getValueMock.mockImplementation((key, defaultValue) => defaultValue);
jest.mock('../src/helpers/config.ts', () => ({
getValue: getValueMock,
}));
jest.mock('os', () => {
const os = jest.requireActual('os');
return {
...os,
networkInterfaces() {
return {
in0: [
{
address: '127.0.0.1',
family: 'IPv4',
},
{
address: 'in0-ipv6-should-not-use',
family: 'IPv6',
},
],
in1: [
{
address: '192.168.0.10',
family: 'IPv4',
},
{
address: 'in1-ipv6-should-not-use',
family: 'IPv6',
},
],
};
},
};
});
jest.mock('path', () => {
const path = jest.requireActual('path');
return {
...path,
resolve(...args) {
if (args[0] === '__jest__') {
return path.join(...args.filter(Boolean));
}
const resolved = path.resolve(...args);
if (resolved.includes('__jest__')) {
const li = resolved.lastIndexOf('__jest__');
return resolved.substr(li);
}
// handle resolutions with __dirname
// used in base config's resolveLoader
const root = path.resolve(__dirname, '..');
if (resolved.startsWith(root)) {
const newPath = resolved.replace(root, '__jest__');
if (newPath.startsWith('__jest__/src')) {
return newPath.replace(
'__jest__/src',
'__jest__/node_modules/@nativescript/webpack/dist'
);
}
return newPath;
}
return resolved;
},
};
});
// a virtual mock for package.json
jest.mock(
'__jest__/package.json',
() => ({
main: 'src/app.js',
devDependencies: {
typescript: '*',
},
}),
{ virtual: true }
);