Search code examples
typescripttestingautomated-testse2e-testingtestcafe

Typescript always errors when importing a config.json file


I am using the latest version of Visual Studio Code. VS Code is using Typescript v3.3.3. I have installed the following packages via npm both locally (save-dev) and globally:

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

I have also created a tsconfig.json file and added the property - "resolveJsonModule: true"

I have created a config.json file and it is being picked up correctly in my .ts file correctly after adding the resolveJsonModule property above -

Import config from './config.json';

However whenever I run my script I get the following error:

    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)

I've already tried the solutions provided here: Typescript compiler error when importing json file

But none of them work for me.


Solution

  • 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.
    • Create an interface (perhaps named MyConfig) describing the contents of your configuration, and import the JSON file with let json: MyConfig = require('./config.json'); Not as good as letting TypeScript figure things out itself, but it should work around TestCafe's limitations.