Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"ci-info": "^4.0.0",
"debug": "^4.1.1",
"inquirer": "^8.2.1",
"node-fetch": "^2.6.7",
"os-name": "^4.0.1",
"splunk-logging": "^0.11.1"
},
Expand All @@ -28,7 +27,6 @@
"eslint-plugin-standard": "^4.0.1",
"execa": "^4.0.2",
"jest": "^29",
"jest-fetch-mock": "^3.0.0",
"jest-junit": "^13.0.0",
"memfs": "^4.6.0",
"oclif": "^4.3.6",
Expand Down
1 change: 0 additions & 1 deletion src/telemetry-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

const fetch = require('node-fetch')
const config = require('@adobe/aio-lib-core-config')
const osName = require('os-name')
const inquirer = require('inquirer')
Expand Down
3 changes: 1 addition & 2 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* governing permissions and limitations under the License.
*/

const fetch = require('node-fetch')
const inquirer = require('inquirer')
const config = require('@adobe/aio-lib-core-config')

Expand All @@ -28,7 +27,7 @@ const mockPackageJson = {

describe('hook interfaces', () => {
beforeEach(() => {
fetch.mockReset()
global.setFetchMock()
})

test('command-error', async () => {
Expand Down
4 changes: 1 addition & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* governing permissions and limitations under the License.
*/

const fetch = require('node-fetch')
const TheCommand = require('../src/commands/telemetry')
const { stdout } = require('stdout-stderr')

Expand All @@ -19,9 +18,8 @@ jest.mock('inquirer')
let command

beforeEach(() => {
fetch.resetMocks()
command = new TheCommand([])
fetch.mockResponseOnce('ok')
global.setFetchMock()
})

test('exports a run function', async () => {
Expand Down
8 changes: 6 additions & 2 deletions test/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ const { vol } = require('memfs')
jest.setTimeout(3000)
jest.useFakeTimers()

const fetch = require('jest-fetch-mock')
jest.setMock('node-fetch', fetch)
global.setFetchMock = (ok = true, mockData = {}) => {
global.fetch = jest.fn().mockResolvedValue({
ok,
json: () => ok ? Promise.resolve(mockData) : Promise.reject(mockData)
})
}

vol.reset()

Expand Down
27 changes: 24 additions & 3 deletions test/telemetry-lib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
* governing permissions and limitations under the License.
*/

const fetch = require('node-fetch')
const telemetryLib = require('../src/telemetry-lib')
const config = require('@adobe/aio-lib-core-config')

jest.mock('@adobe/aio-lib-core-config')

jest.mock('debug', () => {
global.mockDebug = jest.fn()
return () => global.mockDebug
})
describe('telemetry-lib', () => {
beforeEach(() => {
jest.resetModules()
fetch.mockReset()
global.mockDebug.mockReset()
global.setFetchMock()
})

test('exports messages', async () => {
Expand All @@ -43,10 +46,28 @@ describe('telemetry-lib', () => {
test('uses client id from config', async () => {
config.get.mockReturnValue('clientidxyz')
telemetryLib.init('a@4', 'binTest2')

await telemetryLib.trackEvent('test-event')

expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.clientId')
expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.optOut', 'global')
expect(fetch).toHaveBeenCalledWith(expect.any(String),
expect.objectContaining({ body: expect.stringContaining('"clientId":"clientidxyz"') }))
expect(global.mockDebug).not.toHaveBeenCalledWith(expect.stringContaining('error reaching telemetry server'))
})

test('fetch error', async () => {
const error = 'some error'
global.fetch = jest.fn().mockRejectedValue(error)

config.get.mockReturnValue('clientidxyz')
telemetryLib.init('a@4', 'binTest2')
await telemetryLib.trackEvent('test-event')

expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.clientId')
expect(config.get).toHaveBeenCalledWith('binTest2-cli-telemetry.optOut', 'global')
expect(fetch).toHaveBeenCalledWith(expect.any(String),
expect.objectContaining({ body: expect.stringContaining('"clientId":"clientidxyz"') }))
expect(global.mockDebug).toHaveBeenCalledWith(expect.stringContaining('error reaching telemetry server'), error)
})
})