Skip to content

Commit

Permalink
Bring some tests fixes and additions from the ESM branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jul 4, 2024
1 parent 71b0762 commit e65d850
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
50 changes: 45 additions & 5 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func TestLoadCycle(t *testing.T) {
require.NoError(t, fsext.WriteFile(fileSystem, "/counter.js", []byte(`
let main = require("./main.js");
exports.count = 5;
export function a() {
exports.a = function() {
return main.message;
}
`), fs.ModePerm))
Expand All @@ -303,15 +303,14 @@ func TestLoadCycle(t *testing.T) {
let counter = require("./counter.js");
let count = counter.count;
let a = counter.a;
let message= "Eval complete";
exports.message = message;
exports.message = "Eval complete";
export default function() {
exports.default = function() {
if (count != 5) {
throw new Error("Wrong value of count "+ count);
}
let aMessage = a();
if (aMessage != message) {
if (aMessage != exports.message) {
throw new Error("Wrong value of a() "+ aMessage);
}
}
Expand Down Expand Up @@ -683,3 +682,44 @@ func TestOptionsAreNotGloballyWritable(t *testing.T) {

require.EqualValues(t, time.Minute*5, r2.GetOptions().MinIterationDuration.Duration)
}

func TestDefaultNamedExports(t *testing.T) {
t.Parallel()
_, err := getSimpleRunner(t, "/main.js", `export default function main() {}`,
lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)
}

func TestStarImport(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/commonjs_file.js": `exports.something = 5;`,
})
require.NoError(t, err)

r1, err := getSimpleRunner(t, "/script.js", `
import * as cjs from "./commonjs_file.js"; // commonjs
import * as k6 from "k6"; // "new" go module
// TODO: test with basic go module maybe
if (cjs.something != 5) {
throw "cjs.something has wrong value" + cjs.something;
}
if (typeof k6.sleep != "function") {
throw "k6.sleep has wrong type" + typeof k6.sleep;
}
export default () => {}
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
_, err = NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
require.NoError(t, err)
}
19 changes: 19 additions & 0 deletions js/path_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ func TestOpenPathResolution(t *testing.T) {
`,
},
},
"space in path": {
fsMap: map[string]any{
"/A/B D/data.txt": "data file",
"/A/C D/B/script.js": `
// Here the path is relative to this module but to the one calling
module.exports = () => open("./../data.txt");
`,
"/A/B D/B/script.js": `
module.exports = require("./../../C D/B/script.js")();
`,
"/A/A/A/A/script.js": `
let data = require("./../../../B D/B/script.js");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
}

for name, testCase := range testCases {
Expand Down
9 changes: 5 additions & 4 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestOptionsSettingToScript(t *testing.T) {
t.Run(fmt.Sprintf("Variant#%d", i), func(t *testing.T) {
t.Parallel()
data := variant + `
exports.default = function() {
export default function() {
if (!options) {
throw new Error("Expected options to be defined!");
}
Expand Down Expand Up @@ -549,7 +549,8 @@ func TestRunnerIntegrationImports(t *testing.T) {
mod := mod
t.Run(mod, func(t *testing.T) {
t.Run("Source", func(t *testing.T) {
_, err := getSimpleRunner(t, "/script.js", fmt.Sprintf(`import "%s"; exports.default = function() {}`, mod), rtOpts)
_, err := getSimpleRunner(t, "/script.js",
fmt.Sprintf(`import "%s"; export default function() {}`, mod), rtOpts)
require.NoError(t, err)
})
})
Expand All @@ -563,8 +564,8 @@ func TestRunnerIntegrationImports(t *testing.T) {
"Absolute": {"/path/script.js", "/path/to/lib.js"},
"Relative": {"/path/script.js", "./to/lib.js"},
"Adjacent": {"/path/to/script.js", "./lib.js"},
"STDIN-Absolute": {"-", "/path/to/lib.js"},
"STDIN-Relative": {"-", "./path/to/lib.js"},
"STDIN-Absolute": {"/-", "/path/to/lib.js"},
"STDIN-Relative": {"/-", "./path/to/lib.js"},
}
for name, data := range testdata {
name, data := name, data
Expand Down

0 comments on commit e65d850

Please sign in to comment.