ts-nameof icon indicating copy to clipboard operation
ts-nameof copied to clipboard

Add mocha instructions

Open dsherret opened this issue 6 years ago • 3 comments

As title.

dsherret avatar Jun 22 '19 00:06 dsherret

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 avatar Oct 08 '19 21:10 bzhu94

@bzhu94 in your case:

  1. Install ts-nameof, @types/ts-nameof, and ttypescript as dev dependencies.
  2. Set the TS_NODE_COMPILER environment variable to ttypescript when 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.

  1. 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" }] 
}

dsherret avatar Oct 14 '19 02:10 dsherret

Also, sorry this is so difficult to setup. Please go upvote this issue: https://github.com/Microsoft/TypeScript/issues/14419

dsherret avatar Oct 14 '19 02:10 dsherret