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:

require("ts-node").register({
project: "test/tsconfig.testing.json",
});

So my folder structure is this

> src
   > graphql
       > test
           > integration
                 // all the .spec.ts files are in here
              compiler.ts
              tsconfig.testing.json
    package.json

so:
src/graphql/test/compiler.ts
src/graphql/test/tsconfig.testing.json
src/graphql/package.json

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?

src/graphql/test/tsconfig.testing.json

{
"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": [
"../../dist",
"/node_modules",
"../node_modules"
]
}


src/graphql/package.json

{
"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"
}
}
0
12 comments

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

0

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.

0

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

0

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

Do you need a user name and not an ID?

0

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

0

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.

0

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?

 

0

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.

1

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?

0

we are having the same issue. Here is the users.test.ts , we can run from command line but not from webstorm 2021.3.1 

// import assert from 'assert';
import app from '../../src/app';
import * as assert from 'assert';

describe('\'users\' service', () => {
it('registered the service', () => {
const service = app.service('users');

assert.ok(service, 'Registered the service');
});
});

//package.json (command line works)

"scripts": {
"test": "npm run lint && npm run compile && npm run mocha",
"lint": "eslint src/. test/. --config .eslintrc.json --ext .ts --fix",
"dev": "ts-node-dev --no-notify src/",
"start": "npm run compile && node lib/",
"mocha": "mocha --require ts-node/register --require source-map-support/register \"test/**/*.ts\" --recursive --exit",
"compile": "shx rm -rf lib/ && tsc"
}

The error is:


usr/bin/node /home/reda/dev/js/featherjs/feathers-cli-211224/node_modules/mocha/bin/mocha --ui bdd --reporter /home/reda/.local/share/JetBrains/Toolbox/apps/WebStorm/ch-0/213.6461.19/plugins/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js /home/reda/dev/js/featherjs/feathers-cli-211224/test/services/users.test.ts --grep ^'users' service

/home/reda/dev/js/featherjs/feathers-cli-211224/test/services/users.test.ts:2


import app from '../../src/app';
^^^^^^

SyntaxError: Cannot use import statement outside a module

----

to make it work in Webstorm, I have to edit the configuration and pass the following arguments to mocha.

--require ts-node/register --require source-map-support/register
0

Please sign in to leave a comment.