这不是我第一次遇到"1 timer(s) still in the queue",但通常我会找到某种方法使用tick()或detectChanges()等来摆脱它。
下面的测试工作正常,直到我尝试测试我知道应该抛出异常的条件:
it('should be able to change case', fakeAsync(() => {
expect(component).toBeTruthy();
fixture.whenStable().then(fakeAsync(() => {
component.case = 'lower';
fixture.autoDetectChanges();
tick(500);
const input = fixture.nativeElement.querySelector('input') as HTMLInputElement;
typeInElement('abcDEF', input);
fixture.autoDetectChanges();
tick(500);
expect(component.text).toEqual('abcdef');
component.case = 'upper';
fixture.autoDetectChanges();
tick(500);
typeInElement('abcDEF', input);
fixture.autoDetectChanges();
tick(500);
expect(component.text).toEqual('ABCDEF');
// Everything above works fine. Here's where the trouble begins
expect(() => {
component.case = 'foo';
fixture.autoDetectChanges();
tick(500);
}).toThrowError(/Invalid case attribute/);
}));
}));
Run Code Online (Sandbox Code Playgroud)
我正在测试的是一个 Angular 组件,它是 Material 输入字段的包装器。该组件有许多可选属性,其中大多数只是常见输入字段功能的传递属性,但也有一些自定义属性,例如我在上面测试的用于大写/小写转换的属性。 …
我有一个src包含源代码和单元测试的目录,以及一个test包含单独速度测试的目录。
当我使用 构建项目时tsc,我得到如下目录结构:
dist/
src/
index.js
...
test/
speed-test.js
Run Code Online (Sandbox Code Playgroud)
然而,我更喜欢将“平面”输出发送到我的dist目录,如下所示:
dist/
index.js
...
speed-test.js
...
Run Code Online (Sandbox Code Playgroud)
如果我speed-test.ts从test目录中删除,tsc则不会将src目录添加到dist. 仅当需要(或者至少当tsc决定需要时)来区分编译代码的源时,才会添加额外的目录结构。
我确信这有时对于避免文件名冲突非常有用,但在这种情况下这对我来说并不重要,而且我不想得到这个额外的“帮助”。
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noImplicitReturns": true,
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加"rootDirs"的选项["src", "test"],但这没有帮助。 …
我想在 webpack 和 babel 处理文件后对其进行操作。emit在保存新文件之前有一个钩子被触发,但我看不到操作文件内容的方法。所以我决定使用afterEmit钩子读取刚刚写入的文件,修改它,然后写回:
plugins: [
new class OutputMonitor {
apply(compiler) {
compiler.hooks.afterEmit.tap('OutputMonitor', compilation => {
if (compilation.emittedAssets.has('index.js')) {
let contents = fs.readFileSync('./dist/web/index.js', 'utf-8');
// Strip out dynamic import() so it doesn't generate warnings.
contents = contents.replace(/import(?=\("tseuqer-yb")/, 'console.log');
// Strip out large and large-alt timezone definitions from this build.
contents = contents.replace(large, 'null');
contents = contents.replace(largeAlt, 'null');
fs.writeFileSync('./dist/web/index.js', contents);
}
});
}
}()
],
Run Code Online (Sandbox Code Playgroud)
这样就完成了工作,但是还有更好的方法吗?
我想在 AWS lambda 函数中使用 npm 包“请求”。
我正在尝试按照本文中概述的程序进行操作:https : //medium.com/@anjanava.biswas/nodejs-runtime-environment-with-aws-lambda-layers-f3914613e20e
我创建了这样的目录结构:
nodejs
? package-lock.json
? package.json
????node_modules
Run Code Online (Sandbox Code Playgroud)
我的 package.json 看起来像这样:
{
"name": "my-package-name",
"version": "1.0.0",
"description": "whatever",
"author": "My Name",
"license": "MIT",
"dependencies": {
"request": "^2.88.0"
}
}
Run Code Online (Sandbox Code Playgroud)
据我从文章中可以看出,我应该做的就是 run npm i,压缩目录,将其作为图层上传,然后将该图层添加到我的 lambda 函数中。
我已经完成了所有这些,但是当我尝试测试我的功能时,我得到的只是:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'request'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'request'",
"Require stack:",
...
Run Code Online (Sandbox Code Playgroud)
...好像从未添加过图层。无论是否添加图层,错误都完全相同。如果有某种权限问题需要解决,文章中没有任何内容表明这一点。
我尝试了一些不同的事情,例如我的 .zip 文件是否包含顶级目录“nodejs”或仅包含其内容。我试过用这样的文件添加"main": "index.js",到我的package.json, 中 …
javascript amazon-web-services node.js aws-lambda aws-lambda-layers
对于需要从 Node.js 12 更新到 14 的构建过程,我想要一个 bash 脚本来检测是否nvm已安装,如果是,则执行nvm use v14(或nvm install v14如果需要),然后我想要 nvm 选择的节点版本在 bash 脚本终止后保持在 14 ,而不仅仅是在脚本运行期间。
我可以使用此脚本切换到 v14,但脚本终止后,shell 环境仍保持在 v12:
#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
nvm --version
echo $NVM_BIN
node --version
nvm use v14
echo $NVM_BIN
node --version
Run Code Online (Sandbox Code Playgroud)
仅在 bash 脚本中执行该nvm命令很痛苦,因为nvm它不是真正的命令,而是 shell 函数,并且脚本必须使用前三行来设置nvm脚本。
输出是:
0.33.11
/home/pi/.nvm/versions/node/v12.21.0/bin
v12.21.0
Now using node …Run Code Online (Sandbox Code Playgroud) 我发现了一篇关于相关主题的不同帖子(如何在退出时执行异步操作),但它可能不适用于 macOS,或者在运行 Node.js v14 时不再适用。
我原来的问题更复杂。我无法在 SIGINT 的回调(或异步回调)中正常工作setTimeout。await但后来我意识到我真正需要做的,首先也是最重要的是能够捕获并忽略SIGINT。
如果我能做到这一点,那么如果我愿意的话,我也可以关闭以响应 SIGINT。
所以我回到基础知识,看看是否可以简单地禁用 SIGINT。我不能。
我可以检测到 SIGINT,我可以用控制台消息或任何其他同步代码进行响应,但无论我做什么,我的代码都会关闭,而且很快就会关闭。
process.on('SIGINT', () => { console.log('here'); return false; });
process.on('SIGINT', () => { console.log('here'); return true; });
process.on('SIGINT', () => { console.log('here'); return undefined; });
process.on('SIGINT', () => { console.log('here'); return new Promise(resolve => { console.log(resolve); }); });
Run Code Online (Sandbox Code Playgroud)
Ctrl这些方法都无法阻止我的 Node.js 服务器在点击+后关闭C,或者以其他方式向进程发送 SIGINT。
更新:
当我通过运行我的服务器时node app.js,SIGINT 拦截正在正常工作!
显然,我已经被通过nodemon. 来自我的 IDE 终端的+或使用 …
如下所示,我正在使用该[src]属性.我要做的是预览从设备的相机拍摄的图像.请参阅下面的其他打字稿代码.
<img [src]="lastImage" style="width: 100%" [hidden]="lastImage === null">
<button ion-button icon-left (click)="presentActionSheet()">
<ion-icon name="camera"></ion-icon>Select Image
</button>
Run Code Online (Sandbox Code Playgroud)
这是.ts代码
lastImage: string = null;
public presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image Source',
buttons: [
{
text: 'Load from Library',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
public takePicture(sourceType) {
// Create options for the Camera Dialog
var …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 webpack 生成一个 npm 包,该包将包含 ES6 级别的 JavaScript(从 TypeScript 生成),并且还将使用 ES6 模块格式,而不会转换为 ES5 和 CommonJS。(最终,除了 ES6 代码之外,我还希望在包中包含 ES5/CommonJS。)
当我尝试运行时出现此错误webpack:
[webpack-cli] 错误 [ERR_REQUIRE_ESM]:必须使用导入来加载 ES 模块:/Users/.../webpack.config.mjs
但是,正如您所看到的,我import正在我的webpack.config.mjs文件中使用它,如下所示:
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import path from 'path';
const NODE_ENV = process.env.NODE_ENV || 'production';
export default {
mode: NODE_ENV,
entry: './src/index.ts',
target: 'node',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
...
Run Code Online (Sandbox Code Playgroud)
我正在使用 node v14.15.1,它应该支持 ES6 模块生成。我的package.json看起来像这样:
{
"name": "my-package",
"version": "2.0.0",
"description": "Blah, blah, …Run Code Online (Sandbox Code Playgroud) 当我运行 karma 时,我收到以下警告:
配置中的警告 'mode' 选项尚未设置,webpack 将回退到该值的 'production'。将 'mode' 选项设置为 'development' 或 'production' 以启用每个环境的默认值。您还可以将其设置为“无”以禁用任何默认行为。了解更多:https : //webpack.js.org/concepts/mode/
我尝试按照上面链接中的建议添加mode: 'development'到我的webpack-test.config.js文件中,但这不仅没有产生任何影响,Intellij IDEA 抱怨说:
webpack:不允许属性“模式”
我的单元测试无论如何都在运行,但我想摆脱这个警告。任何帮助深表感谢。
这是我的webpack-test.config.js文件:
const path = require('path');
const webpack = require('webpack');
const ROOT = path.resolve( __dirname, 'src' );
module.exports = {
mode: 'production',
context: ROOT,
resolve: {
extensions: ['.ts', '.js'],
modules: [
ROOT,
'node_modules'
]
},
module: {
rules: [
// PRE-LOADERS
{
enforce: 'pre',
test: /\.js$/,
use: 'source-map-loader'
},
// LOADERS
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试将一些单元测试添加到我正在开发的 Visual Studio Code 扩展中。我遵循此处描述的设置扩展测试的方法:https ://code.visualstudio.com/api/working-with-extensions/testing-extension
然而,这个秘籍并没有显示任何实际测试扩展的有用信息,只是显示了进行测试的框架。
对于我想做的测试,我想做的第一件事就是打开一个示例项目。这就是我陷入困境的地方。这只是我尝试打开项目文件夹的多种变体之一:
import assert from 'assert';
import { after, before, it } from 'mocha';
import path from 'path';
import { commands, Extension, extensions, Uri, window } from 'vscode';
suite('Extension Tests', () => {
let extension: Extension<any>;
const projectFolder = Uri.file(path.join(__dirname, '../../../test/suite/sample-project'));
window.showInformationMessage('Start all tests.');
before(() => {
extension = extensions.getExtension('kshetline.ligatures-limited');
const cmd = commands.executeCommand('vscode.openFolder', projectFolder).then(
() => console.log('opened'),
() => console.log('didn\'t open'));
console.log('before');
return cmd;
});
after(() => {
console.log('after'); …Run Code Online (Sandbox Code Playgroud) unit-testing mocha.js typescript visual-studio-code vscode-extensions
javascript ×6
typescript ×4
node.js ×3
unit-testing ×3
webpack ×3
angular ×2
aws-lambda ×1
babel-loader ×1
bash ×1
cordova ×1
es6-modules ×1
ionic3 ×1
jasmine ×1
karma-runner ×1
mocha.js ×1
npm ×1
nvm ×1
sigint ×1
tsconfig ×1