Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Jul 22, 2021
1 parent 7caa142 commit ccba5cb
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 14 deletions.
12 changes: 12 additions & 0 deletions src/test/index.spec.ts
Expand Up @@ -103,6 +103,7 @@ test.beforeAll(async () => {
test.suite('ts-node', (test) => {
const cmd = `"${BIN_PATH}" --project "${PROJECT}"`;
const cmdNoProject = `"${BIN_PATH}"`;
const cmdEsmLoaderNoProject = `node --loader ts-node/esm`;

test('should export the correct version', () => {
expect(VERSION).to.equal(require('../../package.json').version);
Expand Down Expand Up @@ -367,6 +368,17 @@ test.suite('ts-node', (test) => {
expect(stdout).to.contain('Hello World!');
});

test('swc transpiler supports native ESM emit', async () => {
const { err, stdout } = await exec(
`${cmdEsmLoaderNoProject} ./index.ts`,
{
cwd: resolve(TEST_DIR, 'transpile-only-swc-native-esm'),
}
);
expect(err).to.equal(null);
expect(stdout).to.contain('Hello file://');
});

test('should pipe into `ts-node` and evaluate', async () => {
const execPromise = exec(cmd);
execPromise.child.stdin!.end("console.log('hello')");
Expand Down
26 changes: 14 additions & 12 deletions src/transpilers/swc.ts
Expand Up @@ -58,21 +58,23 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
function createSwcOptions(isTsx: boolean): swcTypes.Options {
const swcTarget = targetMapping.get(target!) ?? 'es3';
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
const moduleConfig = {
noInterop: !esModuleInterop,
type:
module === ModuleKind.CommonJS
? 'commonjs'
: module === ModuleKind.AMD
? 'amd'
: module === ModuleKind.UMD
? 'umd'
: undefined,
} as swcTypes.ModuleConfig;
const moduleType =
module === ModuleKind.CommonJS
? 'commonjs'
: module === ModuleKind.AMD
? 'amd'
: module === ModuleKind.UMD
? 'umd'
: undefined;
return {
sourceMaps: sourceMap,
// isModule: true,
module: moduleConfig,
module: moduleType
? ({
noInterop: !esModuleInterop,
type: moduleType,
} as swcTypes.ModuleConfig)
: undefined,
swcrc: false,
jsc: {
externalHelpers: importHelpers,
Expand Down
2 changes: 2 additions & 0 deletions tests/transpile-only-swc-native-esm/index.ts
@@ -0,0 +1,2 @@
const x: number = `Hello ${import.meta.url.slice(0, 7)}!`;
console.log(x);
3 changes: 3 additions & 0 deletions tests/transpile-only-swc-native-esm/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
13 changes: 13 additions & 0 deletions tests/transpile-only-swc-native-esm/tsconfig.json
@@ -0,0 +1,13 @@
{
"ts-node": {
"transpileOnly": true,
"transpiler": "ts-node/transpilers/swc-experimental"
},
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"allowJs": true,
"jsx": "react",
"experimentalDecorators": true
}
}
4 changes: 4 additions & 0 deletions tests/transpile-only-swc-via-tsconfig/index.ts
Expand Up @@ -7,3 +7,7 @@ class World {}
parseInt(1101, 2);
const x: number = `Hello ${World.name}!`;
console.log(x);

// test module type emit
import { readFileSync } from 'fs';
readFileSync;
2 changes: 1 addition & 1 deletion tests/transpile-only-swc-via-tsconfig/tsconfig.json
Expand Up @@ -5,7 +5,7 @@
},
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"module": "CommonJS",
"allowJs": true,
"jsx": "react",
"experimentalDecorators": true
Expand Down
4 changes: 4 additions & 0 deletions tests/transpile-only-swc/index.ts
Expand Up @@ -7,3 +7,7 @@ class World {}
parseInt(1101, 2);
const x: number = `Hello ${World.name}!`;
console.log(x);

// test module type emit
import { readFileSync } from 'fs';
readFileSync;
2 changes: 1 addition & 1 deletion tests/transpile-only-swc/tsconfig.json
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"module": "CommonJS",
"allowJs": true,
"jsx": "react",
"experimentalDecorators": true
Expand Down

0 comments on commit ccba5cb

Please sign in to comment.