Skip to content

test(vscode): Vscode‐extension test 실행

yu iseo edited this page Aug 21, 2024 · 3 revisions

vscode 테스트 실행 방법

관련 설정 및 테스트 코드를 작성후 다음 명령어를 입력.

npm test

관련 설정

package.json

  "scripts": {
    "test": "node ./out/test/runTest.js"
  },

test 실행을 node로 실행하기 때문에 commonJS 문법을 사용하여 모듈을 불러 와야 함.

왜인지는 모르겠으나 package.json에서 "type":"module"을 입력해도 import를 사용할 수 없었음.

src/test/runTest.ts - test진입점

/* eslint-disable @typescript-eslint/no-var-requires */
const { runTests } = require('@vscode/test-electron');
const path = require('path');

async function main() {
  try {
    // The folder containing the Extension Manifest package.json
    // Passed to `--extensionDevelopmentPath`
    const extensionDevelopmentPath = path.resolve(__dirname, '../../');
    // The path to test runner
    // Passed to --extensionTestsPath
    const extensionTestsPath = path.resolve(__dirname, './suite/index');
    // Download VS Code, unzip it and run the integration test
    await runTests({ extensionDevelopmentPath, extensionTestsPath });
  } catch (err) {
    console.error("Failed to run tests");
    process.exit(1);
  }
}

main();

srt/test/suite/index.ts

/* eslint-disable @typescript-eslint/no-var-requires */
const glob = require("glob");
const MochaLib = require("mocha");
const pathUtil = require("path");

function run(): Promise<void> {
  const mocha = new MochaLib({
    ui: "tdd",
    color: true,
  });

  const testsRoot = pathUtil.resolve(__dirname, "..");

  return new Promise<void>((c, e) => {
    glob("**/**.test.js", { cwd: testsRoot }, (err: Error | null, files: string[]) => {
      if (err) {
        return e(err);
      }

      files.forEach((f: string) => mocha.addFile(pathUtil.resolve(testsRoot, f)));

      try {
        mocha.run((failures: number) => {
          if (failures > 0) {
            e(new Error(`${failures} tests failed.`));
          } else {
            c();
          }
        });
      } catch (err) {
        console.error(err);
        e(err);
      }
    });
  });
}

module.exports = { run };

testcode 작성하기

  • test하고자 하는 파일 이름에 .test.를 입력 예를 들면 extension.ts의 경우 extension.test.ts

테스트 코드 예시

// src/test/suite/extension.test.ts

/* eslint-disable @typescript-eslint/no-var-requires */
const assert = require("assert");
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
const vscode = require("vscode");
// const myExtension = require('../../extension');

suite("Extension Test Suite", () => {
  vscode.window.showInformationMessage("Start all tests.");

  test("Sample test", () => {
    assert.strictEqual(-1, [1, 2, 3].indexOf(5));
    assert.strictEqual(-1, [1, 2, 3].indexOf(0));
  });
});

실행 결과

image