在 Visual Studio 中对 Typescript 进行单元测试

问题描述 投票:0回答:2

我已经寻找一种在 Visual Studio 中测试 Typescript 的方法两天了。这样做似乎有一个问题。我尝试过使用不同的框架,但我无法让它在单独的项目中工作。有人对如何让它在单独的项目中工作有什么建议吗?我需要创建一个可以运行该程序的 Web 界面吗?

谢谢你。

unit-testing typescript visual-studio-2017
2个回答
2
投票

大多数 TypeScript 单元测试框架都在命令行上运行。

要将这些与 Visual Studio 集成,您可以使用像 Gulp 这样的任务运行程序。

gulpfile.js
设置为运行测试后,它将在 Visual Studio“任务运行程序资源管理器”窗口中可见
CTRL
+
ALT
+
Backspace

您可以将任务设置为在构建解决方案时运行,方法是在任务运行程序资源管理器中右键单击该任务并选择“绑定 -> 构建后”。

Karma 的 Gulp 文件示例:

/// <binding AfterBuild='test' />
var gulp = require('gulp');
var Server = require('karma').Server;

gulp.task('test', function (done) {
  new Server({
    configFile: __dirname + '/karma.conf.js',
    singleRun: true
  }, done).start();
});

0
投票

这不是一个普通的堆栈:

stderr | src/packagePlugin/__tests__/bad.test.ts > PopUp Component > renders correctly with default props
[Vue warn]: injection "Symbol(v-scx)" not found.
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Server rendering context not provided. Make sure to only call useSSRContext() conditionally in the server build. 
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" > 
  at <VTUROOT>
[Vue warn]: Component is missing template or render function:  {
  name: 'PopUp',
  setup: [Function (anonymous)],
  props: {
    id: { type: [Function: String], default: null },
    mode: { type: [Function: String], required: true },
    props: { type: [Function: Object], default: [Function: default] },
    refSource: { type: [Function: Object], default: null },
    autoOpen: { type: [Function: Boolean], default: false }
  },
  methods: {
    getFill: [Function: getFill],
    forceHide: [Function: forceHide],
    refresh: [Function: refresh],
    log: [Function: log],
    show: [Function: show],
    getPos: [Function: getPos],
    hide: [Function: hide],
    startUp: [Function: startUp]
  },
  mounted: [Function: mounted],
  beforeUnmount: [Function: beforeUnmount],
  ssrRender: [Function: _sfc_ssrRender],
  __file: 'C:/source/gw-popup/src/packagePlugin/components/PopUp.vue',
  components: {}
}
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" >
  at <VTUROOT>

stderr | src/packagePlugin/__tests__/bad.test.ts > PopUp Component > shows and hides the popup
[Vue warn]: injection "Symbol(v-scx)" not found.
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Server rendering context not provided. Make sure to only call useSSRContext() conditionally in the server build.
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Component is missing template or render function:  {
  name: 'PopUp',
  setup: [Function (anonymous)],
  props: {
    id: { type: [Function: String], default: null },
    mode: { type: [Function: String], required: true },
    props: { type: [Function: Object], default: [Function: default] },
    refSource: { type: [Function: Object], default: null },
    autoOpen: { type: [Function: Boolean], default: false }
  },
  methods: {
    getFill: [Function: getFill],
    forceHide: [Function: forceHide],
    refresh: [Function: refresh],
    log: [Function: log],
    show: [Function: show],
    getPos: [Function: getPos],
    hide: [Function: hide],
    startUp: [Function: startUp]
  },
  mounted: [Function: mounted],
  beforeUnmount: [Function: beforeUnmount],
  ssrRender: [Function: _sfc_ssrRender],
  __file: 'C:/source/gw-popup/src/packagePlugin/components/PopUp.vue',
  components: {}
}
  at <PopUp mode="tooltip" id="test-popup" ref="VTU_COMPONENT" >
  at <VTUROOT>

stderr | src/packagePlugin/__tests__/bad.test.ts > ToolTip Component > renders correctly
[Vue warn]: injection "Symbol(v-scx)" not found.
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Server rendering context not provided. Make sure to only call useSSRContext() conditionally in the server build.
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Component is missing template or render function:  {
  name: 'ToolTip',
  components: {
    PopUp: {
      name: 'PopUp',
      setup: [Function (anonymous)],
      props: [Object],
      methods: [Object],
      mounted: [Function: mounted],
      beforeUnmount: [Function: beforeUnmount],
      ssrRender: [Function: _sfc_ssrRender],
      __file: 'C:/source/gw-popup/src/packagePlugin/components/PopUp.vue'
    }
  },
  setup: [Function (anonymous)],
  methods: {
    registerTooltips: [Function: registerTooltips],
    show: [Function: show],
    hide: [Function: hide]
  },
  props: {
    id: { type: [Function: String], default: 'ToolTip' },
    cssClass: { type: [Function: String], default: 'toolTip' }
  },
  mounted: [Function: mounted],
  beforeUnmount: [Function: beforeUnmount],
  ssrRender: [Function: _sfc_ssrRender],
  __file: 'C:/source/gw-popup/src/packagePlugin/components/ToolTip.vue'
}
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>

stderr | src/packagePlugin/__tests__/bad.test.ts > ToolTip Component > registers tooltips correctly
[Vue warn]: injection "Symbol(v-scx)" not found.
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Server rendering context not provided. Make sure to only call useSSRContext() conditionally in the server build.
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>
[Vue warn]: Component is missing template or render function:  {
  name: 'ToolTip',
  components: {
    PopUp: {
      name: 'PopUp',
      setup: [Function (anonymous)],
      props: [Object],
      methods: [Object],
      mounted: [Function: mounted],
      beforeUnmount: [Function: beforeUnmount],
      ssrRender: [Function: _sfc_ssrRender],
      __file: 'C:/source/gw-popup/src/packagePlugin/components/PopUp.vue'
    }
  },
  setup: [Function (anonymous)],
  methods: {
    registerTooltips: [Function: registerTooltips],
    show: [Function: show],
    hide: [Function: hide]
  },
  props: {
    id: { type: [Function: String], default: 'ToolTip' },
    cssClass: { type: [Function: String], default: 'toolTip' }
  },
  mounted: [Function: mounted],
  beforeUnmount: [Function: beforeUnmount],
  ssrRender: [Function: _sfc_ssrRender],
  __file: 'C:/source/gw-popup/src/packagePlugin/components/ToolTip.vue'
}
  at <ToolTip id="test-tooltip" ref="VTU_COMPONENT" >
  at <VTUROOT>

✓ src/packagePlugin/__tests__/index.test.ts (1)
❯ src/packagePlugin/__tests__/bad.test.ts (4)
  ❯ PopUp Component (2)
    × renders correctly with default props
    × shows and hides the popup
  ❯ ToolTip Component (2)
    × renders correctly
    × registers tooltips correctly

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 4 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

FAIL  src/packagePlugin/__tests__/bad.test.ts > PopUp Component > renders correctly with default props
FAIL  src/packagePlugin/__tests__/bad.test.ts > PopUp Component > shows and hides the popup
FAIL  src/packagePlugin/__tests__/bad.test.ts > ToolTip Component > renders correctly
FAIL  src/packagePlugin/__tests__/bad.test.ts > ToolTip Component > registers tooltips correctly
TypeError: Cannot read properties of null (reading 'createComment')
❯ createComment node_modules/@vue/runtime-dom/dist/runtime-dom.cjs.js:47:32
❯ processCommentNode node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4662:17
❯ patch node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4564:9
❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5215:11
❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:226:19
❯ setupRenderEffect node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5343:5
❯ mountComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5118:7
❯ processComponent node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5071:9
❯ patch node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:4600:11
❯ ReactiveEffect.componentUpdateFn [as fn] node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:5215:11
© www.soinside.com 2019 - 2024. All rights reserved.