Skip to content
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

Compilation error with mocha and TypeScript 2.0 on Windows 10 #168

Closed
ospatil opened this issue Aug 7, 2016 · 13 comments
Closed

Compilation error with mocha and TypeScript 2.0 on Windows 10 #168

ospatil opened this issue Aug 7, 2016 · 13 comments

Comments

@ospatil
Copy link

ospatil commented Aug 7, 2016

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 test

I 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.

@andregs
Copy link

andregs commented Sep 21, 2016

Same issue here, and the triple slash workaround also worked for me.

@ecozoic
Copy link

ecozoic commented Oct 13, 2016

Same issue here (although it's occurring w/ @types/protractor and not mocha), on Windows with ts-node@1.4.3. The triple-slash fix worked for me.

@erik-sn
Copy link

erik-sn commented Oct 18, 2016

Same issue on Ubuntu using @types - triple slash also fixed it for me.

@budiadiono
Copy link

Giving "types": [ "node", "mocha", "chai" ] under compilerOptions on tsconfig.json solve the issue.

@jamelt
Copy link

jamelt commented Oct 28, 2016

@budiadiono Can you elaborate? I added that to my tsconfig.json to no avail.

{
  "compilerOptions": {
    "types": [ "node", "mocha", "chai", "bunyan", "lodash"],
    "typeRoots": ["typings"]
  }
}

I've also tried specifying the typeRoots in compilerOptions as 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
Copy link

@jamelt, maybe you forgot to install @types/[module] for requested types? Thus you have to do "include": [ "**/*.ts" ] that makes typescript look for the scripts in entire location.

@mortterna
Copy link

mortterna commented 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.ts compiles just fine on windows without tripleslash annotations.

forman added a commit to CCI-Tools/cate-desktop that referenced this issue Nov 23, 2016
@hans-permana
Copy link

Giving "types": [ "node", "mocha", "chai" ] under compilerOptions on tsconfig.json solve the issue.

this works for me.

@jjcamp
Copy link

jjcamp commented 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
Copy link
Member

blakeembrey commented 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.json or <reference types>. Do not use an import as it will emit a runtime require statement, which is why you can't use it for global declarations.

niik added a commit to desktop/desktop that referenced this issue Dec 8, 2016
Without this we're getting an error while using ts-node on Windows.

See
TypeStrong/ts-node#168
TypeStrong/atom-typescript#1055

Note that this works around the issue but as far as I understand
things it shouldn't be necessary.
shiftkey added a commit to desktop/dugite that referenced this issue Dec 19, 2016
chengsieuly added a commit to chengsieuly/devdecks that referenced this issue Jan 20, 2017
Settings for types could be set in tsconfig.json under
"types": [...]. However, this method only works for Mac.
On Windows 10, pathing is done differently so it cannot locate
the type definitions. We must specify absolute paths to these types
to make it compatible with Windows 10.

TypeStrong/ts-node#168
@wrumsby
Copy link

wrumsby commented Mar 9, 2017

I was running into a very similar problem, ensuring typeRoots was defined and correct in my tsconfig.json solved the issue for me, e.g.

yarn add -D @types/mocha
{
   "types": [
     "mocha"
  ],
  "typeRoots": [
    "./node_modules/@types"
  ]
}

@forman
Copy link

forman commented Mar 13, 2017

@wrumsby, thanks, that worked for me as well!

@bertolo1988
Copy link

bertolo1988 commented Jul 6, 2017

The only thing that worked for me in Ubuntun 17.04 was:

npm install --save-dev @types/mocha

and adding this to tsconfig.json

{
   "types": [
     "mocha"
  ],
  "typeRoots": [
    "./node_modules/@types"
  ]
}

thanks @wrumsby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests