@@ -82,6 +82,10 @@ test.suite('ts-node', (test) => {
82
82
const cmd = `"${ BIN_PATH } " --project "${ PROJECT } "` ;
83
83
/** Default `ts-node` invocation without `--project` */
84
84
const cmdNoProject = `"${ BIN_PATH } "` ;
85
+ const experimentalModulesFlag = semver . gte ( process . version , '12.17.0' )
86
+ ? ''
87
+ : '--experimental-modules' ;
88
+ const cmdEsmLoaderNoProject = `node ${ experimentalModulesFlag } --loader ts-node/esm` ;
85
89
86
90
test ( 'should export the correct version' , ( ) => {
87
91
expect ( VERSION ) . to . equal ( require ( '../../package.json' ) . version ) ;
@@ -346,6 +350,19 @@ test.suite('ts-node', (test) => {
346
350
expect ( stdout ) . to . contain ( 'Hello World!' ) ;
347
351
} ) ;
348
352
353
+ if ( semver . gte ( process . version , '12.16.0' ) ) {
354
+ test ( 'swc transpiler supports native ESM emit' , async ( ) => {
355
+ const { err, stdout } = await exec (
356
+ `${ cmdEsmLoaderNoProject } ./index.ts` ,
357
+ {
358
+ cwd : resolve ( TEST_DIR , 'transpile-only-swc-native-esm' ) ,
359
+ }
360
+ ) ;
361
+ expect ( err ) . to . equal ( null ) ;
362
+ expect ( stdout ) . to . contain ( 'Hello file://' ) ;
363
+ } ) ;
364
+ }
365
+
349
366
test ( 'should pipe into `ts-node` and evaluate' , async ( ) => {
350
367
const execPromise = exec ( cmd ) ;
351
368
execPromise . child . stdin ! . end ( "console.log('hello')" ) ;
@@ -1770,23 +1787,24 @@ test.suite('ts-node', (test) => {
1770
1787
} ) ;
1771
1788
1772
1789
test . suite ( 'esm' , ( test ) => {
1773
- const experimentalModulesFlag = semver . gte ( process . version , '12.17.0' )
1774
- ? ''
1775
- : '--experimental-modules' ;
1776
- const esmCmd = `node ${ experimentalModulesFlag } --loader ts-node/esm` ;
1777
-
1778
1790
if ( semver . gte ( process . version , '12.16.0' ) ) {
1779
1791
test ( 'should compile and execute as ESM' , async ( ) => {
1780
- const { err, stdout } = await exec ( `${ esmCmd } index.ts` , {
1781
- cwd : join ( TEST_DIR , './esm' ) ,
1782
- } ) ;
1792
+ const { err, stdout } = await exec (
1793
+ `${ cmdEsmLoaderNoProject } index.ts` ,
1794
+ {
1795
+ cwd : join ( TEST_DIR , './esm' ) ,
1796
+ }
1797
+ ) ;
1783
1798
expect ( err ) . to . equal ( null ) ;
1784
1799
expect ( stdout ) . to . equal ( 'foo bar baz biff libfoo\n' ) ;
1785
1800
} ) ;
1786
1801
test ( 'should use source maps' , async ( ) => {
1787
- const { err, stdout } = await exec ( `${ esmCmd } "throw error.ts"` , {
1788
- cwd : join ( TEST_DIR , './esm' ) ,
1789
- } ) ;
1802
+ const { err, stdout } = await exec (
1803
+ `${ cmdEsmLoaderNoProject } "throw error.ts"` ,
1804
+ {
1805
+ cwd : join ( TEST_DIR , './esm' ) ,
1806
+ }
1807
+ ) ;
1790
1808
expect ( err ) . not . to . equal ( null ) ;
1791
1809
expect ( err ! . message ) . to . contain (
1792
1810
[
@@ -1806,7 +1824,7 @@ test.suite('ts-node', (test) => {
1806
1824
err,
1807
1825
stdout,
1808
1826
} = await exec (
1809
- `${ esmCmd } --experimental-specifier-resolution=node index.ts` ,
1827
+ `${ cmdEsmLoaderNoProject } --experimental-specifier-resolution=node index.ts` ,
1810
1828
{ cwd : join ( TEST_DIR , './esm-node-resolver' ) }
1811
1829
) ;
1812
1830
expect ( err ) . to . equal ( null ) ;
@@ -1817,39 +1835,48 @@ test.suite('ts-node', (test) => {
1817
1835
err,
1818
1836
stdout,
1819
1837
} = await exec (
1820
- `${ esmCmd } --experimental-modules --es-module-specifier-resolution=node index.ts` ,
1838
+ `${ cmdEsmLoaderNoProject } --experimental-modules --es-module-specifier-resolution=node index.ts` ,
1821
1839
{ cwd : join ( TEST_DIR , './esm-node-resolver' ) }
1822
1840
) ;
1823
1841
expect ( err ) . to . equal ( null ) ;
1824
1842
expect ( stdout ) . to . equal ( 'foo bar baz biff libfoo\n' ) ;
1825
1843
} ) ;
1826
1844
test ( 'via NODE_OPTIONS' , async ( ) => {
1827
- const { err, stdout } = await exec ( `${ esmCmd } index.ts` , {
1828
- cwd : join ( TEST_DIR , './esm-node-resolver' ) ,
1829
- env : {
1830
- ...process . env ,
1831
- NODE_OPTIONS : `${ experimentalModulesFlag } --experimental-specifier-resolution=node` ,
1832
- } ,
1833
- } ) ;
1845
+ const { err, stdout } = await exec (
1846
+ `${ cmdEsmLoaderNoProject } index.ts` ,
1847
+ {
1848
+ cwd : join ( TEST_DIR , './esm-node-resolver' ) ,
1849
+ env : {
1850
+ ...process . env ,
1851
+ NODE_OPTIONS : `${ experimentalModulesFlag } --experimental-specifier-resolution=node` ,
1852
+ } ,
1853
+ }
1854
+ ) ;
1834
1855
expect ( err ) . to . equal ( null ) ;
1835
1856
expect ( stdout ) . to . equal ( 'foo bar baz biff libfoo\n' ) ;
1836
1857
} ) ;
1837
1858
} ) ;
1838
1859
1839
1860
test ( 'throws ERR_REQUIRE_ESM when attempting to require() an ESM script when ESM loader is enabled' , async ( ) => {
1840
- const { err, stderr } = await exec ( `${ esmCmd } ./index.js` , {
1841
- cwd : join ( TEST_DIR , './esm-err-require-esm' ) ,
1842
- } ) ;
1861
+ const { err, stderr } = await exec (
1862
+ `${ cmdEsmLoaderNoProject } ./index.js` ,
1863
+ {
1864
+ cwd : join ( TEST_DIR , './esm-err-require-esm' ) ,
1865
+ }
1866
+ ) ;
1843
1867
expect ( err ) . to . not . equal ( null ) ;
1844
1868
expect ( stderr ) . to . contain (
1845
1869
'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:'
1846
1870
) ;
1847
1871
} ) ;
1848
1872
1849
1873
test ( 'defers to fallback loaders when URL should not be handled by ts-node' , async ( ) => {
1850
- const { err, stdout, stderr } = await exec ( `${ esmCmd } index.mjs` , {
1851
- cwd : join ( TEST_DIR , './esm-import-http-url' ) ,
1852
- } ) ;
1874
+ const { err, stdout, stderr } = await exec (
1875
+ `${ cmdEsmLoaderNoProject } index.mjs` ,
1876
+ {
1877
+ cwd : join ( TEST_DIR , './esm-import-http-url' ) ,
1878
+ }
1879
+ ) ;
1853
1880
expect ( err ) . to . not . equal ( null ) ;
1854
1881
// expect error from node's default resolver
1855
1882
expect ( stderr ) . to . match (
@@ -1858,16 +1885,19 @@ test.suite('ts-node', (test) => {
1858
1885
} ) ;
1859
1886
1860
1887
test ( 'should bypass import cache when changing search params' , async ( ) => {
1861
- const { err, stdout } = await exec ( `${ esmCmd } index.ts` , {
1862
- cwd : join ( TEST_DIR , './esm-import-cache' ) ,
1863
- } ) ;
1888
+ const { err, stdout } = await exec (
1889
+ `${ cmdEsmLoaderNoProject } index.ts` ,
1890
+ {
1891
+ cwd : join ( TEST_DIR , './esm-import-cache' ) ,
1892
+ }
1893
+ ) ;
1864
1894
expect ( err ) . to . equal ( null ) ;
1865
1895
expect ( stdout ) . to . equal ( 'log1\nlog2\nlog2\n' ) ;
1866
1896
} ) ;
1867
1897
1868
1898
test ( 'should support transpile only mode via dedicated loader entrypoint' , async ( ) => {
1869
1899
const { err, stdout } = await exec (
1870
- `${ esmCmd } /transpile-only index.ts` ,
1900
+ `${ cmdEsmLoaderNoProject } /transpile-only index.ts` ,
1871
1901
{
1872
1902
cwd : join ( TEST_DIR , './esm-transpile-only' ) ,
1873
1903
}
@@ -1876,9 +1906,12 @@ test.suite('ts-node', (test) => {
1876
1906
expect ( stdout ) . to . equal ( '' ) ;
1877
1907
} ) ;
1878
1908
test ( 'should throw type errors without transpile-only enabled' , async ( ) => {
1879
- const { err, stdout } = await exec ( `${ esmCmd } index.ts` , {
1880
- cwd : join ( TEST_DIR , './esm-transpile-only' ) ,
1881
- } ) ;
1909
+ const { err, stdout } = await exec (
1910
+ `${ cmdEsmLoaderNoProject } index.ts` ,
1911
+ {
1912
+ cwd : join ( TEST_DIR , './esm-transpile-only' ) ,
1913
+ }
1914
+ ) ;
1882
1915
if ( err === null ) {
1883
1916
throw new Error ( 'Command was expected to fail, but it succeeded.' ) ;
1884
1917
}
@@ -1899,7 +1932,7 @@ test.suite('ts-node', (test) => {
1899
1932
1900
1933
async function runModuleTypeTest ( project : string , ext : string ) {
1901
1934
const { err, stderr, stdout } = await exec (
1902
- `${ esmCmd } ./module-types/${ project } /test.${ ext } ` ,
1935
+ `${ cmdEsmLoaderNoProject } ./module-types/${ project } /test.${ ext } ` ,
1903
1936
{
1904
1937
env : {
1905
1938
...process . env ,
0 commit comments