我有两个 TypeScript 包,一个包(包 A)依赖于另一个(包 B)。每个包都有一个使用 Karma 设置的单元测试。从 NPM 安装所有依赖项后,当我为每个单独运行单元测试时,单元测试运行良好。但是,如果我npm link package-b在包 A 中使用并运行包 A 的单元测试,我会收到标题中所述的错误:“TS2322:类型‘超时’不可分配给‘数字’类型。”
有问题的线路是对 的调用setTimeout。挖后,我发现,虽然单独运行测试,没有npm link,打字稿正确识别setTimeout的签名typescript/lib/lib.dom为所需的类型,但在使用后出现故障的情况下,npm link它使用节点的使用setTimeout中的签名@types/node/index。我通过将返回类型更改setTimeout为string并string在Timeout.
我不确定的是为什么TypeScript 编译器决定在这种特定情况下使用替代定义,也不确定我如何说服它使用所需的定义。我很高兴发布一些代码,但我不确定在这种情况下什么有用,因为失败的线路上只有setTimeout调用。
我想在字符串中转义转义序列.
例如:如果我有一个字符串,其内容是"\n\u0073",我需要逃避他们以这样的方式,如果我把它打印到命令行,我会看到
this:
\n\u0073
instead of:
s
Run Code Online (Sandbox Code Playgroud)
我也将转义双引号(")和反斜杠(\),我想出一个表达式来逃避已经:
Pattern p = Pattern.compile("([\"\\\\])");
String str = p.matcher("\"\n\u0073\\"").replaceAll("\\\\$1");
Run Code Online (Sandbox Code Playgroud)
这让我产生了:
\"
s\\
Run Code Online (Sandbox Code Playgroud)
但是,它不会处理转义序列.我想要的是:
\"\n\u0073\\
Run Code Online (Sandbox Code Playgroud)
我需要做哪些修改才能逃脱转义序列?
我已阅读Google 能否模拟具有智能指针返回类型的方法?但它并没有真正给出太多答案。
我有一个返回 unique_ptr 实例的工厂。返回 unique_ptr 是一项要求,如果没有真正充分的理由和与高于我的薪酬等级的人进行讨论,就不能更改(如果事实证明您不应该返回这些东西,我愿意这样做)。
在被测试的代码中,存在三个有问题的对象。第一个是各种处理程序。使用该处理程序,您可以创建第二个对象,这是所有事情都说完并完成后唯一真正有趣的对象。但是创建第二个是一个复杂的过程,需要特定处理程序实例完成额外的工作,以及工厂(第三个对象)来处理创建 Mr. Second 所需的逻辑,而不管处理程序要求如何。
这是有问题的测试代码(更改了类名以避免 HR 地狱):
class BarTest : public ::testing::Test
{
protected:
Bar foo;
};
TEST_F(BarTest, createFoo)
{
// Data used in this test
std::string fooName = "test";
MockFooFactory<int> fooFactory;
MockFoo<int>* foo = new MockFoo<int>;
// When createFoo is called, it should call the foo factory's construct method with the given
// fooName and a pointer to the calling bar
EXPECT_CALL(fooFactory, construct(fooName, &bar)).
WillOnce(::testing::Return(std::unique_ptr<Foo<int>>(foo)));
// Test it
std::unique_ptr<Foo<int>> returnedFoo …Run Code Online (Sandbox Code Playgroud) const http = require("http");
function createServer(
name,
hostname
)
{
const server = http.createServer(
(
request,
response
) =>
{
console.log("request on", name);
response.end(name);
}
);
server.on(
"error",
(error) => console.log(`${name} error`, error)
);
server.on(
"close",
() => console.log(`${name} closed`)
);
server.listen(4321, hostname, () => console.log(`${name} listening (${server.listening}) on address`, server.address()));
}
createServer(
"Server 1",
"localhost"
);
createServer(
"Server 2",
"127.0.0.1"
);
createServer(
"Server 3",
"127.0.1.1"
);
createServer(
"Server 4",
"localhost"
);
createServer(
"Server 5",
"10.9.129.49"
);
createServer(
"Server 6", …Run Code Online (Sandbox Code Playgroud) 当尝试在 Jest 中渲染我的应用程序以进行一些集成测试时,我收到以下错误:
TypeError: Cannot read property 'createNode' of undefined
at AnimatedParam.createNode [as __nativeInitialize] (node_modules/react-native-reanimated/src/core/AnimatedNode.js:126:24)
at AnimatedParam.__nativeInitialize [as __attach] (node_modules/react-native-reanimated/src/core/AnimatedNode.js:71:10)
at new __attach (node_modules/react-native-reanimated/src/core/AnimatedParam.js:11:10)
at createAnimatedParam (node_modules/react-native-reanimated/src/core/AnimatedParam.js:71:10)
at createAnimatedFunction (node_modules/react-native-reanimated/src/core/AnimatedFunction.js:38:17)
at Object.<anonymous> (node_modules/react-native-reanimated/src/derived/interpolate.js:17:39)
Run Code Online (Sandbox Code Playgroud)
它抱怨的代码react-native-reanimated如下所示:
__nativeInitialize() {
if (!this.__initialized) {
ReanimatedModule.createNode(this.__nodeID, { ...this.__nodeConfig });
this.__initialized = true;
}
}
Run Code Online (Sandbox Code Playgroud)
并且是来自库ReanimatedModule的类型别名。除此之外,我还没有找到任何有助于解决此问题的有用信息。NativeModulereact-native
这里特别奇怪的是,据我所知,我没有直接react-native-reanimated在我的代码库中使用,并且我能找到的唯一使用它的库组件没有在正在测试的组件中呈现。
我无法以任何合理的方式精简我的代码来重现此问题,并且相关代码受公司版权保护,因此我无法共享存储库。我将继续尝试在一个小示例中重现该错误,但我想将这个问题提出来,以防有人对此问题有任何经验。
c++ ×1
escaping ×1
gmock ×1
googlemock ×1
java ×1
javascript ×1
jestjs ×1
karma-mocha ×1
karma-runner ×1
mocking ×1
node.js ×1
port ×1
react-native ×1
regex ×1
server ×1
typescript ×1
unique-ptr ×1
unit-testing ×1