简体   繁体   中英

How do I correctly configure mocha tests with ts_transformer_keys?

I can't seem to set the custom transformer for ts-transform-keys with my mocha tests.

I'm using mocha 6.1.4

ts-node 8.3.0 https://www.npmjs.com/package/ts-node

ts-trasnformer-keys 0.3.5 https://github.com/kimamula/ts-transformer-keys

ttypescript 1.5.7 https://github.com/cevek/ttypescript

The ts-node documentation says that you cannot set a custom transformer on the CLI, only programatically. So I'm trying to use ttypescript to get around that restriction.

I've tried the following...

Note: test.ts contains the following

import { keys } from 'ts-transformer-keys';

describe("xyz"), () => {
  it("123", (done) => {
     keys<CustomInterface>();
  });
});

Attempt 1) - Set the ts-node with an environment variable

TS_NODE_COMPILER="ttypescript" mocha test/test.ts --require ts-node/register

Then I have the following in test/tsconfig.json

{
  "compilerOptions": {
    "plugins": [
      { "transform": "ts-transformer-keys/transformer" }
    ]
  }
}

This results in Uncaught TypeError: ts_transformer_keys_1.keys is not a function which indicates that the custom transformer wasn't used at compile time.

Attempt 2) Following the typescript API example from ts-transformer-keys

I added a mocha.opts file with the following

--file test/transformer-config.js

and a transformer-config.js file with the following

const ts = require('typescript');
const keysTransformer = require('ts-transformer-keys/transformer').default;

const program = ts.createProgram(['test/test.ts'], {
  strict: true,
  noEmitOnError: true,
  target: ts.ScriptTarget.ES5
});

const transformers = {
  before: [keysTransformer(program)],
  after: []
};
const { emitSkipped, diagnostics } = program.emit(undefined, undefined, undefined, false, transformers);

if (emitSkipped) {
  throw new Error(diagnostics.map(diagnostic => diagnostic.messageText).join('\n'));
}

Then I invoke it like this mocha test/test.ts --require ts-node/register

This results in the following error


/Users/jjohnson/Documents/OCS/hmp/git/hmp-server/server/test/ttypescript-register.js:17
  throw new Error(diagnostics.map(diagnostic => diagnostic.messageText).join('\n'));
        ^
Error: [object Object]
[object Object]
[object Object]
    at Object.<anonymous> (/Users/jjohnson/Documents/OCS/hmp/git/hmp-server/server/test/ttypescript-register.js:17:9)
    at Module._compile (internal/modules/cjs/loader.js:777:30)
...

It feels like in Attempt 1 it wasn't ever calling the code that sets the custom transformer in tsconfig.json or if it was getting called the code was failing silently.

It feels like in Attempt 2 I'm creating a new instance of the typescript program and then that fails for some reason. And even if it succeeded I'm not sure that this is the right way to go about configuring things since the ts.createProgram wants a list of RootNames for the files it will transpile.

Maybe my entire approach is wrong.

All I really want is a way that in my mocha tests I can verify that the expected result type is what the method returned. And I'd like to be able to do this w/out touching too much of the source code.

you should be able to define your required module (see below) and run ts-node programmatically. In this way, you can safely use any customer transformer.

// tsnode.js

const transformer = require('ts-transformer-keys/transformer').default;
require("ts-node").register({
  transformers: program => ({
    before: [
      transformer(program)
    ]
  })
});

then you can run mocha with require

mocha --require './tsnode.js' --watch-extensions ts,tsx "test/**/*.{ts,tsx}

You can tell ts-node which compiler to use in tsconfig.json . This is covered in the ts-node docs . If your using transformers presumably your also using ttypescript compiler. You just need to add this:

  "ts-node": {
    "compiler": "ttypescript"
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM