chore: mock os.networkInterfaces

This commit is contained in:
Igor Randjelovic
2021-03-05 18:28:00 +01:00
parent aa0daba6a5
commit 39d90d5c32
3 changed files with 47 additions and 11 deletions

View File

@@ -14,6 +14,38 @@ jest.mock('cosmiconfig', () => ({
}, },
})); }));
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', () => { jest.mock('path', () => {
const path = jest.requireActual('path'); const path = jest.requireActual('path');
return { return {

View File

@@ -1,12 +1,6 @@
import os from 'os'; import os from 'os';
export function getIPS() { export function getIPS() {
// todo: perhaps mock networkInterfaces instead?
if (__TEST__) {
// in tests we don't need the real ips
return ['127.0.0.1', '192.168.0.10'];
}
const interfaces = os.networkInterfaces(); const interfaces = os.networkInterfaces();
return Object.keys(interfaces) return Object.keys(interfaces)
.map((name) => { .map((name) => {

View File

@@ -1,15 +1,17 @@
import { merge } from 'webpack-merge'; import { merge } from 'webpack-merge';
import { addVirtualEntry, addVirtualModule } from './virtualModules'
import { getPackageJson, getProjectRootPath } from './project';
import { addCopyRule, removeCopyRule } from './copyRules'; import { addCopyRule, removeCopyRule } from './copyRules';
import { determineProjectFlavor } from './flavor'; import { determineProjectFlavor } from './flavor';
import { error, info, warn } from './log'; import { error, info, warn } from './log';
import { getValue } from './config'; import { getValue } from './config';
import { getIPS } from './host'
import { import {
getAllDependencies, getAllDependencies,
hasDependency, hasDependency,
getDependencyPath, getDependencyPath,
} from './dependencies'; } from './dependencies';
import { getPackageJson, getProjectRootPath } from './project';
import { import {
addPlatform, addPlatform,
getAbsoluteDistPath, getAbsoluteDistPath,
@@ -20,6 +22,7 @@ import {
getPlatformName, getPlatformName,
} from './platform'; } from './platform';
// intentionally populated manually // intentionally populated manually
// as this generates nicer typings // as this generates nicer typings
// that show all the utils inline // that show all the utils inline
@@ -40,15 +43,14 @@ export default {
flavor: { flavor: {
determineProjectFlavor, determineProjectFlavor,
}, },
host: {
getIPS,
},
log: { log: {
error, error,
info, info,
warn, warn,
}, },
project: {
getProjectRootPath,
getPackageJson,
},
platform: { platform: {
addPlatform, addPlatform,
getAbsoluteDistPath, getAbsoluteDistPath,
@@ -58,4 +60,12 @@ export default {
getPlatform, getPlatform,
getPlatformName, getPlatformName,
}, },
project: {
getProjectRootPath,
getPackageJson,
},
virtualModules: {
addVirtualEntry,
addVirtualModule
}
}; };