这里有些奇怪的行为。我正在使用mocha编写单元测试,该单元测试调用将LatLng转换为传单L.LatLng对象的Class的静态方法。在测试结束时,我正在检查返回的对象是否确实是L.LatLng的实例。令人惊讶的是,即使它确实是L.LatLng的一个实例,该测试也失败了。闲逛了几个小时后,我得以将问题缩小到以下范围。 如果我通过tsconfig路径参数导入Converter类
import { LeafletConverter } from '@maps/leaflet/leaflet-converter';
然后测试失败。但是,如果我通过相对路径导入它:
import { LeafletConverter } from '../../../app/maps/leaflet/leaflet-converter';
然后,测试通过且没有错误。问题是,这是怎么回事,有什么办法可以保持'@maps / ...'导入样式?
版本:
$ tsc --version
Version 3.6.3
VSCode 1.39.2
测试文件:
import { expect } from 'chai';
import 'mocha';
import L from "leaflet";
import { LeafletConverter as DirectLeafletConverter } from '../../../app/maps/leaflet/leaflet-converter';
import { LeafletConverter } from '@maps/leaflet/leaflet-converter';
import { LatLng } from '@maps/lat-lng';
describe.only('maps/leaflet/asdf',() => {
it('tsconfig path import - this test fails',() => {
const leafletll = LeafletConverter.toLeafletLatLng(new LatLng(1,2));
const test = leafletll instanceof L.LatLng;
expect(test).to.be.true;
});
it('direct import - this test passes',() => {
const leafletll = DirectLeafletConverter.toLeafletLatLng(new LatLng(1,2));
const test = leafletll instanceof L.LatLng;
expect(test).to.be.true;
});
});
LeafletConverter.ts
import { LatLng } from "@maps/lat-lng";
import L from "leaflet";
import { LatLngBounds } from "@maps/lat-lng-bounds";
export abstract class LeafletConverter {
private constructor(){}
...
public static toLeafletLatLng(latLng: LatLng): L.LatLng {
let result: L.LatLng = null;
if(LatLng.isValid(latLng)) {
result = L.latLng(latLng.lat,latLng.lng);
}
return result;
}
...
}
测试命令和输出:
$ "C:\\Program Files\\nodejs\\node.exe" --inspect-brk=22915 node_modules\\mocha\\bin\\_mocha -r ts-node/register -r tsconfig-paths/register --timeout 30000 --colors C:\\Users\\Asdf\\tmp\\rm.js\\group
test/**/*.test.ts
Debugger listening on ws://127.0.0.1:22915/e1b8ec38-90ac-41dd-aded-c2c20e2ffa28
For help,see: https://nodejs.org/en/docs/inspector
Debugger attached.
maps/leaflet/asdf
1) tsconfig path import
√ direct import
1 passing (14ms)
1 failing
1) maps/leaflet/asdf
tsconfig path import:
AssertionError: expected false to be true
+ expected - actual
-false
+true
at Context.<anonymous> (C:\Users\Asdf\tmp\rm.js\group-clusterer\src\test\maps\leaflet\asdf.test.ts:16:27)
Waiting for the debugger to disconnect...
package.json
{
"name": "typescript-webpack-seed-project","version": "0.0.1","description": "","main": "index.js","scripts": {
"test": "mocha -r ts-node/register -r tsconfig-paths/register src/test/**/*.test.ts","compile": "npx tsc","cc": "npx tsc -w","build": "webpack"
},"author": "","license": "ISC","devDependencies": {
"@types/chai": "^4.2.3","@types/googlemaps": "^3.38.0","@types/leaflet": "^1.5.5","@types/mocha": "^5.2.7","@types/node": "^12.12.5","@types/q": "^1.5.2","@types/rbush": "^2.0.3","chai": "^4.2.0","jsdom": "15.2.1","jsdom-global": "3.0.2","leaflet": "^1.5.1","mocha": "^6.2.1","ts-loader": "^6.2.0","ts-node": "^8.4.1","tsconfig-paths": "^3.9.0","tsconfig-paths-webpack-plugin": "^3.2.0","tslint": "^5.20.0","typescript": "^3.6.3","uglifyjs-webpack-plugin": "^2.2.0","webpack": "^4.41.2","webpack-cli": "^3.3.9"
},"dependencies": {
"q": "^1.5.1","rbush": "^3.0.1"
}
}