|
9 | 9 | tsxEsmApiCjsPath,
|
10 | 10 | type NodeApis,
|
11 | 11 | } from '../utils/tsx.js';
|
12 |
| -import { createPackageJson } from '../fixtures.js'; |
| 12 | +import { createPackageJson, createTsconfig } from '../fixtures.js'; |
13 | 13 |
|
14 | 14 | const tsFiles = {
|
15 | 15 | 'file.ts': `
|
@@ -188,7 +188,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
|
188 | 188 | expect(stdout).toBe('Fails as expected\nfoo bar');
|
189 | 189 | });
|
190 | 190 |
|
191 |
| - describe('register / unregister', ({ test }) => { |
| 191 | + describe('register / unregister', ({ test, describe }) => { |
192 | 192 | test('register / unregister', async () => {
|
193 | 193 | await using fixture = await createFixture({
|
194 | 194 | 'package.json': createPackageJson({ type: 'module' }),
|
@@ -285,9 +285,141 @@ export default testSuite(({ describe }, node: NodeApis) => {
|
285 | 285 | });
|
286 | 286 | expect(stdout).toBe('file.ts\nfoo.ts\nbar.ts\nindex.js');
|
287 | 287 | });
|
| 288 | + |
| 289 | + describe('tsconfig', ({ test }) => { |
| 290 | + test('should error on unresolvable tsconfig', async () => { |
| 291 | + await using fixture = await createFixture({ |
| 292 | + 'tsconfig.json': createTsconfig({ |
| 293 | + extends: 'doesnt-exist', |
| 294 | + }), |
| 295 | + 'register.mjs': ` |
| 296 | + import { register } from ${JSON.stringify(tsxEsmApiPath)}; |
| 297 | + register(); |
| 298 | + `, |
| 299 | + }); |
| 300 | + |
| 301 | + const { exitCode, stderr } = await execaNode('register.mjs', [], { |
| 302 | + reject: false, |
| 303 | + cwd: fixture.path, |
| 304 | + nodePath: node.path, |
| 305 | + nodeOptions: [], |
| 306 | + }); |
| 307 | + expect(exitCode).toBe(1); |
| 308 | + expect(stderr).toMatch('File \'doesnt-exist\' not found.'); |
| 309 | + }); |
| 310 | + |
| 311 | + test('disable lookup', async () => { |
| 312 | + await using fixture = await createFixture({ |
| 313 | + 'tsconfig.json': createTsconfig({ |
| 314 | + extends: 'doesnt-exist', |
| 315 | + }), |
| 316 | + 'register.mjs': ` |
| 317 | + import { register } from ${JSON.stringify(tsxEsmApiPath)}; |
| 318 | + register({ |
| 319 | + tsconfig: false, |
| 320 | + }); |
| 321 | + `, |
| 322 | + }); |
| 323 | + |
| 324 | + await execaNode('register.mjs', [], { |
| 325 | + cwd: fixture.path, |
| 326 | + nodePath: node.path, |
| 327 | + nodeOptions: [], |
| 328 | + }); |
| 329 | + }); |
| 330 | + |
| 331 | + test('custom path', async () => { |
| 332 | + await using fixture = await createFixture({ |
| 333 | + 'package.json': createPackageJson({ type: 'module' }), |
| 334 | + 'tsconfig.json': createTsconfig({ |
| 335 | + extends: 'doesnt-exist', |
| 336 | + }), |
| 337 | + 'tsconfig-custom.json': createTsconfig({ |
| 338 | + compilerOptions: { |
| 339 | + jsxFactory: 'Array', |
| 340 | + jsxFragmentFactory: 'null', |
| 341 | + }, |
| 342 | + }), |
| 343 | + 'register.mjs': ` |
| 344 | + import { register } from ${JSON.stringify(tsxEsmApiPath)}; |
| 345 | + register({ |
| 346 | + tsconfig: './tsconfig-custom.json', |
| 347 | + }); |
| 348 | + await import('./tsx.tsx'); |
| 349 | + `, |
| 350 | + 'tsx.tsx': ` |
| 351 | + console.log(<>hi</>); |
| 352 | + `, |
| 353 | + }); |
| 354 | + |
| 355 | + const { stdout } = await execaNode('register.mjs', [], { |
| 356 | + cwd: fixture.path, |
| 357 | + nodePath: node.path, |
| 358 | + nodeOptions: [], |
| 359 | + }); |
| 360 | + expect(stdout).toBe('[ null, null, \'hi\' ]'); |
| 361 | + }); |
| 362 | + |
| 363 | + test('custom path - invalid', async () => { |
| 364 | + await using fixture = await createFixture({ |
| 365 | + 'package.json': createPackageJson({ type: 'module' }), |
| 366 | + 'register.mjs': ` |
| 367 | + import { register } from ${JSON.stringify(tsxEsmApiPath)}; |
| 368 | + register({ |
| 369 | + tsconfig: './doesnt-exist', |
| 370 | + }); |
| 371 | + await import('./tsx.tsx'); |
| 372 | + `, |
| 373 | + 'tsx.tsx': ` |
| 374 | + console.log(<>hi</>); |
| 375 | + `, |
| 376 | + }); |
| 377 | + |
| 378 | + const { exitCode, stderr } = await execaNode('register.mjs', [], { |
| 379 | + reject: false, |
| 380 | + cwd: fixture.path, |
| 381 | + nodePath: node.path, |
| 382 | + nodeOptions: [], |
| 383 | + }); |
| 384 | + expect(exitCode).toBe(1); |
| 385 | + expect(stderr).toMatch('Cannot resolve tsconfig at path'); |
| 386 | + }); |
| 387 | + |
| 388 | + test('fallsback to env var', async () => { |
| 389 | + await using fixture = await createFixture({ |
| 390 | + 'package.json': createPackageJson({ type: 'module' }), |
| 391 | + 'tsconfig.json': createTsconfig({ |
| 392 | + extends: 'doesnt-exist', |
| 393 | + }), |
| 394 | + 'tsconfig-custom.json': createTsconfig({ |
| 395 | + compilerOptions: { |
| 396 | + jsxFactory: 'Array', |
| 397 | + jsxFragmentFactory: 'null', |
| 398 | + }, |
| 399 | + }), |
| 400 | + 'register.mjs': ` |
| 401 | + import { register } from ${JSON.stringify(tsxEsmApiPath)}; |
| 402 | + register(); |
| 403 | + await import('./tsx.tsx'); |
| 404 | + `, |
| 405 | + 'tsx.tsx': ` |
| 406 | + console.log(<>hi</>); |
| 407 | + `, |
| 408 | + }); |
| 409 | + |
| 410 | + const { stdout } = await execaNode('register.mjs', [], { |
| 411 | + cwd: fixture.path, |
| 412 | + nodePath: node.path, |
| 413 | + nodeOptions: [], |
| 414 | + env: { |
| 415 | + TSX_TSCONFIG_PATH: 'tsconfig-custom.json', |
| 416 | + }, |
| 417 | + }); |
| 418 | + expect(stdout).toBe('[ null, null, \'hi\' ]'); |
| 419 | + }); |
| 420 | + }); |
288 | 421 | });
|
289 | 422 |
|
290 |
| - // add CJS test |
291 | 423 | describe('tsImport()', ({ test }) => {
|
292 | 424 | test('module', async () => {
|
293 | 425 | await using fixture = await createFixture({
|
@@ -436,6 +568,28 @@ export default testSuite(({ describe }, node: NodeApis) => {
|
436 | 568 | });
|
437 | 569 | expect(stdout).toBe('foo\nfoo');
|
438 | 570 | });
|
| 571 | + |
| 572 | + test('tsconfig disable', async () => { |
| 573 | + await using fixture = await createFixture({ |
| 574 | + 'package.json': createPackageJson({ type: 'module' }), |
| 575 | + 'tsconfig.json': createTsconfig({ extends: 'doesnt-exist' }), |
| 576 | + 'import.mjs': ` |
| 577 | + import { tsImport } from ${JSON.stringify(tsxEsmApiPath)}; |
| 578 | +
|
| 579 | + await tsImport('./file.ts', { |
| 580 | + parentURL: import.meta.url, |
| 581 | + tsconfig: false, |
| 582 | + }); |
| 583 | + `, |
| 584 | + ...tsFiles, |
| 585 | + }); |
| 586 | + |
| 587 | + await execaNode('import.mjs', [], { |
| 588 | + cwd: fixture.path, |
| 589 | + nodePath: node.path, |
| 590 | + nodeOptions: [], |
| 591 | + }); |
| 592 | + }); |
439 | 593 | });
|
440 | 594 | } else {
|
441 | 595 | test('no module.register error', async () => {
|
|
0 commit comments