From 7b249c1956bc7ddc35362e5f841dd3fe77362b41 Mon Sep 17 00:00:00 2001 From: Tierney Cyren Date: Wed, 6 Nov 2024 12:25:48 +0000 Subject: [PATCH] test: add tests for refactored code Signed-off-by: Tierney Cyren --- core/test/combos.js | 7 +++++ core/test/parseOptions.js | 55 +++++++++++++++++++++++++++++++++++ core/test/schedule.js | 52 +++++++++++++++++++++++++++++++++ core/test/unique-source.js | 14 ++++----- core/test/versions.js | 59 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 core/test/parseOptions.js create mode 100644 core/test/schedule.js create mode 100644 core/test/versions.js diff --git a/core/test/combos.js b/core/test/combos.js index 18d4bf26..30429f69 100644 --- a/core/test/combos.js +++ b/core/test/combos.js @@ -1,6 +1,7 @@ const assert = require('node:assert'); const nodevu = require('../index'); const { describe, it, beforeEach } = require('test'); +const { fetch: undiciFetch } = require('undici') const beforeEachTemplate = require('../util/dev/beforeEachTemplate'); @@ -31,6 +32,12 @@ const now = JSON.parse(staticNow); describe('check to make sure that combining options works as expected', async () => { beforeEach(beforeEachTemplate); + it('should work with a different fetch', async () => { + const staticData = await nodevu({ undiciFetch }); + + check(staticData); + }); + it('should still return valid data when the index is a different URL from the default while also using static now', async () => { const urls = { index: 'https://bnb.im/dist/index.json', diff --git a/core/test/parseOptions.js b/core/test/parseOptions.js new file mode 100644 index 00000000..8b3b3335 --- /dev/null +++ b/core/test/parseOptions.js @@ -0,0 +1,55 @@ +const { deepStrictEqual } = require('node:assert'); +const { describe, it, beforeEach } = require('test'); +const { fetch: undiciFetch } = require('undici') +const { DateTime } = require('luxon') +const nodevu = require('../index'); +const parseOptions = require('../util/prod/optionsParser') + +describe('the parseOptions module should return all correct defaults', async () => { + it('should return the default date', async () => { + const now = DateTime.now() + const defaultParsedOptions = parseOptions({}); + deepStrictEqual(defaultParsedOptions.now.day, now.day) + deepStrictEqual(defaultParsedOptions.now.hour, now.hour) + deepStrictEqual(defaultParsedOptions.now.minute, now.minute) + deepStrictEqual(defaultParsedOptions.now.month, now.month) + }) + + it('defaultParsedOptions.fetch should be globalThis.fetch when no options are passed', async () => { + const defaultParsedOptions = parseOptions({}); + deepStrictEqual(defaultParsedOptions.fetch, globalThis.fetch) + }) + + it('should return the origin index.json for url.index', async () => { + const defaultParsedOptions = parseOptions({}); + deepStrictEqual(defaultParsedOptions.urls.index, 'https://nodejs.org/dist/index.json') + }) + + it('should return the origin schedule.json for url.schedule', async () => { + const defaultParsedOptions = parseOptions({}); + deepStrictEqual(defaultParsedOptions.urls.schedule, 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json') + }) +}) + +describe('the parseOptions module should still work when defaults are changed', async () => { + it('should still work when a custom date is passed', async () => { + const currentNow = DateTime.now() + const defaultParsedOptions = parseOptions({ now: currentNow }); + deepStrictEqual(defaultParsedOptions.now, currentNow) + }) + + it('defaultParsedOptions.fetch should be globalThis.fetch when no options are passed', async () => { + const defaultParsedOptions = parseOptions({ fetch: undiciFetch }); + deepStrictEqual(defaultParsedOptions.fetch, undiciFetch) + }) + + it('should return the origin index.json for url.index', async () => { + const defaultParsedOptions = parseOptions({ urls: { index: 'https://example.com'}}); + deepStrictEqual(defaultParsedOptions.urls.index, 'https://example.com') + }) + + it('should return the origin schedule.json for url.schedule', async () => { + const defaultParsedOptions = parseOptions({ urls: { schedule: 'https://example.com'}}); + deepStrictEqual(defaultParsedOptions.urls.schedule, 'https://example.com') + }) +}) \ No newline at end of file diff --git a/core/test/schedule.js b/core/test/schedule.js new file mode 100644 index 00000000..d505601b --- /dev/null +++ b/core/test/schedule.js @@ -0,0 +1,52 @@ +const { deepStrictEqual } = require('node:assert'); +const { describe, it } = require('test'); +const { fetch: undiciFetch } = require('undici') +const { DateTime } = require('luxon'); +const nodevu = require('../index'); +const schedule = require('../util/prod/schedule') +const optionsParser = require('../util/prod/optionsParser') + +// checks that verify the result of data returned +function check (data) { + for (const [line, metadata] of Object.entries(data)) { + for (const [metadataKey, metadataValue] of Object.entries(metadata)) { + deepStrictEqual(typeof metadataKey, 'string') + deepStrictEqual(typeof metadataValue, 'string') + } + } +} + +// set up options object that would normally be passed to the module +const options = { + fetch: globalThis.fetch, + urls: { + schedule: 'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json' + } +} + +describe('under normal condiditons, schedule should work', async () => { + it('should work with default options', async () => { + const data = await schedule(options) + check(data) + }) + + it('should work with Undici fetch', async () => { + options.fetch = undiciFetch + const data = await schedule(options) + check(data) + }) +}) + +describe('schedule should work with optionsParser', async () => { + it('should work with the default output of optionsParser', async () => { + const parsedOptions = optionsParser({}) + const data = await schedule(parsedOptions) + }) + + it('should work with a different fetch pased to optionsParser', async () => { + const parsedOptions = optionsParser({ + fetch: undiciFetch + }) + const data = await schedule(parsedOptions) + }) +}) \ No newline at end of file diff --git a/core/test/unique-source.js b/core/test/unique-source.js index 9a6e7908..023d2453 100644 --- a/core/test/unique-source.js +++ b/core/test/unique-source.js @@ -1,4 +1,4 @@ -const assert = require('node:assert'); +const { deepStrictEqual } = require('node:assert'); const nodevu = require('../index'); const { describe, it, beforeEach } = require('test'); @@ -13,12 +13,12 @@ describe('check to make sure that changing sources works as expected', async () index: 'https://bnb.im/dist/index.json', }; const staticData = await nodevu({ urls }); - assert.deepStrictEqual( + deepStrictEqual( staticData.v17.releases['v17.0.0'].dependencies.npm, '8.1.0', ); - assert.deepStrictEqual(staticData.v14.support.codename, 'Fermium'); - assert.deepStrictEqual(staticData.v8.support.phases.current, 'end'); + deepStrictEqual(staticData.v14.support.codename, 'Fermium'); + deepStrictEqual(staticData.v8.support.phases.current, 'end'); }); // failing @@ -27,11 +27,11 @@ describe('check to make sure that changing sources works as expected', async () schedule: 'https://bnb.im/dist/schedule.json', }; const staticData = await nodevu({ urls }); - assert.deepStrictEqual( + deepStrictEqual( staticData.v17.releases['v17.0.0'].dependencies.v8, '9.5.172.21', ); - assert.deepStrictEqual(staticData.v14.support.lts.newest, '14.21.3'); - assert.deepStrictEqual(staticData.v9.support.phases.current, 'end'); + deepStrictEqual(staticData.v14.support.lts.newest, '14.21.3'); + deepStrictEqual(staticData.v9.support.phases.current, 'end'); }); }); diff --git a/core/test/versions.js b/core/test/versions.js new file mode 100644 index 00000000..d6a9437e --- /dev/null +++ b/core/test/versions.js @@ -0,0 +1,59 @@ +const { deepStrictEqual } = require('node:assert'); +const { describe, it } = require('test'); +const { fetch: undiciFetch } = require('undici') +const { DateTime } = require('luxon'); +const nodevu = require('../index'); +const versions = require('../util/prod/versions') +const optionsParser = require('../util/prod/optionsParser') + +// checks that verify the result of data returned +function check (data) { + deepStrictEqual(typeof data[0].version, 'string') + deepStrictEqual(typeof data[0].date, 'string') + deepStrictEqual(Array.isArray(data[0].files), true) + deepStrictEqual(typeof data[0].npm, 'string') + deepStrictEqual(typeof data[0].v8, 'string') + deepStrictEqual(typeof data[0].uv, 'string') + deepStrictEqual(typeof data[0].zlib, 'string') + deepStrictEqual(typeof data[0].openssl, 'string') + deepStrictEqual(typeof data[0].modules, 'string') + deepStrictEqual(typeof data[0].lts, 'boolean') + deepStrictEqual(typeof data[0].security, 'boolean') +} + +// set up options object that would normally be passed to the module +const options = { + fetch: globalThis.fetch, + urls: { + index: 'https://nodejs.org/dist/index.json' + } +} + +describe('under normal condiditons, versions should work', async () => { + it('should work with default options', async () => { + const data = await versions(options) + check (data) + }) + + it('should work with Undici fetch', async () => { + options.fetch = undiciFetch + const data = await versions(options) + check(data) + }) +}) + +describe('versions should work with optionsParser', async () => { + it('should work with the default output of optionsParser', async () => { + const parsedOptions = optionsParser({}) + const data = await versions(parsedOptions) + check(data) + }) + + it('should work with a different fetch pased to optionsParser', async () => { + const parsedOptions = optionsParser({ + fetch: undiciFetch + }) + const data = await versions(parsedOptions) + check(data) + }) +}) \ No newline at end of file