Is there a way for test typescript frameworks to be allowed only in test files in VS Code?

This question applies to each of the following source file organization strategies:

Tests completely separate

/src
/tests

Function tests

/src/feature/
/src/feature/__tests__

File tests

/src/feature/foo.ts
/src/feature/foo.test.ts

The installation @mocha/typesmakes these test-only declarations available as valid identifiers throughout the code base. Simply update tsconfig.json and specify "types": []it to exclude it, but at the moment when you manually link to it in only one file, whether through import 'mocha'or /// <reference types="mocha" />, it infects the entire code base again.

, , ?

, VS . , tsconfig , , gulp - , VS Code, squigglies "" . unit test , .

+6
1

... , . , :

mocha.ts, (, ) - , :

declare var global: any;

export const describe = global.describe as (msg: string, cb: () => void) => void;
export const it = global.it as (msg: string, cb: () => void) => void;

describe it ./mocha . .

enter image description here

redefine ; c & p , , . , .

- , , "", , describe : require('mocha').describe... ( mocha cli) . . http://mochajs.org/#require ... , .

mocha.ts :

declare var global: any;

export const describe = global.describe as IContextDefinition;
export const it = global.it as ITestDefinition;

// following is from DefinitelyTyped

interface IContextDefinition {
  (description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
  only(description: string, callback: (this: ISuiteCallbackContext) => void): ISuite;
  skip(description: string, callback: (this: ISuiteCallbackContext) => void): void;
  timeout(ms: number): void;
}

interface ISuiteCallbackContext {
  timeout(ms: number): this;
  retries(n: number): this;
  slow(ms: number): this;
}

interface ISuite {
  parent: ISuite;
  title: string;

  fullTitle(): string;
}

interface ITestDefinition {
  (expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): ITest;
  only(expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): ITest;
  skip(expectation: string, callback?: (this: ITestCallbackContext, done: MochaDone) => any): void;
  timeout(ms: number): void;
  state: "failed" | "passed";
}
interface ITestCallbackContext {
  skip(): this;
  timeout(ms: number): this;
  retries(n: number): this;
  slow(ms: number): this;
  [index: string]: any;
}

interface MochaDone {
  (error?: any): any;
}

interface ITest extends IRunnable {
  parent: ISuite;
  pending: boolean;
  state: 'failed' | 'passed' | undefined;

  fullTitle(): string;
}

interface IRunnable {
  title: string;
  fn: Function;
  async: boolean;
  sync: boolean;
  timedOut: boolean;
  timeout(n: number): this;
}
+1

Source: https://habr.com/ru/post/1017222/


All Articles