jest
允许您使用CLI 选项或文件设置全局配置值。package.json
jest.config.js
您可以在测试中设置一些配置值,例如
jest.setTimeout(10000000)
Run Code Online (Sandbox Code Playgroud)
但我找不到读取配置值的方法,例如
const initialTimeout = jest.testTimeout // this is undefined
jest.setTimeout(10000000)
// do something that takes unusually long time
jest.setTimeout(initialTimeout)
Run Code Online (Sandbox Code Playgroud)
那么如何读取测试中当前设置的全局配置值呢?
可以说我有一个私人NPM库,内举办JFrog artifactory的:
https://my-domain.com/artifactory/api/npm/my-repo
。在这个存储库中,我发布了一个 npm package: my-package
,它构建得很好。my-package
依赖(或更多)公共 npm包,例如lodash
.
但是,当我创建一个新项目并尝试安装时my-package
,出现以下错误:
$ npm install my-package --registry https://my-domain.com/artifactory/api/npm/my-repo
npm ERR! code E404
npm ERR! 404 Not Found - GET https://my-domain.com/artifactory/api/npm/my-repo/lodash - not_found
npm ERR! 404
npm ERR! 404 'lodash^4.17.11' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'my-package' …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有编译器选项或类似的东西可以使传播对象严格。
请参阅以下示例以了解我的意思:
interface Foo {
a: string;
}
interface Bar {
a: string;
b: number;
}
const barObject: Bar = { a: "a string", b: 1 };
// should give a warning because Bar has more properties (here b) than Foo
const fooObject: Foo = { ...barObject };
// actually prints 1
console.log((fooObject as any).b);
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
我想为具有以下属性的元素创建动画:
为此,我使用了一个IntersectionObserver
并且我接近了预期的结果。
我面临的唯一问题是,当我在动画期间沿滚动方向(在本例中)平移元素时transform: translateY
。这将导致IntersectionObserver
触发多次甚至无限次。
function isIntersectingFromTop(entry){
return entry.boundingClientRect.bottom != entry.intersectionRect.bottom;
}
function isIntersectingFromBottom(entry){
return entry.boundingClientRect.top != entry.intersectionRect.top;
}
var count = 0;
function incrementCounter(entry){
document.querySelector(".counter").textContent += "intersectionRation (" + count + "): " + entry.intersectionRatio + "\n";
count++;
}
let observer = new IntersectionObserver(
function (entries, observer) {
entries.forEach(function(entry){
incrementCounter(entry)
if (entry.isIntersecting) {
if(isIntersectingFromTop(entry)){
entry.target.classList.add("animated--up-in");
} else if(isIntersectingFromBottom(entry)) {
entry.target.classList.add("animated--down-in")
}
} else {
/** element is …
Run Code Online (Sandbox Code Playgroud)我想将ctest集成到c ++ / c项目中。我用谷歌测试写单元测试。
我的CMakeLists.txt的相关部分如下所示:
...
####### CREATING EXE #######
add_executable(test_exe main.cpp test.cpp)
target_link_libraries(test_exe GTest::GTest GTest::Main)
set_target_properties (test_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR})
add_test(test_exe test_exe)
Run Code Online (Sandbox Code Playgroud)
如您所见,我指定了可执行文件的输出目录(UNIT_TEST_BIN_OUTPUT_DIR)。
当我使用终端时,可执行文件可以正常工作:
cd <UNIT_TEST_BIN_OUTPUT_DIR>
./test_exe
Run Code Online (Sandbox Code Playgroud)
我想使用ctest执行测试。因此,我转到了cmake生成的“ ctest文件夹”。在这里,我想使用ctest执行cmake中“ add_test”添加的所有测试。
user@user:~/<dir to cmake>/cmake/unit_tests$ ctest
Test project /<dir to cmake>/cmake/unit_tests
Start 1: test_exe
Could not find executable test_exe
Looked in the following places:
test_exe
test_exe
Release/test_exe
Release/test_exe
Debug/test_exe
Debug/test_exe
MinSizeRel/test_exe
MinSizeRel/test_exe
RelWithDebInfo/test_exe
RelWithDebInfo/test_exe
Deployment/test_exe
Deployment/test_exe
Development/test_exe
Development/test_exe
Unable to find executable: test_exe
1/1 Test #1: test_exe ......***Not Run 0.00 sec …
Run Code Online (Sandbox Code Playgroud) 我通过声明public static contextType
消耗上下文的组件内部来使用react的新上下文API(v16.6.0或更高版本).只要声明Provider
它的组件不直接使用消耗其render()
方法中的上下文的组件,这就可以正常工作.
例:
ParentWithContext
这是创建和提供上下文的组件.
export const SomeContext = React.createContext({
someValue: false
});
export default class ParentWithContext extends Component {
public render(){
const contextValue = {someValue: true};
return (
<SomeContext.Provider value={contextValue}>
<ChildOne />
{this.props.children}
</SomeContext.Provider>
);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此组件ChildOne
在其render()
方法中使用组件(右下方).
ChildOne和ChildTwo
这两个组件只是消耗上面的上下文并显示它.
export default class ChildOne extends Component {
public static contextType = SomeContext;
public render(){
return (
<div>
{`Context of ChildOne: ${this.context.someValue}`}
</div>
);
}
}
export …
Run Code Online (Sandbox Code Playgroud) 从私有注册表安装软件包非常简单:
npm install my-package --registry https://<private-registry-url>
Run Code Online (Sandbox Code Playgroud)
这将添加一个条目到package-lock.json
:
"my-package": {
"version": "1.0.0",
"resolved": "https://<private-registry-url>/<some_path>/my-package-1.0.0.tgz",
"integrity": "sha1-Pjs/y9sEp49/OC8+8eEZFdwT3BQ="
},
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,一切都符合预期。
现在的问题是,当您想使用npm install
. 这将失败并出现以下错误:
npm install my-package --registry https://<private-registry-url>
Run Code Online (Sandbox Code Playgroud)
所以它试图my-package
从公共 npm 注册表 ( https://registry.npmjs.org/my-package
) 中获取,但当然失败了,因为my-package
它位于私有注册表中。
现在,这真的伤了我的理解package-lock.json
..不应该NPM照照package-lock.json
,看看那里的包是前解决?相反,它只是假设它必须在公共注册表中。
另一个有趣的事情是,一旦您--registry
再次手动安装带有标志的软件包,它就会起作用:
npm install my-package --registry https://<private-registry-url> && npm i
Run Code Online (Sandbox Code Playgroud)
之后它每次都会工作,直到您升级版本my-package
或切换设备。
我也尝试过npm ci
命令但没有成功(同样的错误)。
那么如何从私有注册表正确安装软件包,以便可以使用npm install
?
比方说,我们有两个接口First
和Second
:
interface First {
a: string;
b: number;
}
interface Second {
b: number;
c: Date;
}
Run Code Online (Sandbox Code Playgroud)
使用相交可以合并两个接口:
type FirstSecond = First & Second // {a: string, b: number, c: Date}
Run Code Online (Sandbox Code Playgroud)
但是可以进行内部连接,因此结果接口仅包含在两个接口中声明的属性:
type FirstSecond = First /*inner join here*/ Second // {b: number}
Run Code Online (Sandbox Code Playgroud)
这对于泛型类型尤其有用。
每当我.
在对象之后输入 a 时,自动完成下拉列表都会包含许多不必要的 css 类名作为选项:
是否可以忽略 ts/tsx 智能感知的 css 文件,所以我只能得到相关选项?
VS代码版本: 1.37.1
默认情况下jest
,您可以简单地jasmine
全局访问。但是一旦你切换testRunner
到jest-circus
,jasmine
是未定义的。以下是一个最小的、可重现的示例:
babel.config.js
module.exports = {
presets: [["@babel/preset-env", { targets: { node: "current" } }]],
};
Run Code Online (Sandbox Code Playgroud)
茉莉花规范.js
it("check jasmine", () => {
console.log(jasmine);
});
Run Code Online (Sandbox Code Playgroud)
开玩笑的配置文件
module.exports = {
rootDir: ".",
testRunner: "jest-circus/runner",
};
Run Code Online (Sandbox Code Playgroud)
包.json
{
"name": "test-jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"babel-jest": "^26.6.3",
"jest": "^26.6.3",
"jest-circus": "^26.6.3"
}
}
Run Code Online (Sandbox Code Playgroud)
运行此测试将导致以下输出:
$ npm test
> …
Run Code Online (Sandbox Code Playgroud) javascript ×3
typescript ×3
jestjs ×2
npm ×2
npm-install ×2
c++ ×1
cmake ×1
css ×1
ctest ×1
googletest ×1
html ×1
intellisense ×1
jasmine ×1
jest-circus ×1
node.js ×1
npm-registry ×1
reactjs ×1
tsx ×1