Ts-Node Cannot use import statement outside a module with Mocha

(hey why is this editor so crappy, why is it all grey text every time I post a topic?  It's been like this for years)

So I'm able to run this just fine from the command-line (from root of the project):

yarn --cwd src/graphql test-graphql-integration

Here is the script: 

"test-graphql-integration": "NODE_ENV=test yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",

compiler.ts is a test helper file which applies ts-node to my test runs:

project: "test/tsconfig.testing.json",

So my folder structure is this

> src
   > graphql
       > test
           > integration
                 // all the .spec.ts files are in here


Ok so the problem I have now is I used to be able to run these tests just fine with the JetBrains test runner.  But I just cannot get past this error when I'm running it from the JetBrains test runner. 

It's weird because why can I run this from the command-line knowing it is hitting the same compiler.ts and tsconfig.testing.json but errors on imports?


"ts-node": {
"files": true,
"transpileOnly": true
"compilerOptions": {
"noEmit": true,
"sourceMap": false,
"module": "esnext",
"moduleResolution": "node",
"target": "es6",
// "lib": ["esnext"],
"inlineSources": false,
"strictNullChecks": false,
"noImplicitAny": false,
"noImplicitThis": false,
"esModuleInterop": true,
"skipLibCheck": true
"include": ["./**/*.spec.ts"],
"exclude": [


"name": "we-do-tdd-graphql",
"engines": {
"node": "14.x",
"npm": "6.x"
"scripts": {
"test-graphql-integration": "NODE_ENV=test yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",
"dependencies": {
"@babel/register": "^7.10.5",
"@babel/runtime": "^7.10.5",
"@types/express": "^4.17.7",
"@types/express-graphql": "^0.9.0",
"express": "^4.17.1",
"express-graphql": "^0.11.0",
"devDependencies": {
"@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/node": "^15.6.1",
"chai": "^4.3.4",
"mocha": "^8.4.0",
"ts-node": "^10.0.0",
"typescript": "^4.2.4"
Comment actions Permalink

Can't make the tests work when using similar setup, even when running your script in terminal. Do you have .babelrc in your project, what does it look like, what folder is it located in? A complete sample project the issue can be repeated with would be helpful

Comment actions Permalink

Yes there's a .babelrc.  Again the tests run fine from the command-line (no idea why you aren't able to), but it's just not working anymore from WebStorm's test runner anymore.  I upgraded latest so I can't tell if it's a WebStorm or some other issue.

I also BTW tried the same test config in IntelliJ, no go, same error.

I can't share the project publicly but I can add you as a collaborator temporarily so that you can run it.  Can you let me know what your github id is so I can add as a collaborator and I'll do that ASAP.  The repo is https://github.com/dschinkel/we-do-tdd so it's private.

Comment actions Permalink

My id is 53188347

Comment actions Permalink

Not sure what you mean, that's not a github id...I can't add you to my repo with 53188347

Comment actions Permalink

It's an ID as reported by https://api.github.com/users/

Do you need a user name and not an ID?

Comment actions Permalink

Yes I need your username to be able to add you as a collaborator to my github repo

Comment actions Permalink

Elena, you're added.  Please check out the build2 branch.  I won't push anything else up there and give you a chance to play with it.  Let me know if you have any questions.

Comment actions Permalink

Thank you!

For me, results are the same in both the IDE and terminal:


Note that I had to change the script to

"test-graphql-integration": "SET NODE_ENV=test && yarn mocha --reporter min test/integration/**/*.spec.ts -r test/compiler.ts -t 6000",

as I'm on Windows... Am I doing anything wrong?


Comment actions Permalink

I figured this out.  I'm going to write a blog post about this. I'll paste it in when I'm done writing it.

Comment actions Permalink

Hi Dave Schinkel

I'm having the same kind of issue, just after updating Webstorm. Did you have any chance to describe your fix in a blog post?


Please sign in to leave a comment.