javascript - Mocha 测试 : Cannot use import statement outside a module

标签 javascript node.js typescript mocha.js

我不确定这是 TS 设置还是需要调整的内容或其他内容。我已经看过这个,它对我没有帮助:Mocha + TypeScript: Cannot use import statement outside a module以及这些关于使用 <script src="file1.js" type="module"></script> 的帖子没有意义,我在这里运行测试。

我用 mocha 运行我的测试 -r ts-node/register src/test/**/*.spec.tsx -r src/test/compiler.ts --color -w

我得到一个关于 import { MemoryRouter as Router } from "react-router-dom"; 的错误:

Cannot use import statement outside a module

featured.company.group.tsx

import { MemoryRouter as Router } from "react-router-dom";
import { expect, mount, React } from "../../test.imports";
import { FeaturedCompanyGroup, FormattedFeaturedCompany } from "../../../client/Company/FeaturedCompanies";

describe("Featured Company Group", () => {
    const country: Country = {
        id: 0,
        name: "string",
        images: {
            flag: "string"
        }
    };
...

我正在使用:

"node": "14.x"
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"@types/mocha": "^8.0.0",
"@types/mocha": "^8.0.0",

我有一个根 ./tsconfig.json:

{
  "compilerOptions": {
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
        "outDir": "./build",
    "target": "ES2020",                     /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "es2020",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "lib": ["es5", "es6", "dom"],                      /* Specify library files to be included in the compilation. */
        "moduleResolution": "node",
        "allowJs": false,                     /* Allow javascript files to be compiled. */
//      "checkJs": true,                     /* Report errors in .js files. */
    "jsx": "react",
    "noImplicitAny": false,
        "sourceMap": false,                   /* Generates corresponding '.map' file. */
    "rootDir": "./",                     /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
        "removeComments": true,              /* Do not emit comments to output. */
    "strict": true,                      /* Enable all strict type-checking options. */
        "noUnusedLocals": true,                /* Report errors on unused locals. */
        "noUnusedParameters": true,            /* Report errors on unused parameters. */
//    "rootDirs": ["."],                        /* List of root folders whose combined content represents the structure of the project at runtime. */
    "typeRoots": [
      "node_modules/@types"
    ],                      /* List of folders to include type definitions from. */
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
        "preserveSymlinks": true,              /* Do not resolve the real path of symlinks. */
        "resolveJsonModule": true,
        "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true,
//      "strictNullChecks": true
    },
    "include": [
        "src"
    ],
    "exclude": [
        "/node_modules",
        "/src/server",
        "/src/client/js/ink-config.js",
        "**/test",
        "dist"
  ]
}

我有一个/src/test/tsconfig.json。 (我不认为这实际上是出于某种原因扩展了我的根,它似乎没有影响这个 tsconfig):

{
    "extends": "../../tsconfig.json",
    "compilerOptions": {
        "noEmit": true,
        "module": "es2020",
        "target": "es2020",
        "types": ["node","mocha", "chai", "react"],
        "lib": ["es6"],
        "compileOnSave": false,
        "jsx": "react",
        "sourceMap": false,
        "skipLibCheck": true,
        "esModuleInterop": true
    },
    "include": ["./**/*spec.tsx"],
    "exclude": ["node_modules","./build"]
}

最佳答案

看起来您正在使用“监视”模式,根据 ts-node docs,它需要 extensions 选项.

关于javascript - Mocha 测试 : Cannot use import statement outside a module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63179103/

相关文章:

javascript - 即使禁用 XMLHTTP 支持,jQuery.support.ajax 也会返回 true

javascript - 在javascript中搜索和删除数组元素

javascript - 从 HTML 应用程序通过 TCP IP 客户端发送短字符串的最佳方式

html - 尝试在样式中插入 typescript 元素时出现术语预期错误

javascript - TypeScript:如何无错误地访问 onChange 事件数据? (对象可能是 'null' )

javascript - 对象的属性未定义,即使对象上的 console.log 显示属性值

Javascript:如何正确检查数组是否为空

node.js - Node JS LDAP 验证用户

Node.js express : why can't I read cookies ?

javascript - 重新加载部分 HTML 而不刷新页面?