My command line is:
cross-env TS_NODE_PROJECT=test/tsconfig.json mocha --compilers ts:ts-node/register,tsx:ts-node/register
I need to run test on .tsx file, in with I import from .ts file (function that I test). In my .ts file I import some .d.ts file, but in .tsx I also need to import some other .d.ts file (to describe some test-specific packages).
As I understand there a some conflict with order of my compilers for mocha.
When ts before tsx:
TypeError: Unable to require `.d.ts` file.
for my .d.ts from .tsx
When tsx before ts:
TSError: ⨯ Unable to compile TypeScript
with “Cannot find namespace” from .d.ts in .ts file.
I tried to import using /// <reference, import, files in tsconfig.json without any success.
What's the code you have emitting this? As it says, you shouldn't be requiring a .d.ts file - it seems you're using TypeScript incorrectly here. For instance, it's impossible for ts-node to resolve the .d.ts file unless you've gone and written something like import x from './foo.d' which is incorrect - it should be ./foo as the path (without the .d). You'll need to share more code for help.
You can find a whole code here: https://github.com/m18ru/jsx2posthtml/blob/master/test/index.tsx
Currently I solve this problem by using untyped require function instead of import statement.
In comments, marked with TODO: you can find that I tried to use <reference /> (not import) to use a base types for untyped packages posthtml-parse and posthtml-render. It’s correct TypeScript code and there is no other way to import local types (not from node_modules).
I don't understand how that could have solved it. If you're saying that you required a .d.ts file somehow, that file would never have had a runtime (that's in the .js file) so it's invalid. It seems like the issue you had is because you don't have any types for the libraries?
No, problem with types (currently — implicit any for this modules) is another error and I catch this error directly in vscode with typescript intellicence. My errors, as I wrote in issue — that it can’t require .d.ts file from test/index.tsx in first configuration, and that it can’t find namespace JSX in src/index.ts in second configuration (when I write --compilers tsx:ts-node/register,ts:ts-node/register).
A .d.ts file used only as information during the compilation and have no output, yes. If I use it in import statement it can be a problem (but it should not — normally, TS remove such imports from resulting .js and just did not produce any output file for them), but there I use it in <reference /> — the TS-specific thing, and it should be used just as extra information for compiler. And it works in my file https://github.com/m18ru/jsx2posthtml/blob/master/src/index.ts — look at the top of file:
/// <reference path="./JSX.d.ts" />
So, why it did not work in .tsx?
It looks like it’s somehow depends on order of ts and tsx in --compiler option. It can handle .d.ts in .ts files, but not .tsx, if ts loaded first, or maybe not and it’s just my fantasy, but why first .d.ts reference works with first version of compilers order?
If you can share something reproducible, I will take a look. As it is, I don't see any way changing the order would make a difference but welcome to see it happen so I can look at it. I'd bet there's something else going on since all that is doing is a require to ts-node and the setup ends up identical in either case.
Seems like I can reproduce this at https://github.com/types/npm-no-op
In this case, the target is index.d.ts while the test file is spec/test.ts.
@unional What's the error you're getting? I believe in your case it's a valid error - you have a glob matching .d.ts files which blue-tape would be trying to execute (and running a .d.ts file is invalid, there's no runtime to it).
You are right. Changing blue-tape **/*.ts to blue-tape spec/**/*.ts fix that issue.
Most helpful comment
You are right. Changing
blue-tape **/*.tstoblue-tape spec/**/*.tsfix that issue.