-
-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ts-node using with mocha and tsconfig.json #521
Comments
Hi @courteous! I've been struggling with a similar issue lately and here's the setup that worked for me: package.json {
"name": "my-project",
"version": "0.1.0",
"main": "/out/index.js",
"scripts": {
"prestart": "./node_modules/typescript/bin/tsc",
"start": "node out/index.js",
"pretest": "./node_modules/.bin/tslint --config tslint.json --project tsconfig.json",
"test": "./node_modules/mocha/bin/mocha --require ts-node/register test/**/*.spec.ts"
},
"devDependencies": {
"@types/chai": "^4.1.1",
"@types/express": "^4.11.0",
"@types/mocha": "^2.2.46",
"@types/node": "^8.5.8",
"chai": "^4.1.2",
"mocha": "^5.0.0",
"ts-node": "^4.1.0",
"tslint": "^5.9.1",
"typescript": "^2.6.2"
}
} tsconfig.json {
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"outDir": "./out",
"strict": true,
"target": "es2016"
},
"include": [
"src/**/*.ts"
]
}
So as you can see the Hope that helps =) |
Hello Denis, many thanks for the reply. I have tired exactly what you described and for that proposed i wanted to consolidate all my *.ts file in one folder which i did just not. i.e. currently all *.ts are in folder called ts/ which is on in on level as the package.json and tsconfig.json. However even when putting the "inlude"statement in the tsconfig as you proposed and execution the npm start as in our cases that did not help. What i did is found a a way to replicate the problem on a very simple project that i uploaded here: https://github.com/courteous/failMinimalProjectUnitTesting the only difference here is that i removed one of your lines i.e. since for the replication tslint.conf is not required. if i rename tsconfig.json to something else then test work, if i rename it back to tsconfig.json then they fail. any help is really appreciated. |
got it changing node-ts does not support es6 yet, described here: changing the module parameters from // "module": "es6", worked for me, although es6 support will be nice since that is where the road is going anyway. |
JSON parsing should be a bit better with #536 so the error will be easier to track down. Your issue was with invalid JSON (trailing comma in the |
I have this issue on v7.0.0 (which was released after @blakeembrey's comment on 19 Feb). Mine is definitely caused by a trailing comma, which is usually fine (npm and typescript aren't complaining). |
Can you print version information and confirm? There’s no code parsing JSON in this library anymore so it shouldn’t be possible. |
@blakeembry I did before I posted: |
D'oh, sorry! I didn't look high enough in the stacktrace: |
I got also the Then my Mocha is started like
If you don't like to have a new dependency for this, you could also do Note: Cross-env is used to have the env setting working on Windows / Linux etc. - tested on Windows. |
@AWolf81 for what it's worth, you can also add ts-node-specific compilerOptions to your root
|
This gist did the job for me: https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d I can't see "node-option" documented anywhere but it actually works and seems prettier than using Mocha section in "mocha": {
"extension": [
"ts"
],
"node-option": [
"experimental-specifier-resolution=node",
"loader=ts-node/esm"
],
"spec": "test/**/*.ts"
} ts-node section in "ts-node": {
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true,
"moduleResolution": "Node"
}
} |
Hello and first thank you for creating this project. I bumped is a problem that seems to be discussed before but it is not clear to me what the solution is:
Here is the problem:
I was following yesterday this unit testing manual that describe one of the ways how to do unit testing with mocha and chai. It is all working as presented there but i found out that if i add my tsconfig.json to the project then the test stop working and fail with the error
It all is working if the file tsconfig.json is not in the main directory
npm run test
if I rename or remove the tsconfig.json file my test a running with mocha ok and if i execute
npm run test
then i get:
and here is how the test script is being executed in package.json:
I have tired adding exclude parameter to my tsconfig.json as mentioned in the issue number 4 here, but that does not seems to fix the problem i.e. here is how my tsconfig.json looks like:
I have tired adding the dist folder to the exclude statements but that only changes the exception thrown i..e
but that does not seems to solve the problem as well.
In issue number 4 it was mentioned that i should use the "--noProject" directive but how to execute that with mocha? i.e what is the command line to test with i.e.
ts-node --no-project node_modules/mocha/bin/mocha -r ts-node/register ./tests/example.ts``
or
ts-node --no-project node_modules/mocha/bin/_mocha -r ts-node/register ./tests/example.ts
or
i do not quite understand to what should i apply that "--no-project" directive.
basically the question is how to use the ts-node correctly with mocha.
and here are some stats
any help is really appreciated.
regards,
The text was updated successfully, but these errors were encountered: