Repository navigation
Closed
Description
I'm trying to run mocha tests using --compilers ts:ts-node/register with TypeScript 2.0 but get the following error only on Windows (10) while it works flawlessly on a Mac -
C:\temp\nodets\node_modules\ts-node\src\index.ts:259
throw new TSError(diagnosticList)
^
TSError: ⨯ Unable to compile TypeScript
test\greeter-spec.ts (6,1): Cannot find name 'describe'. (2304)
test\greeter-spec.ts (7,3): Cannot find name 'it'. (2304)
at getOutput (C:\temp\nodets\node_modules\ts-node\src\index.ts:259:17)
at C:\temp\nodets\node_modules\ts-node\src\index.ts:268:16
at Object.compile (C:\temp\nodets\node_modules\ts-node\src\index.ts:404:17)
at loader (C:\temp\nodets\node_modules\ts-node\src\index.ts:290:33)
at Object.require.extensions.(anonymous function) [as .ts] (C:\temp\nodets\node_modules\ts-node\src\index.ts:307:14)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at C:\temp\nodets\node_modules\mocha\lib\mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (C:\temp\nodets\node_modules\mocha\lib\mocha.js:217:14)
at Mocha.run (C:\temp\nodets\node_modules\mocha\lib\mocha.js:485:10)
at Object.<anonymous> (C:\temp\nodets\node_modules\mocha\bin\_mocha:405:18)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (node.js:348:7)
at startup (node.js:140:9)
at node.js:463:3
npm ERR! Test failed. See above for more details.You can replicate the issue using the following steps -
git clone https://github.com/ospatil/tsnode-issue.git
cd tsnode-issue
npm install
npm testI managed to get it working on Windows by adding a triple slash reference to mocha type definition /// <reference path="../node_modules/@types/mocha/index.d.ts" /> to test/greeter-spec.ts but it seems redundant since it works flawlessly without it on Mac.
Reacted by Nebez Briefkani, Stephen Cleary, André Gomes, John Mitchell, Jamel Toms, Norman Fomferra, BrunoLM, Arya Sabeti, Walter Rumsby, Tam Jordan and 4 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
andregs commentedon Sep 21, 2016
Same issue here, and the triple slash workaround also worked for me.
ecozoic commentedon Oct 13, 2016
Same issue here (although it's occurring w/ @types/protractor and not mocha), on Windows with [email protected]. The triple-slash fix worked for me.
erik-sn commentedon Oct 18, 2016
Same issue on Ubuntu using @types - triple slash also fixed it for me.
budiadiono commentedon Oct 25, 2016
Giving
"types": [ "node", "mocha", "chai" ]undercompilerOptionsontsconfig.jsonsolve the issue.jamelt commentedon Oct 28, 2016
@budiadiono Can you elaborate? I added that to my
tsconfig.jsonto no avail.{ "compilerOptions": { "types": [ "node", "mocha", "chai", "bunyan", "lodash"], "typeRoots": ["typings"] } }I've also tried specifying the
typeRootsincompilerOptionsas well. This does not work either. I'm not entirely sure why.Also is there anyway this can be addressed without having to maintain a separate list of typings? This is a rather broken approach, in my opinion.I was able to resolve this issue by adding this block to my
tsconfig.json.{ "include": [ "**/*.ts" ], }budiadiono commentedon Oct 29, 2016
@jamelt, maybe you forgot to install
@types/[module]for requested types? Thus you have to do"include": [ "**/*.ts" ]that makestypescriptlook for the scripts in entire location.mortterna commentedon Nov 5, 2016
I had similar issue of ts-node on windows not finding protractor types under node_modules/@types without explicit
"include": [ "**/*.ts" ]configuration while in osx everything seemed to work without it.edit: Actually include was not the key since I forgot explicit
/// <reference path="../node_modules/@types/protractor/index.d.ts" />in place which is enough to fix the problem by itself. And for clarification/// <reference types="protractor" />is not enough to make it work. Neither of the tripleslash annotations are needed in osx since types are already mentioned in typescript configuration's "types" array.edi2: And for the reference
node node_modules\typescript\lib\tsc.js --types node,protractor scripts\e2e.tscompiles just fine on windows without tripleslash annotations.fix from TypeStrong/ts-node#168
hans-permana commentedon Nov 23, 2016
this works for me.
explicitly includes node, mocha, and chai because of windows path iss…
jjcamp commentedon Dec 6, 2016
import "mocha";works for me.This doesn't work so well for node library types though (would have to add each individually), and adding node to types in compilerOptions causes conflicts in ts-node if another module also uses node typings.
/// <reference>seems to be the best option in that case, but this seems to be indicative of a larger problem which only happens in ts-node and not the typescript compiler.blakeembrey commentedon Dec 7, 2016
Closing in favour of #216. It looks like all the issues stem from Windows and I'd guess it's a path issue with TypeScript (they do some odd escaping and expect inputs escaped in certain ways - possibly it's a related issue to this). If someone finds the cause, feel free to submit a PR.
If you're working around it, use
tsconfig.jsonor<reference types>. Do not use animportas it will emit a runtime require statement, which is why you can't use it for global declarations.Explicitly specify paths to type roots
workaround known issue with mocha being unable to resolve type
fix: Issue with ts-node pathing on Windows 10
wrumsby commentedon Mar 9, 2017
I was running into a very similar problem, ensuring
typeRootswas defined and correct in mytsconfig.jsonsolved the issue for me, e.g.{ "types": [ "mocha" ], "typeRoots": [ "./node_modules/@types" ] }update tsconfig.json so that unit tests can be run individually. ref: T…
forman commentedon Mar 13, 2017
@wrumsby, thanks, that worked for me as well!
bertolo1988 commentedon Jul 6, 2017
The only thing that worked for me in Ubuntun 17.04 was:
npm install --save-dev @types/mochaand adding this to tsconfig.json
thanks @wrumsby