Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
45 changes: 45 additions & 0 deletions Tests/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"esmodules": false
}
}
],
[
"@babel/preset-typescript",
{
"allowDeclareFields": true,
"allowNamespaces": true
}
]
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator",
[
"@babel/plugin-transform-runtime",
{
"corejs": false,
"helpers": true,
"regenerator": true,
"useESModules": false
}
]
],
"env": {
"development": {
"sourceMaps": "inline",
"retainLines": true
},
"production": {
"plugins": [
"babel-plugin-minify-dead-code-elimination"
]
}
}
}
10 changes: 2 additions & 8 deletions Tests/UnitTests/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,8 @@ task copyScripts {
// run copy at execution phase because npm command is not done at configuration phase
copy
{
from "../../../node_modules/chai"
include "chai.js"
into 'src/main/assets/Scripts'
}
copy
{
from "../../../node_modules/mocha"
include "mocha.js"
from '../../dist'
include "*.js"
into 'src/main/assets/Scripts'
}
copy
Expand Down
15 changes: 7 additions & 8 deletions Tests/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
set(SCRIPTS
"Scripts/symlink_target.js"
"Scripts/tests.js")
"dist/tests.js")

set(EXTERNAL_SCRIPTS
"../node_modules/chai/chai.js"
"../node_modules/mocha/mocha.js")
set(TYPE_SCRIPTS
"Scripts/tests.ts")

set(SOURCES
"Shared/Shared.cpp"
Expand All @@ -24,7 +23,7 @@ if(APPLE)

set_source_files_properties(
${SCRIPTS}
${EXTERNAL_SCRIPTS}
${TYPE_SCRIPTS}
PROPERTIES MACOSX_PACKAGE_LOCATION "Scripts")
else()
set(SOURCES ${SOURCES}
Expand All @@ -38,7 +37,7 @@ elseif(UNIX AND NOT ANDROID)
Linux/App.cpp)
endif()

add_executable(UnitTests ${SOURCES} ${SCRIPTS} ${EXTERNAL_SCRIPTS})
add_executable(UnitTests ${SOURCES} ${SCRIPTS} ${TYPE_SCRIPTS})
target_compile_definitions(UnitTests PRIVATE JSRUNTIMEHOST_PLATFORM="${JSRUNTIMEHOST_PLATFORM}")

target_link_libraries(UnitTests
Expand Down Expand Up @@ -70,7 +69,7 @@ if(IOS)
XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET}
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.jsruntimehost.unittests")
else()
foreach(SCRIPT ${SCRIPTS} ${EXTERNAL_SCRIPTS})
foreach(SCRIPT ${SCRIPTS})
get_filename_component(SCRIPT_NAME "${SCRIPT}" NAME)
add_custom_command(
OUTPUT "${CMAKE_CFG_INTDIR}/Scripts/${SCRIPT_NAME}"
Expand All @@ -88,4 +87,4 @@ endif()
set_property(TARGET UnitTests PROPERTY FOLDER Tests)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES} ${SCRIPTS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../node_modules PREFIX node_modules FILES ${EXTERNAL_SCRIPTS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/Scripts PREFIX scripts FILES ${TYPE_SCRIPTS})
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
mocha.setup({ ui: "bdd", reporter: "spec", retries: 5 });
import MochaDefault from "mocha";
const Mocha = MochaDefault as typeof import("mocha");
import { expect } from "chai";

Mocha.setup('bdd');
// @ts-ignore
Mocha.reporter('spec');

declare const hostPlatform: string;
declare const setExitCode: (code: number) => void;

const expect = chai.expect;

describe("AbortController", function () {
it("should not throw while aborting with no callbacks", function () {
Expand Down Expand Up @@ -60,7 +68,7 @@ describe("AbortController", function () {
});

describe("XMLHTTPRequest", function () {
function createRequest(method, url, body, responseType) {
function createRequest(method: string, url: string, body: any = undefined, responseType: any = undefined): Promise<XMLHttpRequest> {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
Expand All @@ -72,11 +80,11 @@ describe("XMLHTTPRequest", function () {
});
}

function createRequestWithHeaders(method, url, headers, body) {
function createRequestWithHeaders(method: string, url: string, headers: any, body?: string): Promise<XMLHttpRequest> {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
headers.forEach((value, key) => xhr.setRequestHeader(key, value));
headers.forEach((value: string, key: string) => xhr.setRequestHeader(key, value));
xhr.addEventListener("loadend", () => resolve(xhr));
xhr.send(body);
});
Expand Down Expand Up @@ -199,7 +207,7 @@ describe("setTimeout", function () {
});

it("should return an id greater than zero when given an undefined function", function () {
const id = setTimeout(undefined, 0);
const id = setTimeout(undefined as any, 0);
expect(id).to.be.greaterThan(0);
});

Expand Down Expand Up @@ -241,13 +249,13 @@ describe("setTimeout", function () {
catch (e) {
done(e);
}
}, "10");
}, "10" as any);
});

it("should call the given function after zero milliseconds when the delay is a string representing an invalid number", function (done) {
setTimeout(() => {
done();
}, "a");
}, "a" as any);
});

it("should call the given function after other tasks execute when the given delay is zero", function (done) {
Expand Down Expand Up @@ -416,7 +424,7 @@ if (hostPlatform !== "Unix") {
};

ws.onerror = (ev) => {
done(new Error(ev.message));
done(new Error(String(ev)));
};
});

Expand Down Expand Up @@ -459,7 +467,7 @@ if (hostPlatform !== "Unix") {
};

ws2.onerror = (ev) => {
done(new Error(ev.message));
done(new Error(String(ev)));
};
}

Expand All @@ -484,7 +492,7 @@ if (hostPlatform !== "Unix") {
}

ws1.onerror = (ev) => {
done(new Error(ev.message));
done(new Error(String(ev)));
};
});

Expand All @@ -509,7 +517,15 @@ if (hostPlatform !== "Unix") {
describe("URL", function () {

// Currently all of the properties that the polyfill has implemented
function checkURL(url, { href, hostname, origin, pathname, search }) {
interface URLCheckOptions {
href: string;
hostname: string;
origin: string;
pathname: string;
search: string;
}

function checkURL(url: URL, { href, hostname, origin, pathname, search }: URLCheckOptions): void {
expect(url).to.have.property("hostname", hostname);
expect(url).to.have.property("href", href);
expect(url).to.have.property("origin", origin);
Expand Down Expand Up @@ -567,7 +583,7 @@ describe("URL", function () {
it("should update href after URLSearchParams are changed", function () {
// Augment URL with pathname and search
const url = new URL("https://httpbin.org/en-US/docs?foo=1&bar=2");
url.searchParams.set("foo", 999);
url.searchParams.set("foo", 999 as any);
// href should change to reflect searchParams change
checkURL(url, {
href: "https://httpbin.org/en-US/docs?foo=999&bar=2",
Expand All @@ -581,7 +597,7 @@ describe("URL", function () {
it("should update href after URLSearchParams are changed (Starting with 0 params)", function () {
// Augment URL with pathname and search
const url = new URL("https://httpbin.org/en-US/docs");
url.searchParams.set("foo", 999);
url.searchParams.set("foo", 999 as any);
// href should change to reflect searchParams change
checkURL(url, {
href: "https://httpbin.org/en-US/docs?foo=999",
Expand Down Expand Up @@ -617,12 +633,13 @@ describe("URLSearchParams", function () {
const paramsSet = new URLSearchParams("");

it("should throw exception when trying to set with less than 2 parameters", function () {
// @ts-expect-error
expect(() => paramsSet.set()).to.throw();
});

it("should add a number and retrieve it as a string from searchParams", function () {
// Set Number
paramsSet.set("foo", 400);
paramsSet.set("foo", 400 as any);
expect(paramsSet.get("foo")).to.equal("400");
});

Expand All @@ -634,13 +651,13 @@ describe("URLSearchParams", function () {

it("should add a boolean and retrieve it as a string from searchParams", function () {
// Set Boolean
paramsSet.set("baz", true);
paramsSet.set("baz", true as any);
expect(paramsSet.get("baz")).to.equal("true");
});

it("should set an existing number and retrieve it as a string from searchParams", function () {
// Set Existing Value
paramsSet.set("foo", 9999);
paramsSet.set("foo", 9999 as any);
expect(paramsSet.get("foo")).to.equal("9999");
});

Expand Down Expand Up @@ -728,10 +745,10 @@ describe("Console", function () {
});

describe("Blob", function () {
let emptyBlobs, helloBlobs, stringBlob, typedArrayBlob, arrayBufferBlob, blobBlob;
let emptyBlobs: Blob[], helloBlobs: Blob[], stringBlob: Blob, typedArrayBlob: Blob, arrayBufferBlob: Blob, blobBlob: Blob;

before(function () {
emptyBlobs = [new Blob(), new Blob([])];
emptyBlobs = [new Blob([]), new Blob([])];
stringBlob = new Blob(["Hello"]);
typedArrayBlob = new Blob([new Uint8Array([72, 101, 108, 108, 111])]),
arrayBufferBlob = new Blob([new Uint8Array([72, 101, 108, 108, 111]).buffer]),
Expand Down Expand Up @@ -842,7 +859,7 @@ describe("Blob", function () {
});

function runTests() {
mocha.run(failures => {
mocha.run((failures: number) => {
// Test program will wait for code to be set before exiting
if (failures > 0) {
// Failure
Expand Down
5 changes: 1 addition & 4 deletions Tests/UnitTests/Shared/Shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ TEST(JavaScript, All)
});

Babylon::ScriptLoader loader{runtime};
loader.Eval("var global = {};", ""); // Required for chai.js
loader.Eval("var location = { href: '' };", ""); // Required for mocha.js
loader.LoadScript("app:///Scripts/chai.js");
loader.LoadScript("app:///Scripts/mocha.js");
loader.Eval("location = { href: '' };", ""); // Required for Mocha.js as we do not have a location
loader.LoadScript("app:///Scripts/tests.js");

auto exitCode{exitCodePromise.get_future().get()};
Expand Down
13 changes: 13 additions & 0 deletions Tests/babel-plugin-replace-bigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = function() {
return {
visitor: {
BigIntLiteral(path) {
const value = path.node.value; // Get the BigInt literal value
path.replaceWith({
type: 'NumericLiteral',
value: Number(value), // Convert BigInt to Number
});
},
},
};
};
11 changes: 11 additions & 0 deletions Tests/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compact": false,
"presets": [
["@babel/preset-env", {
"targets": {
"ie": "11"
}
}]
],
"plugins": ["./babel-plugin-replace-bigint"]
}
Loading
Loading