0

我正在尝试以ALM-Rangers/Countdown-Widget-Extension为例,将单元测试添加到我自己的 VSS SDK Web 扩展中。

我能够下载并运行测试,但是当我尝试将其应用于我自己的项目(Microsoft/vsts-team-calendar 的一个分支)时,运行时出现错误tsc ./tests/TestSpec.ts

src/Calendar/Utils/TestDate.ts(1,29):错误 TS2307:找不到模块“VSS/Utils/Date”。

测试规范.ts:

import jasmine = require("jasmine");
import * as DateUtils from "../src/Calendar/Utils/TestDate";

describe('JavaScript addition operator', function() {
    it ('adds two numbers together', function(){
        expect(1+2).toEqual(3);
    });
});

describe('getDatesInRange', () => {
    it('gets dates in range', () => {
        expect(DateUtils.getDatesInRange(
            new Date(2018, 4, 25), 
            new Date(2018,4,26)
        ))
        .toEqual([
            new Date(2018, 4, 25), 
            new Date(2018,4,26)
        ]);
    });
});

测试日期.ts:

import Utils_Date = require("VSS/Utils/Date");

export function getDatesInRange(startDate:Date, endDate: Date): Date[] {
    const dates = [];
    let current: Date = startDate;
    while (current.getTime() <= endDate.getTime()) {
        dates.push(new Date(<any>current));
        current = Utils_Date.addDays(current, 1);
    }
    return dates;
}

webpack 模块规则:

module: {
    rules: [{ test: /\.tsx?$/, loader: "ts-loader" }],
},

tsconfig.json:

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "noUnusedLocals": true,
        "module": "amd",
        "moduleResolution": "node",
        "target": "es5",
        "outDir": "dist/",
        "rootDir": "src/",
        "lib": [
            "es5",
            "dom",
            "es2015.promise"
        ],
        "importHelpers": true,
        "noEmitHelpers": true,
        "skipLibCheck": true,
        "types": [
            "vss-web-extension-sdk",
            "fullcalendar",
            "jquery"
        ]
    },
    "include": [
        "src/**/*.ts"
    ]
}

文件结构:

root
|-- dist
|-- node_modules
|-- scripts
|-- tests
     |-- TestSpec.js
     |-- TestSpec.ts
|-- src
     |-- Calendar
            |-- Controls
                  |-- *Control.ts
            |-- Dialogs
                  |-- EditEventDialog.ts
            |-- Enhancers
                  |-- *Enhancer.ts
            |-- EventSources
                  |-- *EventSource.ts
            |-- Utils
                  |-- TestDate.ts
                  |-- Color.ts
                  |-- Date.ts
                  |-- Guid.ts
                  |-- Promise.ts
            |-- Views
                  |-- CalendarView.ts
                  |-- CalendarView.ts
            |-- Calendar.ts
            |-- CalendarServices.ts
            |-- Contracts.ts
            |-- DateRelativeToValidator.ts
            |-- EventSourceCollection.ts
            |-- Extension.ts
            |-- NamedListData.ts
|-- static
     |-- css
     |-- images
     |-- *.html
package.json
tsconfig.json
vss-extension.json
webpack.config.js

以下是 vss-extension.json 的一些潜在相关部分。不能说我理解这部分:

"files": [
    {
        "path": "static/images",
        "addressable": true
    },
    {
        "path": "static/css",
        "addressable": true
    },
    {
        "path": "dist",
        "packagePath": "js",
        "addressable": true
    },
    {
        "path": "node_modules/vss-web-extension-sdk/lib",
        "addressable": true,
        "packagePath": "sdk"
    },
    {
        "path": "node_modules/fullcalendar/dist",
        "addressable": true,
        "packagePath": "dist/fullcalendar"
    },
    {
        "path": "node_modules/moment",
        "addressable": true,
        "packagePath": "dist/moment"
    },
    {
        "path": "node_modules/moment-timezone",
        "addressable": true,
        "packagePath": "dist/moment-timezone"
    },
    {
        "path": "node_modules/jquery/dist",
        "addressable": true,
        "packagePath": "dist/jquery"
    },
    {
        "path": "node_modules/tablesorter/dist",
        "addressable": true,
        "packagePath": "dist/tablesorter"
    },
    {
        "path": "static/calendar.html",
        "addressable": true,
        "contentType": "text/html"
    },
    {
        "path": "static/calendarServices.html",
        "addressable": true,
        "contentType": "text/html"
    },
    {
        "path": "static/daysOffControls.html",
        "addressable": true,
        "contentType": "text/html"
    },
    {
        "path": "static/freeFormControls.html",
        "addressable": true,
        "contentType": "text/html"
    },
    {
        "path": "LICENSE.txt",
        "addressable": true,
        "contentType": "text/html"
    }
],

更多来自 webpack 配置:

 return {
        entry: {
            "main": "./" + path.relative(process.cwd(), path.join(__dirname, "src", "Calendar", "Extension.ts")),
            "calendarServices": "./" + path.relative(process.cwd(), path.join(__dirname, "src", "Calendar", "CalendarServices.ts")),
            "freeformEventControl":  "./" + path.relative(process.cwd(), path.join(__dirname, "src", "Calendar", "Controls", "FreeFormEventControl.ts")),
            "capacityEventControl":  "./" + path.relative(process.cwd(), path.join(__dirname, "src", "Calendar", "Controls", "CapacityEventControl.ts"))
        },
        output: {
            filename: path.relative(process.cwd(), path.join(__dirname, "dist", "[name].js")),
            libraryTarget: "amd",
        },
4

0 回答 0