0
votes

I am developing an Angular service which uses WebSocket and I would like to launch some tests on this service directly from VSCode Debug window, as I do regularly when I develop any node app. The service is very simple, does not rely on DI or other Angular facilities, so I would expect it possible to test it directly from withing VSCode, as it seems suggested by the documentation here.

The code of the test is this

import { MyService } from './my-service.service';
import { environment } from '../../environments/environment';

describe('MyService', () => {
  it('do something with a WebSocket and test the result', (done) => {
    const myService = new MyService();
    const conn = new WebSocket(environment.serverAddress);
    // do some more stuff and test the results
  });
});

Everything works fine if I run this test with the ng test Angular CLI command.

The problems start when I try to run that single test directly from VSCode using the interactive debugger. To be able to launch the test from the VSCode debug window, I have done 2 things.

I have added a tsconfig.test.json file like this

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "sourceMap": true,
    "declaration": true,
    "experimentalDecorators": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"],
  "typeRoots": ["node_modules/@types"],
  "lib": ["es2018", "dom"]
}

and a configuration in the launch.json, where I reference the tsconfig.test.json file, like this

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Current TS Tests File",
      "type": "node",
      "request": "launch",
      "env": { "TS_NODE_PROJECT": "tsconfig.test.json" },
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "args": ["-r", "ts-node/register", "${relativeFile}"],
      "cwd": "${workspaceRoot}",
      "protocol": "inspector"
    }
  ]
}

After having done this, if I launch the test from VSCode debug window, I get an error which says ReferenceError: expect is not defined.

I can overcome this error importing expect from chai, but then I get a second error saying ReferenceError: WebSocket is not defined.

It is quite clear that I am missing something, but I am not able to see what.

are your test files in src? directory? - bryan60
@bryan60 it is in the same folder as the service code, i.e. in `src/app/my-service/ - Picci