| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1d16976 commit 93c6c90
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -459,7 +459,7 @@ translators.set('wasm', async function(url, source) { | |||
| 459 | 459 | // Strategy for loading a commonjs TypeScript module | |
| 460 | 460 | translators.set('commonjs-typescript', function(url, source) { | |
| 461 | 461 | emitExperimentalWarning('Type Stripping'); | |
| 462 | - assertBufferSource(source, false, 'load'); | ||
| 462 | + assertBufferSource(source, true, 'load'); | ||
| 463 | 463 | const code = stripTypeScriptTypes(stringify(source), url); | |
| 464 | 464 | debug(`Translating TypeScript ${url}`); | |
| 465 | 465 | return FunctionPrototypeCall(translators.get('commonjs'), this, url, code, false); | |
@@ -468,7 +468,7 @@ translators.set('commonjs-typescript', function(url, source) { | |||
| 468 | 468 | // Strategy for loading an esm TypeScript module | |
| 469 | 469 | translators.set('module-typescript', function(url, source) { | |
| 470 | 470 | emitExperimentalWarning('Type Stripping'); | |
| 471 | - assertBufferSource(source, false, 'load'); | ||
| 471 | + assertBufferSource(source, true, 'load'); | ||
| 472 | 472 | const code = stripTypeScriptTypes(stringify(source), url); | |
| 473 | 473 | debug(`Translating TypeScript ${url}`); | |
| 474 | 474 | return FunctionPrototypeCall(translators.get('module'), this, url, code, false); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -137,7 +137,7 @@ async function load(url, context, nextLoad) { | |||
| 137 | 137 | async function createSourceFromMock(mock, format) { | |
| 138 | 138 | // Create mock implementation from provided exports. | |
| 139 | 139 | const { exportNames, hasDefaultExport, url } = mock; | |
| 140 | - const useESM = format === 'module'; | ||
| 140 | + const useESM = format === 'module' || format === 'module-typescript'; | ||
| 141 | 141 | const source = `${testImportSource(useESM)} | |
| 142 | 142 | if (!$__test.mock._mockExports.has('${url}')) { | |
| 143 | 143 | throw new Error(${JSONStringify(`mock exports not found for "${url}"`)}); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,7 +70,7 @@ const kMockUnknownMessage = 3; | |||
| 70 | 70 | const kWaitTimeout = 5_000; | |
| 71 | 71 | const kBadExportsMessage = 'Cannot create mock because named exports ' + | |
| 72 | 72 | 'cannot be applied to the provided default export.'; | |
| 73 | - const kSupportedFormats = ['builtin', 'commonjs', 'module']; | ||
| 73 | + const kSupportedFormats = ['builtin', 'commonjs', 'module', 'module-typescript', 'commonjs-typescript']; | ||
| 74 | 74 | let sharedModuleState; | |
| 75 | 75 | ||
| 76 | 76 | class MockFunctionContext { | |
@@ -517,7 +517,10 @@ class MockTracker { | |||
| 517 | 517 | ||
| 518 | 518 | // Get the file that called this function. We need four stack frames: | |
| 519 | 519 | // vm context -> getStructuredStack() -> this function -> actual caller. | |
| 520 | - const caller = pathToFileURL(getStructuredStack()[3]?.getFileName()).href; | ||
| 520 | + const filename = getStructuredStack()[3]?.getFileName(); | ||
| 521 | + // If the caller is already a file URL, use it as is. Otherwise, convert it. | ||
| 522 | + const hasFileProtocol = StringPrototypeStartsWith(filename, 'file://'); | ||
| 523 | + const caller = hasFileProtocol ? filename : pathToFileURL(filename).href; | ||
| 521 | 524 | const { format, url } = sharedState.moduleLoader.resolveSync( | |
| 522 | 525 | mockSpecifier, caller, null, | |
| 523 | 526 | ); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - import { skip, spawnPromisified } from '../common/index.mjs'; | ||
| 1 | + import { skip, spawnPromisified, isWindows } from '../common/index.mjs'; | ||
| 2 | 2 | import * as fixtures from '../common/fixtures.mjs'; | |
| 3 | 3 | import { match, strictEqual } from 'node:assert'; | |
| 4 | 4 | import { test } from 'node:test'; | |
@@ -336,3 +336,20 @@ test('execute a JavaScript file importing a cjs TypeScript file', async () => { | |||
| 336 | 336 | match(result.stdout, /Hello, TypeScript!/); | |
| 337 | 337 | strictEqual(result.code, 0); | |
| 338 | 338 | }); | |
| 339 | + | ||
| 340 | + // TODO(marco-ippolito) Due to a bug in SWC, the TypeScript loader | ||
| 341 | + // does not work on Windows arm64. This test should be re-enabled | ||
| 342 | + // when https://github.com/nodejs/node/issues/54645 is fixed | ||
| 343 | + test('execute a TypeScript test mocking module', { skip: isWindows && process.arch === 'arm64' }, async () => { | ||
| 344 | + const result = await spawnPromisified(process.execPath, [ | ||
| 345 | + '--test', | ||
| 346 | + '--experimental-test-module-mocks', | ||
| 347 | + '--experimental-strip-types', | ||
| 348 | + '--no-warnings', | ||
| 349 | + fixtures.path('typescript/ts/test-mock-module.ts'), | ||
| 350 | + ]); | ||
| 351 | + strictEqual(result.stderr, ''); | ||
| 352 | + match(result.stdout, /Hello, TypeScript-Module!/); | ||
| 353 | + match(result.stdout, /Hello, TypeScript-CommonJS!/); | ||
| 354 | + strictEqual(result.code, 0); | ||
| 355 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + const logger = (): void => { }; | ||
| 2 | + | ||
| 3 | + module.exports = { | ||
| 4 | + logger | ||
| 5 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + export const logger = (): void => { }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,23 @@ | |||
| 1 | + import { before, mock, test } from 'node:test'; | ||
| 2 | + import { strictEqual } from 'node:assert'; | ||
| 3 | + | ||
| 4 | + const logger = mock.fn((s) => console.log(`Hello, ${s}!`)); | ||
| 5 | + | ||
| 6 | + before(async () => { | ||
| 7 | + mock.module('./module-logger.ts', { | ||
| 8 | + namedExports: { logger } | ||
| 9 | + }); | ||
| 10 | + mock.module('./commonjs-logger.ts', { | ||
| 11 | + namedExports: { logger } | ||
| 12 | + }); | ||
| 13 | + }); | ||
| 14 | + | ||
| 15 | + test('logger', async () => { | ||
| 16 | + const { logger: mockedModule } = await import('./module-logger.ts'); | ||
| 17 | + mockedModule('TypeScript-Module'); | ||
| 18 | + strictEqual(logger.mock.callCount(), 1); | ||
| 19 | + | ||
| 20 | + const { logger: mockedCommonjs } = await import('./commonjs-logger.ts'); | ||
| 21 | + mockedCommonjs('TypeScript-CommonJS'); | ||
| 22 | + strictEqual(logger.mock.callCount(), 2); | ||
| 23 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments