导入config.json文件时Typescript总是错误

fai*_*alk 2 automated-tests typescript e2e-testing testcafe

我正在使用最新版本的Visual Studio Code。VS Code使用Typescript v3.3.3。我已经通过npm在本地(save-dev)和全局安装了以下软件包:

  1. TestCafe v1.1.0
  2. Core-JS v3.0。
  3. TypeScript v3.4.1

我还创建了一个tsconfig.json文件,并添加了属性-“ resolveJsonModule:true”

我已经创建了一个config.json文件,并且在上面添加了resolveJsonModule属性之后,它已在我的.ts文件中正确地被正确拾取-

Import config from './config.json';
Run Code Online (Sandbox Code Playgroud)

但是,每当我运行脚本时,都会出现以下错误:

    Error: TypeScript compilation failed.
    /Users/test/Documents/Dev_Node/TestCafe/UK/homepage-fixture.ts (2, 20): 
    Cannot find module './config.json'. Consider using '--resolveJsonModule' 
    to import module with '.json' extension at Function._reportErrors (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/te st-file/formats/typescript/compiler.js:45:15) at TypeScriptTestFileCompiler._reportErrors [as _precompileCode (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/test-file/formats/typescript/compiler.js:79:40) at TypeScriptTestFileCompiler._precompileCode [as_compileCodeForTestFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/te 
st-file/api-based.js:110:29) at TypeScriptTestFileCompiler._compileCodeForTestFiles [as precompile (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/test-file/api-based.js:169:21) at precompile (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:70:48) at Compiler._precompileFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:66:54) at _precompileFiles (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/index.js:105:91) at map (/Users/test/Documents/Dev_Node/TestCafe/node_modules/testcafe/src/compiler/i ndex.js:105:41)
Run Code Online (Sandbox Code Playgroud)

我已经尝试过此处提供的解决方案:导入json文件时出现Typescript编译器错误

但是他们都不适合我。

Tyl*_*rch 6

According to this longstanding issue: https://github.com/DevExpress/testcafe/issues/1845 TestCafe uses its own TypeScript configuration, and doesn't honor any tsconfig.json that you use.

So it seems to me that your options are:

  • Compile the TypeScript code yourself with your own tsconfig.json that includes the resolveJsonModule flag, and only let TestCafe see the resulting JavaScript code. TypeScript's outDir flag may be helpful here for copying the files to the right place.
  • 创建一个MyConfig描述您的配置内容的接口(也许名为),并使用“ let json: MyConfig = require('./config.json');不如让TypeScript自行解决问题” 导入JSON文件,但它应该可以解决TestCafe的局限性。

  • 尝试删除您的“ package-lock.json”并运行“ npm i -D testcafe打字稿”。 (4认同)