Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: mock os.#networkInterfaces #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions source/test.ts
Expand Up @@ -3,18 +3,34 @@ import kava from 'kava'
import { equal } from 'assert-helpers'
import getMAC, { isMAC } from './index.js'

function mockNetworkInterfaces(mac: string) {
const os = require('os')
const actual = os.networkInterfaces
os.networkInterfaces = function () {
return {
en0: [
{
mac,
},
],
}
}
const restore = () => (os.networkInterfaces = actual)
return restore
}

// Test
kava.suite('getMAC', function (suite, test) {
suite('validation', function (suite, test) {
test('validates dashed mac correctly', function () {
test('validates coloned mac correctly', function () {
equal(isMAC('e4:ce:8f:5b:a7:fe'), true)
})

test('validated capital mac correctly', function () {
equal(isMAC('00:28:31:8A:41:C6'), true)
})

test('validates coloned mac correctly', function () {
test('validates dashed mac correctly', function () {
equal(isMAC('e4-ce-8f-5b-a7-fe'), true)
})

Expand Down Expand Up @@ -45,4 +61,12 @@ kava.suite('getMAC', function (suite, test) {
equal(typeof macAddress, 'string', `${macAddress} is string`)
equal(isMAC(macAddress), true, `${macAddress} is mac address`)
})

test('returns mac', function () {
const mockedMacAddress = '01-23-45-67-89-AB-CD-EF'
const restore = mockNetworkInterfaces(mockedMacAddress)
const macAddress = getMAC()
equal(macAddress, mockedMacAddress)
restore()
})
})