From 39d90d5c3268b9341081ea5dcd49a5d7b80392b6 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Fri, 5 Mar 2021 18:28:00 +0100 Subject: [PATCH] chore: mock os.networkInterfaces --- packages/webpack5/scripts/jest.setup.ts | 32 +++++++++++++++++++++++++ packages/webpack5/src/helpers/host.ts | 6 ----- packages/webpack5/src/helpers/index.ts | 20 ++++++++++++---- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/packages/webpack5/scripts/jest.setup.ts b/packages/webpack5/scripts/jest.setup.ts index 02958e353..ce12f9fe2 100644 --- a/packages/webpack5/scripts/jest.setup.ts +++ b/packages/webpack5/scripts/jest.setup.ts @@ -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', () => { const path = jest.requireActual('path'); return { diff --git a/packages/webpack5/src/helpers/host.ts b/packages/webpack5/src/helpers/host.ts index aaf55ffee..5a909a196 100644 --- a/packages/webpack5/src/helpers/host.ts +++ b/packages/webpack5/src/helpers/host.ts @@ -1,12 +1,6 @@ import os from 'os'; 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(); return Object.keys(interfaces) .map((name) => { diff --git a/packages/webpack5/src/helpers/index.ts b/packages/webpack5/src/helpers/index.ts index 11b772059..63d9cd9a4 100644 --- a/packages/webpack5/src/helpers/index.ts +++ b/packages/webpack5/src/helpers/index.ts @@ -1,15 +1,17 @@ import { merge } from 'webpack-merge'; +import { addVirtualEntry, addVirtualModule } from './virtualModules' +import { getPackageJson, getProjectRootPath } from './project'; import { addCopyRule, removeCopyRule } from './copyRules'; import { determineProjectFlavor } from './flavor'; import { error, info, warn } from './log'; import { getValue } from './config'; +import { getIPS } from './host' import { getAllDependencies, hasDependency, getDependencyPath, } from './dependencies'; -import { getPackageJson, getProjectRootPath } from './project'; import { addPlatform, getAbsoluteDistPath, @@ -20,6 +22,7 @@ import { getPlatformName, } from './platform'; + // intentionally populated manually // as this generates nicer typings // that show all the utils inline @@ -40,15 +43,14 @@ export default { flavor: { determineProjectFlavor, }, + host: { + getIPS, + }, log: { error, info, warn, }, - project: { - getProjectRootPath, - getPackageJson, - }, platform: { addPlatform, getAbsoluteDistPath, @@ -58,4 +60,12 @@ export default { getPlatform, getPlatformName, }, + project: { + getProjectRootPath, + getPackageJson, + }, + virtualModules: { + addVirtualEntry, + addVirtualModule + } };