简体   繁体   English

Mocha 测试:不能在模块外使用 import 语句

[英]Mocha test: Cannot use import statement outside a module

I'm not sure if this is a TS setting or what that needs to be tweaked or something else.我不确定这是 TS 设置还是需要调整什么或其他什么。 I've already looked at this, it did not help me: Mocha + TypeScript: Cannot use import statement outside a module and these posts about using <script src="file1.js" type="module"></script> makes no sense, I'm running tests here.我已经看过这个,它对我没有帮助: Mocha + TypeScript: Cannot use import statement outside a module and these posts about using <script src="file1.js" type="module"></script> make没意义,我在这里运行测试。

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

And I get an error for import { MemoryRouter as Router } from "react-router-dom";import { MemoryRouter as Router } from "react-router-dom"; :

Cannot use import statement outside a module

featured.company.group.tsx特色公司.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"
        }
    };
...

I'm using:我在用着:

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

I have a root./tsconfig.json:我有一个根目录。/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"
  ]
}

and I have a /src/test/ tsconfig.json .我有一个 /src/test/ tsconfig.json (I don't think this is actually extending my root for some reason, it doesn't appear to be affecting this tsconfig): (我不认为这实际上是出于某种原因扩展了我的根,它似乎不会影响这个 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"]
}

Looks like you're using “watch” mode, which needs the extensions option as per the ts-node docs .看起来您正在使用“监视”模式,根据ts-node docs需要extensions选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 与 mocha - 无法在模块外使用导入语句 - Typescript with mocha - Cannot use import statement outside a module Jest 或 Mocha 与 Vue:SyntaxError:无法在模块外使用 import 语句 - Jest or Mocha with Vue: SyntaxError: Cannot use import statement outside a module Mocha + TypeScript:不能在模块外使用导入语句 - Mocha + TypeScript: Cannot use import statement outside a module 开玩笑的单元测试 - SyntaxError:不能在模块外使用 import 语句 - Jest unit test - SyntaxError: Cannot use import statement outside a module 网络驱动程序。 JS。 摩卡。 尝试导入页面对象时 - 错误:无法在模块外使用 import 语句 - Webdriverio. JS. Mocha. When trying to import page objects - error: Cannot use import statement outside a module Javascript - 不能在模块外使用导入语句 - Javascript - Cannot use import statement outside a module firebase中的模块外不能使用import语句 - Cannot use import statement outside the module in firebase 不能在模块外使用 import 语句 - Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module Stackblitz:不能在模块外使用 import 语句 - Stackblitz: Cannot use import statement outside a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM