Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jest测试因意外标记而失败,应为“1”&引用;_Javascript_Node.js_Typescript_Jestjs - Fatal编程技术网

Javascript Jest测试因意外标记而失败,应为“1”&引用;

Javascript Jest测试因意外标记而失败,应为“1”&引用;,javascript,node.js,typescript,jestjs,Javascript,Node.js,Typescript,Jestjs,我有一个使用Typescript和Jest的节点项目。目前我有这个项目结构 使用此tsconfig.json文件 "compilerOptions": { "target": "ES2017", "module": "commonjs", "allowJs": true, "outDir": "dist", "rootDir": "src", "strict": true, "moduleResolution": "node",

我有一个使用Typescript和Jest的节点项目。目前我有这个项目结构

使用此
tsconfig.json
文件

  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "allowJs": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
module.exports = {
  clearMocks: true,
  coverageDirectory: "coverage",
  testEnvironment: "node",
};
{
  "scripts": {
    "start": "node dist/App.js",
    "dev": "nodemon src/App.ts",
    "build": "tsc -p .",
    "test": "jest"
  },
  "dependencies": {
    "commander": "^3.0.1"
  },
  "devDependencies": {
    "@types/jest": "^24.0.18",
    "@types/node": "^12.7.4",
    "jest": "^24.9.0",
    "nodemon": "^1.19.2",
    "ts-jest": "^24.0.2",
    "ts-node": "^8.3.0",
    "typescript": "^3.6.2"
  }
}
这个
jest.config.js
文件

  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "allowJs": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
module.exports = {
  clearMocks: true,
  coverageDirectory: "coverage",
  testEnvironment: "node",
};
{
  "scripts": {
    "start": "node dist/App.js",
    "dev": "nodemon src/App.ts",
    "build": "tsc -p .",
    "test": "jest"
  },
  "dependencies": {
    "commander": "^3.0.1"
  },
  "devDependencies": {
    "@types/jest": "^24.0.18",
    "@types/node": "^12.7.4",
    "jest": "^24.9.0",
    "nodemon": "^1.19.2",
    "ts-jest": "^24.0.2",
    "ts-node": "^8.3.0",
    "typescript": "^3.6.2"
  }
}
这个
package.json
文件

  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "allowJs": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true
  }
module.exports = {
  clearMocks: true,
  coverageDirectory: "coverage",
  testEnvironment: "node",
};
{
  "scripts": {
    "start": "node dist/App.js",
    "dev": "nodemon src/App.ts",
    "build": "tsc -p .",
    "test": "jest"
  },
  "dependencies": {
    "commander": "^3.0.1"
  },
  "devDependencies": {
    "@types/jest": "^24.0.18",
    "@types/node": "^12.7.4",
    "jest": "^24.9.0",
    "nodemon": "^1.19.2",
    "ts-jest": "^24.0.2",
    "ts-node": "^8.3.0",
    "typescript": "^3.6.2"
  }
}
我在测试目录中创建了一个测试文件

import { App } from '../src/App';

describe('Generating App', () => {
  let app: App;

  test('It runs a test', () => {
    expect(true).toBe(true);
  });
});
但不幸的是,我得到了一个语法错误

SyntaxError:C:…\tests\App.test.ts:意外标记,应为“ (5:9)


在我的
应用程序中
变量。似乎测试运行人员无法理解Typescript代码。如何修复配置以支持Jest测试文件中的Typescript?

尝试在Jest配置中添加Typescript扩展名:

module.exports = {
  roots: ['<rootDir>'],
  transform: {
    '^.+\\.ts?$': 'ts-jest'
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$',
  moduleFileExtensions: ['ts', 'js', 'json', 'node'],
  collectCoverage: true,
  clearMocks: true,
  coverageDirectory: "coverage",
};

谢谢你的帮助。当我运行
npm run test
I时,模式返回0个与
run with'--passWithNoTests'匹配的项,以退出C:\。。。检查了6个文件
,似乎无法使用
测试
目录..=?我认为这是因为您的测试在src文件夹之外,所以我更新了jest配置,添加了
根:['''],
而不是
根:['/src'],
现在应该可以工作了:)您的测试位于
测试
文件夹中,但是在
jest.config.js中,您已经指定了文件夹
\uu tests
。指定了不同的路径,但没有与之匹配的路径。在我的情况下,它不会将ts文件传输到js中。当我添加answer的transform属性时,它开始传输并传递给另一个问题,该问题由tsConfig属性解决。谢谢