0

I'm trying to import from a Typescript file into globalSetup.ts, to be run before some tests. Here is an MWE:

globalSetup.ts: following https://stackoverflow.com/a/65708470/394957

require('ts-node/register');                                                                         
                                                                                                     
require('ts-node').register({                                                                        
  transpileOnly: true,                                                                               
});                                                                                                  
                                                                                                     
//require('tsconfig-paths').register();                                                              
import { thing  } from "../../error.ts";                                                             
console.log(thing);           

and error.ts:

                                                                                                     
require('ts-node').register({                                                                        
  transpileOnly: true,                                                                               
});                                                                                                  
require('tsconfig-paths').register();                                                                
                                                                                                     
const thing: int = 1; 

returns

                                                                                                    
> test                                                                                               
> dotenv -e .env.test jest -i                                                                        
                                                                                                     
TypeError: Cannot assign to read only property 'message' of object 'SyntaxError: $PATH/error.ts: Missing initializer in const declaration. (8:11)
                                                                                                     
  6 | require('tsconfig-paths').register();                                                          
  7 |                                                                                                
> 8 | const thing: int = 1;                                                                          
    |            ^                                                                                   
  9 |'                                                                                               
    at runGlobalHook ($PATH/node_modules/@jest/core/build/runGlobalHook.js:130:25)                   
    at async runJest ($PATH/node_modules/@jest/core/build/runJest.js:370:5)                          
    at async _run10000 ($PATH/node_modules/@jest/core/build/cli/index.js:386:7)                      
    at async runCLI ($PATH/node_modules/@jest/core/build/cli/index.js:237:3)                         
    at async Object.run ($PATH/node_modules/jest-cli/build/cli/index.js:155:37)

(I have replaced the current app path with $PATH for privacy.)

How can I import and run a Typescript file in globalSetup.js?

Mark C.
  • 1,773
  • 2
  • 16
  • 26
  • I don't think you're supposed to call `require('ts-node/register')` (which is the same as `require('ts-node').register`) more than once. Would try to remove all but one at the top of `globalSetup.ts` – zapl Sep 27 '22 at 14:49
  • Thanks! I removed the first line and left `require('ts-node').register({transpileOnly: true,});`. No change, alas – Mark C. Sep 27 '22 at 14:53

0 Answers0