最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - ES import in JavaScript modules used from Jest - Stack Overflow

programmeradmin3浏览0评论

npx jest test/test.ts produces:

...
    XXX/src/declarations/package_manager/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Actor, HttpAgent } from "@dfinity/agent";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
...

Note that src/declarations/package_manager/index.js is a generated JS file that uses ES imports (and I cannot change that). This file is indirectly (through another module) imported from test/test.ts.

jest.config.js:

/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
  preset: 'ts-jest/presets/default-esm', // Use this preset to handle ES modules
  testEnvironment: "node",
  transform: {
    "^.+.tsx?$": ["ts-jest",{useESM: true}],
  },
  extensionsToTreatAsEsm: ['.ts'],
  // moduleNameMapper: {
  //   // If you're using non-ESM packages, you might need to map them correctly
  //   '^(\\.{1,2}/.*)\\.js$': '$1',
  // },
};

tsconfig.json:

{
    "compilerOptions": {
      "target": "es6",        /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
      "lib": ["ES2018", "DOM"],  /* Specify library files to be included in the compilation. */
      "allowJs": true,           /* Allow javascript files to be compiled. */
      "jsx": "react",            /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
      "moduleResolution": "nodenext",
      "module": "nodenext",
      "skipLibCheck": true,
    },
    "include": ["src/**/*.ts", "./src/custom.d.ts"],
}

How to make my .ts tests to use .js files with import inside these .js without an error?

npx jest test/test.ts produces:

...
    XXX/src/declarations/package_manager/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Actor, HttpAgent } from "@dfinity/agent";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
...

Note that src/declarations/package_manager/index.js is a generated JS file that uses ES imports (and I cannot change that). This file is indirectly (through another module) imported from test/test.ts.

jest.config.js:

/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
  preset: 'ts-jest/presets/default-esm', // Use this preset to handle ES modules
  testEnvironment: "node",
  transform: {
    "^.+.tsx?$": ["ts-jest",{useESM: true}],
  },
  extensionsToTreatAsEsm: ['.ts'],
  // moduleNameMapper: {
  //   // If you're using non-ESM packages, you might need to map them correctly
  //   '^(\\.{1,2}/.*)\\.js$': '$1',
  // },
};

tsconfig.json:

{
    "compilerOptions": {
      "target": "es6",        /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
      "lib": ["ES2018", "DOM"],  /* Specify library files to be included in the compilation. */
      "allowJs": true,           /* Allow javascript files to be compiled. */
      "jsx": "react",            /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
      "moduleResolution": "nodenext",
      "module": "nodenext",
      "skipLibCheck": true,
    },
    "include": ["src/**/*.ts", "./src/custom.d.ts"],
}

How to make my .ts tests to use .js files with import inside these .js without an error?

Share Improve this question edited Jan 20 at 6:12 porton asked Jan 20 at 5:55 portonporton 5,80511 gold badges52 silver badges114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Make a babel.config.cjs file at the root and add the below as the content:

module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current'
                }
            }
        ]
    ]
};
发布评论

评论列表(0)

  1. 暂无评论