swc-project / pkgs

node.js packages for SWC
59 stars 19 forks source link

swc jest breaks with default .swcrc #25

Closed timbicker closed 8 months ago

timbicker commented 8 months ago

I created a small repo to reproduce the problem: here

I only use a minimal, basic setup according to the docs. The .swcrc is the default config from the docs.

When I run my test:

import {myFunction} from "./myFunction";

type MyType = {
  hi: string
}

test("myFunction", () => {
  const obj: MyType = {
    hi: "hello"
  }
  expect(myFunction(obj)).toBe("hello")
})

I get the following error:


      × Expected ';', '}' or <eof>
       ╭─[swc-path-bug/src/myFunction.test.ts:1:1]
     1 │ import {myFunction} from "./myFunction";
     2 │
     3 │ type MyType = {
       · ──┬─ ──────
       ·   ╰── This is the expression part of an expression statement
     4 │   hi: string
     5 │ }
       ╰────

    Caused by:
        Syntax Error

      at Compiler.transformSync (node_modules/@swc/core/index.js:244:29)
      at transformSync (node_modules/@swc/core/index.js:351:21)
      at Object.process (node_modules/@swc/jest/index.js:73:45)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:545:31)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:674:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:726:19)

When I delete the .swcrc the test passes.

Steps to reproduce:

  1. check out repo
  2. yarn install
  3. yarn test
kdy1 commented 8 months ago

You need to specify typescript

timbicker commented 8 months ago

@kdy1 thanks for your comment! I didn't think of this because in the docs it states with respect to the .swcrc file

Compilation works out of the box with SWC and does not require customization. Optionally, you can override the configuration. Here are the defaults:

So in other words, when I use the default swcrc config, I expect it to work exactly the same as when I don't have a .swcrc file. Maybe it's a good idea to extend the documentation with a hint to this problem (to choose the right parser syntax)?