Minimum node version v18 (#3170)

When the next MagicMirror version is released, node v16 will have
reached its end of life.
This commit is contained in:
Kristjan ESPERANTO
2023-08-20 12:55:17 +02:00
committed by GitHub
parent 7dcea98e99
commit 7c64d8fce6
8 changed files with 89 additions and 101 deletions

View File

@@ -1,113 +1,107 @@
global.moment = require("moment-timezone");
const { performWebRequest, formatTime } = require("../../../../modules/default/utils");
const nodeVersion = process.version.match(/^v(\d+)\.*/)[1];
describe("Default modules utils tests", () => {
describe("performWebRequest", () => {
if (nodeVersion > 18) {
const locationHost = "localhost:8080";
const locationProtocol = "http";
const locationHost = "localhost:8080";
const locationProtocol = "http";
let fetchResponse;
let fetchMock;
let urlToCall;
let fetchResponse;
let fetchMock;
let urlToCall;
beforeEach(() => {
fetchResponse = new Response();
global.fetch = jest.fn(() => Promise.resolve(fetchResponse));
fetchMock = global.fetch;
beforeEach(() => {
fetchResponse = new Response();
global.fetch = jest.fn(() => Promise.resolve(fetchResponse));
fetchMock = global.fetch;
});
describe("When using cors proxy", () => {
Object.defineProperty(global, "location", {
value: {
host: locationHost,
protocol: locationProtocol
}
});
describe("When using cors proxy", () => {
Object.defineProperty(global, "location", {
value: {
host: locationHost,
protocol: locationProtocol
}
});
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
await performWebRequest(urlToCall, "json", true);
await performWebRequest(urlToCall, "json", true);
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?url=${urlToCall}`);
});
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
await performWebRequest(urlToCall, "json", true, headers);
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?sendheaders=header1:value1,header2:value2&url=${urlToCall}`);
});
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?url=${urlToCall}`);
});
describe("When not using cors proxy", () => {
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
await performWebRequest(urlToCall);
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
});
await performWebRequest(urlToCall, "json", true, headers);
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(`${locationProtocol}//${locationHost}/cors?sendheaders=header1:value1,header2:value2&url=${urlToCall}`);
});
});
await performWebRequest(urlToCall, "json", false, headers);
describe("When not using cors proxy", () => {
test("Calls correct URL once", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
const expectedHeaders = { headers: { header1: "value1", header2: "value2" } };
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][1]).toStrictEqual(expectedHeaders);
});
await performWebRequest(urlToCall);
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][0]).toBe(urlToCall);
});
describe("When receiving json format", () => {
test("Returns undefined when no data is received", async () => {
urlToCall = "www.test.com";
test("Sends correct headers", async () => {
urlToCall = "http://www.test.com/path?param1=value1";
const headers = [
{ name: "header1", value: "value1" },
{ name: "header2", value: "value2" }
];
const response = await performWebRequest(urlToCall);
await performWebRequest(urlToCall, "json", false, headers);
expect(response).toBe(undefined);
});
test("Returns object when data is received", async () => {
urlToCall = "www.test.com";
fetchResponse = new Response('{"body": "some content"}');
const response = await performWebRequest(urlToCall);
expect(response.body).toBe("some content");
});
test("Returns expected headers when data is received", async () => {
urlToCall = "www.test.com";
fetchResponse = new Response('{"body": "some content"}', { headers: { header1: "value1", header2: "value2" } });
const response = await performWebRequest(urlToCall, "json", false, undefined, ["header1"]);
expect(response.headers.length).toBe(1);
expect(response.headers[0].name).toBe("header1");
expect(response.headers[0].value).toBe("value1");
});
const expectedHeaders = { headers: { header1: "value1", header2: "value2" } };
expect(fetchMock.mock.calls.length).toBe(1);
expect(fetchMock.mock.calls[0][1]).toStrictEqual(expectedHeaders);
});
} else {
test("Always ok, need one test", () => {});
}
});
describe("When receiving json format", () => {
test("Returns undefined when no data is received", async () => {
urlToCall = "www.test.com";
const response = await performWebRequest(urlToCall);
expect(response).toBe(undefined);
});
test("Returns object when data is received", async () => {
urlToCall = "www.test.com";
fetchResponse = new Response('{"body": "some content"}');
const response = await performWebRequest(urlToCall);
expect(response.body).toBe("some content");
});
test("Returns expected headers when data is received", async () => {
urlToCall = "www.test.com";
fetchResponse = new Response('{"body": "some content"}', { headers: { header1: "value1", header2: "value2" } });
const response = await performWebRequest(urlToCall, "json", false, undefined, ["header1"]);
expect(response.headers.length).toBe(1);
expect(response.headers[0].name).toBe("header1");
expect(response.headers[0].value).toBe("value1");
});
});
});
describe("formatTime", () => {