TypeScript with Jest cannot detect library

Viewed 642

It seems that Jest cannot pick up imported library. I've been reading about it and trying various options. One of those was this. However none of them seems to work.

//xyz.ts

..
/* eslint-disable */
import * as csv from 'csv-stringify/sync';
/* eslint-enable */
..

After running Jest:

Test suite failed to run

    Cannot find module 'csv-stringify/sync' from 'xyz.ts'

Library being used - https://csv.js.org/stringify/ .

I suspect I do something wrong, because even ESLint complains that about it, if I don't disable its checks.

 Unable to resolve path to module 'csv-stringify/sync'  import/no-unresolved

Library's structure:

csv-stringify package

Furthermore, if I'm trying to import as

import * as csv from 'csv-stringify/lib/sync';

Then I receive this error after running Jest

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    <redacted>/node_modules/csv-stringify/lib/sync.js:2
    import { Stringifier } from './index.js';
    ^^^^^^

I guess some kind of transformation might be needed here?

Other files:

jest.config.js

module.exports = {
  // Root path
  roots: [''],
  // testMatch: ['**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)'],
  // Only test TS files, don't test JS
  testMatch: ['**/?(*.)+(spec|test).+(ts)'],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  //  Include below folders to be checked for modules
  moduleDirectories: ['node_modules', 'src'],
};

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "strictNullChecks": true
  },
  "compileOnSave": true,
  "include": [
    "coverage",
    "jest.config.js",
    "src",
    "test"
  ]
}

Addressing suggestions below:

Import it from dist - lib is the source (untranspiled) file which is why you get the second error.

Tried cjs and esm - both didn't work

import * as csv  from 'csv-stringify/dist/cjs/sync';
..

Error

 Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/cjs/sync' is not defined by "exports" 

Have you tried import { stringify } from 'csv-stringify/sync'; as the library suggests?

Yes - I receive yet another error.

 Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/sync' is not defined by "exports" in <redacted>/node_modules/csv-stringify/package.json

package.json exports part

  "exports": {
    ".": {
      "import": "./lib/index.js",
      "require": "./dist/cjs/index.cjs"
    },
    "./sync": {
      "import": "./lib/sync.js",
      "require": "./dist/cjs/sync.cjs"
    },
    "./browser/esm/": "./dist/esm/"
  }
2 Answers

According to this Github issue, this is the result of Jest not using package.json's exports field - related issue here.

I solved it by adding the below to my jest.config.js, as suggested by a user in the issue:

...
moduleNameMapper: {
    "^csv-stringify/sync":
      "<rootDir>/node_modules/csv-stringify/dist/cjs/sync.cjs"
}
...

there might be an issue with the ESM/CJS importing systems..

try configuring Jest to use ESM with this addition in jest.config.ts:

  {
    // ...
    "extensionsToTreatAsEsm": [".ts"]
  }

if that fails try to use CJS importing style const { stringify } = require('csv-stringify/sync');

Related