0

so the thing is. I am building an Angular Test Explorer. I am able to see all the tests and run them all together by using the karma module like this:

  public async runWithModule(): Promise<void> {
    return new Promise<void>(resolve => {
      karma.runner.run({ port: 9876 }, (exitCode: number) => {
        global.console.log("karma run done with ", exitCode);
        resolve();
      });
    });
  }

I am also able to run specific set of tests creating a shell and passing --grep

const command = `karma run -- --grep="${tests}"`;
const exec = require("child_process").exec;
exec(command, {
  cwd: this.angularProjectRootPath + "/node_modules/karma/bin/",
});

unfortunately the method for running a set of tests works different depending on the OS as the shell its different. This is giving me some problems.

I was wondering if anybody cant point me out how is that angular cli is doing karma run and specifying a set of tests when you do a regular ng test.

I asked in the karma repository and support without any answer so that's why I am asking here, I also tried finding that part of the code in the repository of the angular devkit. I have found where they do the karma.server but could not find the part I need.

4

1 回答 1

0

解决方案是向浏览器发出http请求路径/run。这将触发业力运行,您还可以使用 --grep= 指定一组测试,就像在命令行上所做的那样

public async runTests(tests: any): Promise<void> {
  const karmaRunParameters = this.createKarmaRunConfiguration(tests);

  await this.runWithConfig(karmaRunParameters.config);
}

private createKarmaRunConfiguration(tests: any) {
  // if testName is undefined, reset jasmine.getEnv().specFilter function
  // otherwise, last specified specFilter will be used

  if (tests[0] === "root" || tests[0] === undefined) {
    tests = "";
  }
  const serverPort = 9876;
  const urlRoot = "/run";
  const config = {
    port: serverPort,
    refresh: true,
    urlRoot,
    hostname: "localhost",
    clientArgs: [] as string[],
  };
  config.clientArgs = [`--grep=${tests}`];
  return { config, tests };
}

private runWithConfig(config: any): Promise<void> {
  return new Promise<void>(resolve => {
    const options = {
      hostname: config.hostname,
      path: config.urlRoot,
      port: config.port,
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
    };

    const http = require("http");

    const request = http.request(options);

    request.on("error", (e: any) => {
      if (e.code === "ECONNREFUSED") {
        global.console.error("There is no server listening on port %d", options.port);
      }
    });

    request.end(JSON.stringify({
      args: config.clientArgs,
      removedFiles: config.removedFiles,
      changedFiles: config.changedFiles,
      addedFiles: config.addedFiles,
      refresh: config.refresh,
    }));

    request.on("close",() =>{ resolve(); });
  });
}

测试正在正确运行 测试资源管理器使用指定的方法运行 Angular/Karma 测试

于 2019-03-13T09:05:44.697 回答