swc-project / swc-project/swc-node

Doesn't Work with Ava Out of the Box

Open
#672 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2k
Forks
95
Avg merge
14d 11h
Merged PRs (30d)
1

Description

Running Node v16@latest. @swc-node/register doesn't work with Ava out of the box. The instructions in the README aren't comprehensive enough to use straight away.

config:

  "ava": {
    "extensions": ["ts"],
    "require": ["@swc-node/register"],
    "files": ["!**/fixtures/**", "!**/helpers/**", "!**/recipes/**", "!**/types.ts"]
  }

test:

import test from 'ava';

import * as tools from '../src';

test('exports', (t) => {
  t.snapshot(tools);
});

result:

  Uncaught exception in test/test.ts

  SyntaxError: Cannot use import statement outside a module

  › Module._compile (/Users/code/packages/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:136:24)
  › newLoader (/Users/code/packages/node_modules/.pnpm/pirates@4.0.5/node_modules/pirates/lib/index.js:141:7)

  ✖ test/test.ts exited with a non-zero exit code: 1

Now, I understand that error message has to do with ESM module support. OK. I'll tell it to use a tsconfig that should work around that:

tsconfig.base.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "lib": ["ES2018", "ES2020", "esnext.asynciterable", "dom"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "pretty": true,
    "removeComments": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es2020",
    "useUnknownInCatchVariables": false
  },
  "exclude": ["dist", "node_modules", "test/types"]
}

tsconfig.json (used by test):

{
  "compilerOptions": {
    "outDir": "dist"
  },
  "extends": "../../shared/tsconfig.base.json",
  "include": ["src", "bin/*", "test"]
}

updated config:

  "ava": {
    "extensions": ["ts"],
    "require": ["@swc-node/register"],
    "files": ["!**/fixtures/**", "!**/helpers/**", "!**/recipes/**", "!**/types.ts"],
    "environmentVariables": {
      "SWC_NODE_PROJECT": "./tsconfig.json"
    }
  }

Same result. If I swap ts-node/register back in, everything works again. There's either a bit of information left out, or this straight up does not work with Ava. I'd be baffled if it was the latter because I can see tests working here in this repo and they use ava.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the README and reproduce the failure with the shown Ava configuration, test/test.ts, and tsconfig files. Compare this setup with the Ava tests already working in the repository to identify the missing compatibility detail. Done means Ava runs the TypeScript test with @swc-node/register and the README explains the required configuration.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.