jest icon indicating copy to clipboard operation
jest copied to clipboard

`exclude` option causes config transform options to be ignored

Open joeponzio opened this issue 3 years ago • 2 comments

In jest.config.js, parsing .swcrc with an exclude option or passing the exclude key in the transform options causes the transform options to be ignored and falls back to reading the .swcrc.

Versions

node@16 @swc/[email protected] @swc/[email protected] @swc/[email protected] [email protected]

.swcrc

{
  "env": {
    "targets": {
      "node": "16"
    }
  },
  "exclude": ["test.ts$"],
  "jsc": {
    "parser": {
      "syntax": "typescript"
    },
    "baseUrl": ".",
    "paths": {
      "@entities/*": ["entities/*"],
      "@src/*": ["src/*"]
    },
    "target": "es2017"
  },
  "module": {
    "type": "commonjs"
  },
  "sourceMaps": true
}

jest.config.js

const fs = require("fs")
const path = require("path")
const util = require("util")

const config = JSON.parse(
  fs.readFileSync(path.join(process.cwd(), ".swcrc"), "utf-8")
)
// delete config.exclude

module.exports = {
  testEnvironment: "node",
  testMatch: ["<rootDir>/src/**/?(*.)test.ts"],
  testPathIgnorePatterns: ["/node_modules/"],
  transform: {
    "^.+\\.ts$": [
      "@swc/jest",
      {
        ...config,
      },
    ],
  },
}

What is happening

If exclude exists in either the .swcrc or is passed as a transform option, the transform options are skipped and the transform uses the fallback (which then defaults to .swcrc). delete config.exclude does allow it pick up the custom configuration and not try to read .swcrc directly, as does passing a config object directly without exclude.

What I expected

I set up my .swcrc for the build process which excludes my test files and expected to be able to pass an empty array to exclude in the jest config to not have jest exclude those files.

joeponzio avatar Dec 30 '21 03:12 joeponzio

I have a workaround at the moment which is to name .swcrc something other than .swcrc (eg: .lib.swcrc. Then when I invoke swc cli, I set --no-swcrc --config-file=path/to/.lib.swcrc. Works well so far

nartc avatar Mar 04 '22 16:03 nartc

I found another hacky workaround that does not require .swcrc to be renamed. Adding swcrc: false option to an existing transform option will fix this issue as below:

jest.config.js

const fs = require("fs");

const config = JSON.parse(fs.readFileSync(`${__dirname}/.swcrc`, "utf-8"));
config.exclude = [];

module.exports = {
  transform: {
    "^.+\\.(t|j)sx?$": [
      "@swc/jest",
      {
        ...config,
        swcrc: false,         // <-- added this line
      },
    ],
  },
};

reproducible repo

First, the transform options will be passed to the SWC library through its JavaScript bindings and deserialized into swc::config::Options Rust structure with swcrc field set to true by default. In this library, it is checked if the exclude field of the options matches filename field that is always set to each test file by @swc/jest. Then only when swcrc field is true, .swcrc is loaded and immediately checked if its exclude pattern(s) matches the filename field. If either matches, transpilation ends up emitting the "ignored" error. So overwriting the swcrc field to false solves this problem for now.(May not work in future versions since this option is not documented...)

g2xpf avatar Jan 20 '23 16:01 g2xpf