Skip to content

Commit ef57990

Browse files
TS + ES (#112)
1 parent e0b2c0c commit ef57990

File tree

12 files changed

+5302
-621
lines changed

12 files changed

+5302
-621
lines changed

.github/azure-pipelines.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,17 @@ jobs:
8080

8181
# macOS
8282
- template: jobs/macos.yml
83+
parameters:
84+
name: 'macOS_Xcode164'
85+
vmImage: 'macOS-latest'
86+
xCodeVersion: 16.4
8387

8488
# iOS
8589
- template: jobs/ios.yml
8690
parameters:
87-
name: 'iOS_Xcode162'
91+
name: 'iOS_Xcode164'
8892
vmImage: 'macOS-latest'
89-
xCodeVersion: 16.2
93+
xCodeVersion: 16.4
9094
simulator: 'iPhone 16'
9195

9296
- template: jobs/ios.yml

.github/jobs/macos.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
parameters:
2+
name: ''
3+
vmImage: ''
4+
xCodeVersion: ''
5+
16
jobs:
2-
- job: macOS
7+
- job: ${{parameters.name}}
38
timeoutInMinutes: 15
49

510
pool:
6-
vmImage: macos-latest
11+
vmImage: ${{parameters.vmImage}}
712

813
steps:
914
- script: |
10-
sudo xcode-select --switch /Applications/Xcode_15.1.app/Contents/Developer
11-
displayName: 'Select Xcode 15.1'
15+
sudo xcode-select --switch /Applications/Xcode_${{parameters.xCodeVersion}}.app/Contents/Developer
16+
displayName: 'Select Xcode ${{parameters.xCodeVersion}}'
1217
1318
- script: |
1419
cmake -B Build/macOS -GXcode

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ if(IOS)
4040
set(CMAKE_TOOLCHAIN_FILE "${ios-cmake_SOURCE_DIR}/ios.toolchain.cmake" CACHE PATH "")
4141
set(PLATFORM "OS64COMBINED" CACHE STRING "")
4242
set(DEPLOYMENT_TARGET "12" CACHE STRING "")
43+
set(CMAKE_XCODE_GENERATE_SCHEME TRUE)
4344
endif()
4445

4546
project(JsRuntimeHost)

Tests/.babelrc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"esmodules": false
8+
}
9+
}
10+
],
11+
[
12+
"@babel/preset-typescript",
13+
{
14+
"allowDeclareFields": true,
15+
"allowNamespaces": true
16+
}
17+
]
18+
],
19+
"plugins": [
20+
"@babel/plugin-proposal-class-properties",
21+
"@babel/plugin-proposal-object-rest-spread",
22+
"@babel/plugin-proposal-optional-chaining",
23+
"@babel/plugin-proposal-nullish-coalescing-operator",
24+
[
25+
"@babel/plugin-transform-runtime",
26+
{
27+
"corejs": false,
28+
"helpers": true,
29+
"regenerator": true,
30+
"useESModules": false
31+
}
32+
]
33+
],
34+
"env": {
35+
"development": {
36+
"sourceMaps": "inline",
37+
"retainLines": true
38+
},
39+
"production": {
40+
"plugins": [
41+
"babel-plugin-minify-dead-code-elimination"
42+
]
43+
}
44+
}
45+
}

Tests/UnitTests/Android/app/build.gradle

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,8 @@ task copyScripts {
7878
// run copy at execution phase because npm command is not done at configuration phase
7979
copy
8080
{
81-
from "../../../node_modules/chai"
82-
include "chai.js"
83-
into 'src/main/assets/Scripts'
84-
}
85-
copy
86-
{
87-
from "../../../node_modules/mocha"
88-
include "mocha.js"
81+
from '../../dist'
82+
include "*.js"
8983
into 'src/main/assets/Scripts'
9084
}
9185
copy

Tests/UnitTests/CMakeLists.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
set(SCRIPTS
22
"Scripts/symlink_target.js"
3-
"Scripts/tests.js")
3+
"dist/tests.js")
44

5-
set(EXTERNAL_SCRIPTS
6-
"../node_modules/chai/chai.js"
7-
"../node_modules/mocha/mocha.js")
5+
set(TYPE_SCRIPTS
6+
"Scripts/tests.ts")
87

98
set(SOURCES
109
"Shared/Shared.cpp"
@@ -24,7 +23,7 @@ if(APPLE)
2423

2524
set_source_files_properties(
2625
${SCRIPTS}
27-
${EXTERNAL_SCRIPTS}
26+
${TYPE_SCRIPTS}
2827
PROPERTIES MACOSX_PACKAGE_LOCATION "Scripts")
2928
else()
3029
set(SOURCES ${SOURCES}
@@ -38,7 +37,7 @@ elseif(UNIX AND NOT ANDROID)
3837
Linux/App.cpp)
3938
endif()
4039

41-
add_executable(UnitTests ${SOURCES} ${SCRIPTS} ${EXTERNAL_SCRIPTS})
40+
add_executable(UnitTests ${SOURCES} ${SCRIPTS} ${TYPE_SCRIPTS})
4241
target_compile_definitions(UnitTests PRIVATE JSRUNTIMEHOST_PLATFORM="${JSRUNTIMEHOST_PLATFORM}")
4342

4443
target_link_libraries(UnitTests
@@ -70,7 +69,7 @@ if(IOS)
7069
XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET ${DEPLOYMENT_TARGET}
7170
XCODE_ATTRIBUTE_PRODUCT_BUNDLE_IDENTIFIER "com.jsruntimehost.unittests")
7271
else()
73-
foreach(SCRIPT ${SCRIPTS} ${EXTERNAL_SCRIPTS})
72+
foreach(SCRIPT ${SCRIPTS})
7473
get_filename_component(SCRIPT_NAME "${SCRIPT}" NAME)
7574
add_custom_command(
7675
OUTPUT "${CMAKE_CFG_INTDIR}/Scripts/${SCRIPT_NAME}"
@@ -88,4 +87,4 @@ endif()
8887
set_property(TARGET UnitTests PROPERTY FOLDER Tests)
8988

9089
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES} ${SCRIPTS})
91-
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/../node_modules PREFIX node_modules FILES ${EXTERNAL_SCRIPTS})
90+
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/Scripts PREFIX scripts FILES ${TYPE_SCRIPTS})

Tests/UnitTests/Scripts/tests.js renamed to Tests/UnitTests/Scripts/tests.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
mocha.setup({ ui: "bdd", reporter: "spec", retries: 5 });
1+
import * as Mocha from "mocha";
2+
import { expect } from "chai";
3+
4+
Mocha.setup('bdd');
5+
// @ts-ignore
6+
Mocha.reporter('spec');
7+
8+
declare const hostPlatform: string;
9+
declare const setExitCode: (code: number) => void;
210

3-
const expect = chai.expect;
411

512
describe("AbortController", function () {
613
it("should not throw while aborting with no callbacks", function () {
@@ -60,7 +67,7 @@ describe("AbortController", function () {
6067
});
6168

6269
describe("XMLHTTPRequest", function () {
63-
function createRequest(method, url, body, responseType) {
70+
function createRequest(method: string, url: string, body: any = undefined, responseType: any = undefined): Promise<XMLHttpRequest> {
6471
return new Promise((resolve) => {
6572
const xhr = new XMLHttpRequest();
6673
xhr.open(method, url);
@@ -72,11 +79,11 @@ describe("XMLHTTPRequest", function () {
7279
});
7380
}
7481

75-
function createRequestWithHeaders(method, url, headers, body) {
82+
function createRequestWithHeaders(method: string, url: string, headers: any, body?: string): Promise<XMLHttpRequest> {
7683
return new Promise((resolve) => {
7784
const xhr = new XMLHttpRequest();
7885
xhr.open(method, url);
79-
headers.forEach((value, key) => xhr.setRequestHeader(key, value));
86+
headers.forEach((value: string, key: string) => xhr.setRequestHeader(key, value));
8087
xhr.addEventListener("loadend", () => resolve(xhr));
8188
xhr.send(body);
8289
});
@@ -199,7 +206,7 @@ describe("setTimeout", function () {
199206
});
200207

201208
it("should return an id greater than zero when given an undefined function", function () {
202-
const id = setTimeout(undefined, 0);
209+
const id = setTimeout(undefined as any, 0);
203210
expect(id).to.be.greaterThan(0);
204211
});
205212

@@ -241,13 +248,13 @@ describe("setTimeout", function () {
241248
catch (e) {
242249
done(e);
243250
}
244-
}, "10");
251+
}, "10" as any);
245252
});
246253

247254
it("should call the given function after zero milliseconds when the delay is a string representing an invalid number", function (done) {
248255
setTimeout(() => {
249256
done();
250-
}, "a");
257+
}, "a" as any);
251258
});
252259

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

418425
ws.onerror = (ev) => {
419-
done(new Error(ev.message));
426+
done(new Error("WebSocket failed"));
420427
};
421428
});
422429

@@ -459,7 +466,7 @@ if (hostPlatform !== "Unix") {
459466
};
460467

461468
ws2.onerror = (ev) => {
462-
done(new Error(ev.message));
469+
done(new Error(String(ev)));
463470
};
464471
}
465472

@@ -484,7 +491,7 @@ if (hostPlatform !== "Unix") {
484491
}
485492

486493
ws1.onerror = (ev) => {
487-
done(new Error(ev.message));
494+
done(new Error(String(ev)));
488495
};
489496
});
490497

@@ -509,7 +516,15 @@ if (hostPlatform !== "Unix") {
509516
describe("URL", function () {
510517

511518
// Currently all of the properties that the polyfill has implemented
512-
function checkURL(url, { href, hostname, origin, pathname, search }) {
519+
interface URLCheckOptions {
520+
href: string;
521+
hostname: string;
522+
origin: string;
523+
pathname: string;
524+
search: string;
525+
}
526+
527+
function checkURL(url: URL, { href, hostname, origin, pathname, search }: URLCheckOptions): void {
513528
expect(url).to.have.property("hostname", hostname);
514529
expect(url).to.have.property("href", href);
515530
expect(url).to.have.property("origin", origin);
@@ -567,7 +582,7 @@ describe("URL", function () {
567582
it("should update href after URLSearchParams are changed", function () {
568583
// Augment URL with pathname and search
569584
const url = new URL("https://httpbin.org/en-US/docs?foo=1&bar=2");
570-
url.searchParams.set("foo", 999);
585+
url.searchParams.set("foo", 999 as any);
571586
// href should change to reflect searchParams change
572587
checkURL(url, {
573588
href: "https://httpbin.org/en-US/docs?foo=999&bar=2",
@@ -581,7 +596,7 @@ describe("URL", function () {
581596
it("should update href after URLSearchParams are changed (Starting with 0 params)", function () {
582597
// Augment URL with pathname and search
583598
const url = new URL("https://httpbin.org/en-US/docs");
584-
url.searchParams.set("foo", 999);
599+
url.searchParams.set("foo", "999");
585600
// href should change to reflect searchParams change
586601
checkURL(url, {
587602
href: "https://httpbin.org/en-US/docs?foo=999",
@@ -617,12 +632,14 @@ describe("URLSearchParams", function () {
617632
const paramsSet = new URLSearchParams("");
618633

619634
it("should throw exception when trying to set with less than 2 parameters", function () {
635+
// `set` expects parameters, none given.
636+
// @ts-expect-error
620637
expect(() => paramsSet.set()).to.throw();
621638
});
622639

623640
it("should add a number and retrieve it as a string from searchParams", function () {
624641
// Set Number
625-
paramsSet.set("foo", 400);
642+
paramsSet.set("foo", 400 as any);
626643
expect(paramsSet.get("foo")).to.equal("400");
627644
});
628645

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

635652
it("should add a boolean and retrieve it as a string from searchParams", function () {
636653
// Set Boolean
637-
paramsSet.set("baz", true);
654+
paramsSet.set("baz", true as any);
638655
expect(paramsSet.get("baz")).to.equal("true");
639656
});
640657

641658
it("should set an existing number and retrieve it as a string from searchParams", function () {
642659
// Set Existing Value
643-
paramsSet.set("foo", 9999);
660+
paramsSet.set("foo", 9999 as any);
644661
expect(paramsSet.get("foo")).to.equal("9999");
645662
});
646663

@@ -728,10 +745,10 @@ describe("Console", function () {
728745
});
729746

730747
describe("Blob", function () {
731-
let emptyBlobs, helloBlobs, stringBlob, typedArrayBlob, arrayBufferBlob, blobBlob;
748+
let emptyBlobs: Blob[], helloBlobs: Blob[], stringBlob: Blob, typedArrayBlob: Blob, arrayBufferBlob: Blob, blobBlob: Blob;
732749

733750
before(function () {
734-
emptyBlobs = [new Blob(), new Blob([])];
751+
emptyBlobs = [new Blob([]), new Blob([])];
735752
stringBlob = new Blob(["Hello"]);
736753
typedArrayBlob = new Blob([new Uint8Array([72, 101, 108, 108, 111])]),
737754
arrayBufferBlob = new Blob([new Uint8Array([72, 101, 108, 108, 111]).buffer]),
@@ -842,7 +859,7 @@ describe("Blob", function () {
842859
});
843860

844861
function runTests() {
845-
mocha.run(failures => {
862+
mocha.run((failures: number) => {
846863
// Test program will wait for code to be set before exiting
847864
if (failures > 0) {
848865
// Failure

Tests/UnitTests/Shared/Shared.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ TEST(JavaScript, All)
7979
});
8080

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

8885
auto exitCode{exitCodePromise.get_future().get()};

Tests/babel-plugin-replace-bigint.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Replace literal BigInt with Number as it's not supported woth Chakra
2+
// Literals (like 123n) are represented as BigIntLiteral nodes are transformed to NumericLiteral (123)
3+
module.exports = function() {
4+
return {
5+
visitor: {
6+
BigIntLiteral(path) {
7+
const value = path.node.value; // Get the BigInt literal value
8+
path.replaceWith({
9+
type: 'NumericLiteral',
10+
value: Number(value), // Convert BigInt to Number
11+
});
12+
},
13+
},
14+
};
15+
};

0 commit comments

Comments
 (0)