svelte-jester: TypeError [ERR_INVALID_ARG_TYPE]

Having trouble running tests, here’s a demo repo.

I’m using it with SvelteKit and pnpm on Windows 10.

PS D:\Projects\svelte-component-template> pnpm test

> ~TODO~@0.0.1 test D:\Projects\svelte-component-template
> cross-env NODE_OPTIONS=--experimental-vm-modules jest src --config jest.config.json

(node:25868) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:28608) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:38792) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
 FAIL  src/lib/first/First.test.js
  ● Test suite failed to run

    TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received an instance of URL

      at node_modules/.pnpm/svelte-jester@2.1.1_jest@27.1.1+svelte@3.42.4/node_modules/svelte-jester/dist/transformer.cjs:80:118

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 4
  • Comments: 16 (1 by maintainers)

Most upvoted comments

I’m not sure if this is related (or just my mistake) but I was trying to run the latest master branch locally and got this:

asode@mycomp:~/coding/my-svelte-project$ npx jest

internal/process/promises.js:213
        triggerUncaughtException(err, true /* fromPromise */);
        ^

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of SyntaxError
    at Socket.Writable.write (internal/streams/writable.js:285:13)
    at file:///home/asode/coding/svelte-jester/dist/preprocess.js:21:32 {
  code: 'ERR_INVALID_ARG_TYPE'
}
 1: 0xa24ed0 node::Abort() [node]
 2: 0xa2701d  [node]
 3: 0xc06acb  [node]
 4: 0xc08076  [node]
 5: 0xc086f6 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [node]
 6: 0x1427159  [node]
Aborted

From looking at ts-jest.log, I noticed that the

  transform: {
    "^.+\\.svelte$": [
      "svelte-jester",
      { preprocess: "./svelte.config.test.cjs" },
    ],
    "^.+\\.ts$": "ts-jest",
  },

got transformed into /path/to//node_modules/svelte-jester/dist/transformer.cjs. So I thought that maybe it’s jest that transformed the raw string into a require, which in turn loaded the cjs file. I replaced it with

  transform: {
    "^.+\\.svelte$": [
      "<rootDir>/node_modules/svelte-jester/dist/transformer.mjs",
      { preprocess: "./svelte.config.test.cjs" },
    ],
    "^.+\\.ts$": "ts-jest",
  },

And it seemed to work. As js/ts is not my thing, I’ll just add all the seemingly random things that my jest config has now

// jest.config.ts
import type { Config } from "@jest/types";

const config: Config.InitialOptions = {
  verbose: true,
  preset: "ts-jest/presets/default-esm",
  transform: {
    "^.+\\.svelte$": [
      "<rootDir>/node_modules/svelte-jester/dist/transformer.mjs",
      { preprocess: "./svelte.config.test.cjs" }, // holds the most default svelte-preprocess ever
    ],
    "^.+\\.ts$": "ts-jest",
  },
  moduleFileExtensions: ["js", "ts", "svelte"],
  testEnvironment: "jsdom",
  moduleNameMapper: {
    "^\\$lib(.*)$": "<rootDir>/src/lib$1",
    "^\\$app(.*)$": [
      "<rootDir>/.svelte-kit/dev/runtime/app$1",
      "<rootDir>/.svelte-kit/build/runtime/app$1",
    ],
  },
  setupFilesAfterEnv: ["<rootDir>/jest-setup.ts"],
  collectCoverageFrom: ["src/**/*.{ts,tsx,svelte,js,jsx}"],
  transformIgnorePatterns: [],
  extensionsToTreatAsEsm: [".ts", ".svelte"],
  globals: {
    "ts-jest": {
      babelConfig: true,
      useESM: true,
    },
  },
};

export default config;

I tried some variations of exports in package.json, but the only way to convince Jest to use the ESM transformer was to have the ESM version as the default export. I don’t know, if this will work for the CJS transformer then.

See

Seems to be related to Windows again. I will have a look at it tonight.