swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.72k stars 71 forks source link

@swc-node/jest doesn't pass options from tsconfig to swc properly #700

Closed meskill closed 1 year ago

meskill commented 1 year ago

After upgrading from 1.5.5 to 1.6.2 I get next error related to legacyDecorators:

 FAIL  packages/createApp.test.ts
  ● Test suite failed to run

      × Expression expected
        ╭─[/workspaces/app/createApp.test.ts:11:1]
     11 │   it('Создание и запуск приложения', async () => {
     13 │ 
     14 │     @Module({
        ·     ─
     15 │       providers: [
     16 │         {
     17 │           provide: 'level-3',
        ╰────

    Caused by:
        Syntax Error

      at Compiler.transformSync (node_modules/@swc/core/index.js:241:29)
      at transformSync (node_modules/@swc/core/index.js:348:21)
      at transformJest (node_modules/@swc-node/core/index.ts:74:2)
      at Object.process (node_modules/@swc-node/jest/index.ts:33:27)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:519:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:648:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:700:19)

The source problem of the issue:

  1. This code converts @swc-node/jest options to swc options
  2. This code provides options from the tsconfig file to @swc-node/jest options
  3. As now the second piece of code defines swc property it overrides the whole jsc setup in the first piece of code.

The log of the input options and result options are following:

// before transform
 {
  module: 'commonjs',
  sourcemap: 'inline',
  experimentalDecorators: true,
  emitDecoratorMetadata: false,
  esModuleInterop: true,
  swc: {
    filename: '/workspaces/app/createApp.test.ts',
    inputSourceMap: undefined,
    sourceRoot: undefined,
    jsc: {
      externalHelpers: true,
      parser: [Object],
      paths: {},
      keepClassNames: true,
      target: 'es2015',
      transform: [Object]
    }
  }
}
// after transform
 {
  filename: '/workspaces/app/createApp.test.ts',
  jsc: {
    externalHelpers: true,
    parser: { syntax: 'typescript', tsx: true, dynamicImport: true },
    paths: {},
    keepClassNames: true,
    target: 'es2015',
    transform: { react: [Object] }
  },
  minify: false,
  isModule: true,
  module: { type: 'commonjs', noInterop: false },
  sourceMaps: 'inline',
  inlineSourcesContent: true,
  swcrc: false,
  inputSourceMap: undefined,
  sourceRoot: undefined
}

Possible solution

Either use deepMerge to merge all of the options or use another way to provide defaults from tsconfig file

felixmosh commented 1 year ago

Having the same thing with @swc-node/loader

otaviosoares commented 1 year ago

I have a similar issue with @swc-node/register. After debugging, I believe it was introduced on https://github.com/swc-project/swc-node/commit/1d557ece0d9ccbba027ff9f2d262c03d4b918bcb https://github.com/swc-project/swc-node/commit/af643b849c32abb58bd1c0fdf98eeeac08548e25. From 1.5.6 and onwards it's setting a default swc config during compilation.

In my case, running with SWCRC=true ...... solves the problem because it forces it to use the config in .swcrc.

However, I believe it's a bug.