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

Add mocha instructions #57

Open
dsherret opened this issue Jun 22, 2019 · 3 comments
Open

Add mocha instructions #57

dsherret opened this issue Jun 22, 2019 · 3 comments

Comments

@dsherret
Copy link
Owner

As title.

@bzhu94
Copy link

bzhu94 commented Oct 8, 2019

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

@dsherret
Copy link
Owner Author

dsherret commented Oct 14, 2019

@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
Copy link
Owner Author

Also, sorry this is so difficult to setup. Please go upvote this issue: microsoft/TypeScript#14419

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

No branches or pull requests

2 participants