ts-nameof
ts-nameof copied to clipboard
Add mocha instructions
As title.
Can I please get a quick summary on how this can be done? Even when I use the triple slash directives /// <reference path="./node_modules/ts-nameof/ts-nameof.d.ts" /> or /// <reference types="./node_modules/ts-nameof" /> in my mocha tests, running the tests gives the following exception: ReferenceError: nameof is not defined.
The command I'm using is to run the tests is mocha -r ts-node/register Tests/**/*.tsx Tests/**/*.ts and my tsconfig.json is
{
"compileOnSave": true,
"compilerOptions": {
"target": "es6",
"jsx": "react",
"noImplicitAny": false,
"removeComments": false,
"declaration": false,
"noEmitOnError": true,
"sourceMap": false,
"moduleResolution": "node"
},
"include": [ "./**/*.ts", "./**/*.tsx"],
"exclude": [ "./node_modules" ]
}
@bzhu94 in your case:
- Install
ts-nameof,@types/ts-nameof, andttypescriptas dev dependencies. - Set the
TS_NODE_COMPILERenvironment variable tottypescriptwhen running your script.
For example, I think you could do that with the cross-env package by doing:
cross-env TS_NODE_COMPILER="ttypescript" mocha -r ts-node/register Tests/**/*.tsx Tests/**/*.ts
(save cross-env as a dev dependency from npm)
You should be able to remove all the type reference directives (/// <reference />) if you install the @types/ts-nameof package. Generally that package or the type reference directive is only needed to get type checking to work and it doesn't have any affect on runtime.
- Update your tsconfig.json to have a "plugins" field. ttypescript will use this to inject ts-nameof as a transformer for the compiler to use.
{
"compileOnSave": true,
"compilerOptions": {
"target": "es6",
"jsx": "react",
"noImplicitAny": false,
"removeComments": false,
"declaration": false,
"noEmitOnError": true,
"sourceMap": false,
"moduleResolution": "node"
},
"include": [ "./**/*.ts", "./**/*.tsx"],
"exclude": [ "./node_modules" ],
"plugins": [{ "transform": "ts-nameof", "type": "raw" }]
}
Also, sorry this is so difficult to setup. Please go upvote this issue: https://github.com/Microsoft/TypeScript/issues/14419