elit 3.6.3 → 3.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/test.cjs CHANGED
@@ -3290,9 +3290,124 @@ var testPattern = void 0;
3290
3290
  var currentTestFile = void 0;
3291
3291
  var currentSourceMapConsumer = void 0;
3292
3292
  var wrapperLineOffset = 0;
3293
+ var TEST_MODULE_EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs", ".json"];
3294
+ function resolveTestLoader(filePath) {
3295
+ return /\.(?:ts|tsx|mts|cts)$/i.test(filePath) ? "ts" : "js";
3296
+ }
3297
+ function createTestTransformOptions(filePath, format, sourcemap) {
3298
+ return {
3299
+ loader: resolveTestLoader(filePath),
3300
+ format,
3301
+ sourcemap,
3302
+ sourcefile: filePath,
3303
+ target: "es2020",
3304
+ tsconfigRaw: {
3305
+ compilerOptions: {
3306
+ jsx: "react",
3307
+ jsxFactory: "h",
3308
+ jsxFragmentFactory: "Fragment"
3309
+ }
3310
+ }
3311
+ };
3312
+ }
3313
+ function resolveExistingTestModulePath(basePath) {
3314
+ const nodePath = require("path");
3315
+ if (existsSync(basePath) && statSync(basePath).isFile()) {
3316
+ return basePath;
3317
+ }
3318
+ for (const extension of TEST_MODULE_EXTENSIONS) {
3319
+ const candidatePath = `${basePath}${extension}`;
3320
+ if (existsSync(candidatePath) && statSync(candidatePath).isFile()) {
3321
+ return candidatePath;
3322
+ }
3323
+ }
3324
+ if (existsSync(basePath) && statSync(basePath).isDirectory()) {
3325
+ const packageJsonPath = nodePath.join(basePath, "package.json");
3326
+ if (existsSync(packageJsonPath) && statSync(packageJsonPath).isFile()) {
3327
+ try {
3328
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
3329
+ for (const candidateEntry of [packageJson.main, packageJson.module]) {
3330
+ if (typeof candidateEntry !== "string" || candidateEntry.trim().length === 0) {
3331
+ continue;
3332
+ }
3333
+ try {
3334
+ return resolveExistingTestModulePath(nodePath.resolve(basePath, candidateEntry));
3335
+ } catch {
3336
+ continue;
3337
+ }
3338
+ }
3339
+ } catch {
3340
+ }
3341
+ }
3342
+ for (const extension of TEST_MODULE_EXTENSIONS) {
3343
+ const candidatePath = nodePath.join(basePath, `index${extension}`);
3344
+ if (existsSync(candidatePath) && statSync(candidatePath).isFile()) {
3345
+ return candidatePath;
3346
+ }
3347
+ }
3348
+ }
3349
+ return basePath;
3350
+ }
3351
+ function resolveTestModulePath(fromFilePath, specifier) {
3352
+ if (!specifier.startsWith(".") && !specifier.startsWith("/")) {
3353
+ return specifier;
3354
+ }
3355
+ const nodePath = require("path");
3356
+ const basePath = specifier.startsWith(".") ? nodePath.resolve(dirname(fromFilePath), specifier) : specifier;
3357
+ return resolveExistingTestModulePath(basePath);
3358
+ }
3359
+ function shouldTranspileTestModule(filePath) {
3360
+ return /\.(?:ts|tsx|mts|cts|js|jsx|mjs|cjs)$/i.test(filePath);
3361
+ }
3362
+ function createTestModuleRequire(fromFilePath, moduleCache) {
3363
+ return (specifier) => {
3364
+ if (specifier.startsWith("elit/") || specifier === "elit") {
3365
+ return require(specifier);
3366
+ }
3367
+ const resolvedPath = resolveTestModulePath(fromFilePath, specifier);
3368
+ if (resolvedPath === specifier) {
3369
+ return require(specifier);
3370
+ }
3371
+ if (!existsSync(resolvedPath) || !statSync(resolvedPath).isFile()) {
3372
+ return require(resolvedPath);
3373
+ }
3374
+ if (!shouldTranspileTestModule(resolvedPath)) {
3375
+ return require(resolvedPath);
3376
+ }
3377
+ return loadTranspiledTestModule(resolvedPath, moduleCache);
3378
+ };
3379
+ }
3380
+ function loadTranspiledTestModule(modulePath, moduleCache) {
3381
+ const cached = moduleCache.get(modulePath);
3382
+ if (cached) {
3383
+ return cached.exports;
3384
+ }
3385
+ const source = readFileSync(modulePath, "utf-8");
3386
+ let transpiled;
3387
+ try {
3388
+ transpiled = (0, import_esbuild.transformSync)(source, createTestTransformOptions(modulePath, "cjs", false));
3389
+ } catch (error) {
3390
+ throw new Error(`Failed to transpile test dependency ${modulePath}: ${error instanceof Error ? error.message : String(error)}`);
3391
+ }
3392
+ const moduleRecord = { exports: {} };
3393
+ const moduleObj = { exports: moduleRecord.exports };
3394
+ moduleCache.set(modulePath, moduleRecord);
3395
+ try {
3396
+ const fn = new Function("module", "exports", "require", "__filename", "__dirname", transpiled.code);
3397
+ const requireFn = createTestModuleRequire(modulePath, moduleCache);
3398
+ fn(moduleObj, moduleObj.exports, requireFn, modulePath, dirname(modulePath));
3399
+ } catch (error) {
3400
+ throw new Error(`Failed to execute test dependency ${modulePath}: ${error instanceof Error ? error.message : String(error)}`);
3401
+ }
3402
+ moduleRecord.exports = moduleObj.exports;
3403
+ if (!modulePath.includes(".test.") && !modulePath.includes(".spec.")) {
3404
+ coveredFiles.add(modulePath);
3405
+ }
3406
+ return moduleRecord.exports;
3407
+ }
3293
3408
  function createTestFunction(defaultTimeout = 5e3) {
3294
3409
  const testFn = function(name, fn, timeout) {
3295
- const test2 = {
3410
+ const test = {
3296
3411
  name,
3297
3412
  fn,
3298
3413
  skip: currentSuite.skip,
@@ -3301,10 +3416,10 @@ function createTestFunction(defaultTimeout = 5e3) {
3301
3416
  timeout: timeout ?? defaultTimeout,
3302
3417
  suite: currentSuite
3303
3418
  };
3304
- currentSuite.tests.push(test2);
3419
+ currentSuite.tests.push(test);
3305
3420
  };
3306
3421
  testFn.skip = (name, fn, timeout) => {
3307
- const test2 = {
3422
+ const test = {
3308
3423
  name,
3309
3424
  fn,
3310
3425
  skip: true,
@@ -3313,11 +3428,11 @@ function createTestFunction(defaultTimeout = 5e3) {
3313
3428
  timeout: timeout ?? defaultTimeout,
3314
3429
  suite: currentSuite
3315
3430
  };
3316
- currentSuite.tests.push(test2);
3431
+ currentSuite.tests.push(test);
3317
3432
  };
3318
3433
  testFn.only = (name, fn, timeout) => {
3319
3434
  hasOnly = true;
3320
- const test2 = {
3435
+ const test = {
3321
3436
  name,
3322
3437
  fn,
3323
3438
  skip: false,
@@ -3326,10 +3441,10 @@ function createTestFunction(defaultTimeout = 5e3) {
3326
3441
  timeout: timeout ?? defaultTimeout,
3327
3442
  suite: currentSuite
3328
3443
  };
3329
- currentSuite.tests.push(test2);
3444
+ currentSuite.tests.push(test);
3330
3445
  };
3331
3446
  testFn.todo = (name, fn, timeout) => {
3332
- const test2 = {
3447
+ const test = {
3333
3448
  name,
3334
3449
  fn,
3335
3450
  skip: false,
@@ -3338,7 +3453,7 @@ function createTestFunction(defaultTimeout = 5e3) {
3338
3453
  timeout: timeout ?? defaultTimeout,
3339
3454
  suite: currentSuite
3340
3455
  };
3341
- currentSuite.tests.push(test2);
3456
+ currentSuite.tests.push(test);
3342
3457
  };
3343
3458
  return testFn;
3344
3459
  }
@@ -3904,29 +4019,7 @@ async function runTests(options) {
3904
4019
  try {
3905
4020
  const source = await readFile(file, "utf-8");
3906
4021
  const testFileDir = dirname(file);
3907
- const importRegex = /import\s+{\s*([^}]+)\s*}\s+from\s+['"]([^'"]+)['"]/g;
3908
- const imports = {};
3909
- let importIndex = 0;
3910
- let codeWithoutImports = source.replace(importRegex, (_, named, path) => {
3911
- const varName = `__import_${importIndex++}`;
3912
- const trimmedNamed = named.trim();
3913
- imports[varName] = { path, named: trimmedNamed };
3914
- return `// ${trimmedNamed} import injected later
3915
- `;
3916
- });
3917
- const result = (0, import_esbuild.transformSync)(codeWithoutImports, {
3918
- loader: file.endsWith(".ts") || file.endsWith(".tsx") ? "ts" : "js",
3919
- format: "iife",
3920
- sourcemap: "inline",
3921
- target: "es2020",
3922
- tsconfigRaw: {
3923
- compilerOptions: {
3924
- jsx: "react",
3925
- jsxFactory: "h",
3926
- jsxFragmentFactory: "Fragment"
3927
- }
3928
- }
3929
- });
4022
+ const result = (0, import_esbuild.transformSync)(source, createTestTransformOptions(file, "cjs", "inline"));
3930
4023
  let code = result.code;
3931
4024
  const sourceMapMatch = code.match(/\/\/# sourceMappingURL=data:application\/json;base64,(.+)/);
3932
4025
  if (sourceMapMatch) {
@@ -3937,99 +4030,15 @@ async function runTests(options) {
3937
4030
  } else {
3938
4031
  currentSourceMapConsumer = void 0;
3939
4032
  }
3940
- const importedValues = {};
3941
- const importParamNames = [];
3942
- const importAssignments = [];
3943
- if (Object.keys(imports).length > 0) {
3944
- for (const [, { path, named }] of Object.entries(imports)) {
3945
- let resolvedPath = path;
3946
- if (path.startsWith(".")) {
3947
- const nodePath = require("path");
3948
- resolvedPath = nodePath.resolve(testFileDir, path);
3949
- }
3950
- if (!resolvedPath.endsWith(".ts") && !resolvedPath.endsWith(".js") && !resolvedPath.endsWith(".mjs") && !resolvedPath.endsWith(".cjs")) {
3951
- resolvedPath += ".ts";
3952
- }
3953
- if (resolvedPath.endsWith(".ts")) {
3954
- try {
3955
- const importSource = await readFile(resolvedPath, "utf-8");
3956
- const transpiled = (0, import_esbuild.transformSync)(importSource, {
3957
- loader: "ts",
3958
- format: "cjs",
3959
- target: "es2020",
3960
- tsconfigRaw: {
3961
- compilerOptions: {
3962
- jsx: "react",
3963
- jsxFactory: "h",
3964
- jsxFragmentFactory: "Fragment"
3965
- }
3966
- }
3967
- });
3968
- const moduleExports = {};
3969
- const moduleObj = { exports: moduleExports };
3970
- const fn2 = new Function("module", "exports", "require", "__filename", "__dirname", transpiled.code);
3971
- const requireFn = (id) => {
3972
- if (id.startsWith("elit/") || id === "elit") {
3973
- return require(id);
3974
- }
3975
- if (id.startsWith(".")) {
3976
- const nodePath = require("path");
3977
- const absPath = nodePath.resolve(dirname(resolvedPath), id);
3978
- return require(absPath);
3979
- }
3980
- return require(id);
3981
- };
3982
- fn2(moduleObj, moduleExports, requireFn, resolvedPath, dirname(resolvedPath));
3983
- if (!resolvedPath.includes(".test.") && !resolvedPath.includes(".spec.")) {
3984
- coveredFiles.add(resolvedPath);
3985
- }
3986
- let exportedValue = moduleObj.exports[named];
3987
- if (exportedValue === void 0 && moduleObj.exports.default) {
3988
- exportedValue = moduleObj.exports.default[named];
3989
- }
3990
- if (exportedValue === void 0 && typeof moduleObj.exports === "object") {
3991
- exportedValue = moduleObj.exports[named];
3992
- }
3993
- const paramKey = `__import_${Math.random().toString(36).substring(2, 11)}`;
3994
- importedValues[paramKey] = exportedValue;
3995
- importParamNames.push(paramKey);
3996
- importAssignments.push(`const ${named} = ${paramKey};`);
3997
- } catch (err) {
3998
- const paramKey = `__import_${Math.random().toString(36).substring(2, 11)}`;
3999
- importedValues[paramKey] = null;
4000
- importParamNames.push(paramKey);
4001
- importAssignments.push(`const ${named} = ${paramKey}; /* Error importing ${resolvedPath}: ${err} */`);
4002
- }
4003
- } else {
4004
- const requiredModule = require(resolvedPath);
4005
- const exportedValue = requiredModule[named];
4006
- const paramKey = `__import_${Math.random().toString(36).substring(2, 11)}`;
4007
- importedValues[paramKey] = exportedValue;
4008
- importParamNames.push(paramKey);
4009
- importAssignments.push(`const ${named} = ${paramKey};`);
4010
- }
4011
- }
4012
- }
4013
- let preamble = "";
4014
- if (Object.keys(imports).length > 0) {
4015
- const iifeStartMatch = code.match(/^(\s*(?:var\s+\w+\s*=\s*)?\(\(\)\s*=>\s*\{\n)/);
4016
- if (iifeStartMatch) {
4017
- const iifePrefix = iifeStartMatch[1];
4018
- const assignments = `${importAssignments.join("\n")}
4019
- `;
4020
- preamble = iifePrefix;
4021
- code = iifePrefix + assignments + code.slice(iifeStartMatch[1].length);
4022
- } else {
4023
- preamble = importAssignments.join("\n") + "\n";
4024
- code = preamble + code;
4025
- }
4026
- }
4027
- wrapperLineOffset = preamble.split("\n").length;
4033
+ wrapperLineOffset = 0;
4028
4034
  setupGlobals();
4029
- const allParams = ["describe", "it", "test", "expect", "beforeAll", "afterAll", "beforeEach", "afterEach", "vi", "require", "module", "__filename", "__dirname", ...importParamNames];
4030
- const allArgs = [describe, it, test, expect, beforeAll, afterAll, beforeEach, afterEach, vi, require, module, file, testFileDir, ...importParamNames.map((p) => importedValues[p])];
4031
- const fn = new Function(...allParams, code);
4032
- await fn(...allArgs);
4035
+ const moduleCache = /* @__PURE__ */ new Map();
4036
+ const moduleRecord = { exports: {} };
4037
+ const moduleObj = { exports: moduleRecord.exports };
4038
+ moduleCache.set(file, moduleRecord);
4039
+ const fn = new Function("module", "exports", "require", "__filename", "__dirname", code);
4040
+ const requireFn = createTestModuleRequire(file, moduleCache);
4041
+ await fn(moduleObj, moduleObj.exports, requireFn, file, testFileDir);
4033
4042
  await executeSuite(currentSuite, timeout, bail);
4034
4043
  if (currentSourceMapConsumer) {
4035
4044
  currentSourceMapConsumer.destroy();
@@ -4094,22 +4103,22 @@ async function executeSuite(suite, timeout, bail, parentMatched = false) {
4094
4103
  for (const hook of beforeAllHooks) {
4095
4104
  await hook();
4096
4105
  }
4097
- for (const test2 of suite.tests) {
4098
- if (hasOnly && !test2.only && !suite.only) {
4106
+ for (const test of suite.tests) {
4107
+ if (hasOnly && !test.only && !suite.only) {
4099
4108
  continue;
4100
4109
  }
4101
4110
  let testMatches = true;
4102
4111
  if (testPattern) {
4103
4112
  const escapedPattern = escapeRegex(testPattern);
4104
4113
  const regex = new RegExp(escapedPattern, "i");
4105
- testMatches = regex.test(test2.name);
4114
+ testMatches = regex.test(test.name);
4106
4115
  }
4107
4116
  if (!testMatches) {
4108
4117
  continue;
4109
4118
  }
4110
- if (test2.skip || suite.skip) {
4119
+ if (test.skip || suite.skip) {
4111
4120
  testResults.push({
4112
- name: test2.name,
4121
+ name: test.name,
4113
4122
  status: "skip",
4114
4123
  duration: 0,
4115
4124
  suite: suite.name,
@@ -4117,9 +4126,9 @@ async function executeSuite(suite, timeout, bail, parentMatched = false) {
4117
4126
  });
4118
4127
  continue;
4119
4128
  }
4120
- if (test2.todo) {
4129
+ if (test.todo) {
4121
4130
  testResults.push({
4122
- name: test2.name,
4131
+ name: test.name,
4123
4132
  status: "todo",
4124
4133
  duration: 0,
4125
4134
  suite: suite.name,
@@ -4133,13 +4142,13 @@ async function executeSuite(suite, timeout, bail, parentMatched = false) {
4133
4142
  const startTime = Date.now();
4134
4143
  try {
4135
4144
  await Promise.race([
4136
- test2.fn(),
4145
+ test.fn(),
4137
4146
  new Promise(
4138
- (_, reject) => setTimeout(() => reject(new Error(`Test timed out after ${test2.timeout}ms`)), test2.timeout)
4147
+ (_, reject) => setTimeout(() => reject(new Error(`Test timed out after ${test.timeout}ms`)), test.timeout)
4139
4148
  )
4140
4149
  ]);
4141
4150
  testResults.push({
4142
- name: test2.name,
4151
+ name: test.name,
4143
4152
  status: "pass",
4144
4153
  duration: Date.now() - startTime,
4145
4154
  suite: suite.name,
@@ -4153,7 +4162,7 @@ async function executeSuite(suite, timeout, bail, parentMatched = false) {
4153
4162
  codeSnippet = error.codeSnippet;
4154
4163
  }
4155
4164
  testResults.push({
4156
- name: test2.name,
4165
+ name: test.name,
4157
4166
  status: "fail",
4158
4167
  duration: Date.now() - startTime,
4159
4168
  error,