Deno 1.10 发布:改进内置 test runner、新增 Web Storage API

栏目: 软件资讯 · 发布时间: 3年前

内容简介:Deno 1.10 已正式发布,此版本包含许多新功能、性能优化以及错误修复。 改进内置的 test runner 在 Web Workers 中支持结构化克隆 (structured clone) 新增 Web Storage API 支持远程导入映射 (maps) 如果已经安装...

Deno 1.10 已正式发布,此版本包含许多新功能、性能优化以及错误修复。

如果已经安装了 Deno,运行deno upgrade命令即可升级到 1.10 版本。如果是首次安装,可以参考下面的方法:

# Using Shell (macOS and Linux):
curl -fsSL https://deno.land/x/install/install.sh | sh

# Using PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex

# Using Homebrew (macOS):
brew install deno

# Using Scoop (Windows):
scoop install deno

# Using Chocolatey (Windows):
choco install deno

改进内置的 test runner

Deno 1.10 对内置的 test runner 进行了重大改进。

  • 支持隔离和并行执行测试
  • 支持为测试用例指定可配置的确切权限
Deno.test({
  name: "write only",
  permissions: { write: true, read: false },
  async fn() {
    await Deno.writeTextFile("./foo.txt", "I can write!");
    console.log(await Deno.readTextFile("./foo.txt"));
  },
});
$ deno test --allow-read --allow-write --unstable test_permissions.ts
Check file:///Users/ry/src/deno/test_permissions.ts
running 1 test from file:///Users/ry/src/deno/test_permissions.ts
test write only ... FAILED (5ms)

failures:

write only
PermissionDenied: Requires read access to "./foo.txt", run again with the --allow-read flag
    at deno:core/core.js:86:46
    at unwrapOpResult (deno:core/core.js:106:13)
    at async open (deno:runtime/js/40_files.js:46:17)
    at async Object.readTextFile (deno:runtime/js/40_read_file.js:40:18)
    at async fn (file:///Users/ry/src/deno/test_permissions.ts:6:17)
    at async asyncOpSanitizer (deno:runtime/js/40_testing.js:21:9)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:58:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:85:9)
    at async runTest (deno:runtime/js/40_testing.js:199:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:244:7)

failures:

	write only

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (37ms)
  • 更好地展示 test runner 输出内容
running 4 tests from file:///dev/deno/cli/tests/unit/tty_test.ts
test consoleSizeFile ... ok (11ms)
test consoleSizeError ... ok (4ms)
test isatty ... ok (4ms)
test isattyError ... ok (3ms)
running 6 tests from file:///dev/deno/cli/tests/unit/rename_test.ts
test renameSyncSuccess ... ok (17ms)
test renameSyncReadPerm ... ok (5ms)
test renameSyncWritePerm ... ok (6ms)
test renameSuccess ... ok (13ms)
test renameSyncErrorsUnix ... ok (34ms)
test renameSyncErrorsWin ... ignored (1ms)
...
  • 支持运行测试时监视文件更改
  • 支持在文档中进行类型检测
/**
 * ```
 * import { example } from "./test_docs.ts";
 *
 * console.assert(example() == 42);
 * ```
 */
export function example(): string {
  return "example";
}

示例如下:

$ deno test --doc https://deno.com/v1.10/test_docs.ts

Check file:///dev/test_docs.ts:2-7
error: TS2367 [ERROR]: This condition will always return 'false' since the types 'string' and 'number' have no overlap.
console.assert(example() == 42);
               ~~~~~~~~~~~~~~~
    at file:///dev/test_docs.ts:2-7.ts:3:16

新增 Web Storage API

此版本增加了对 Web Storage API 的支持。该 API 由localStoragesessionStorage组成,可以用于持久存储少量数据,而无需直接访问文件系统。开发者可以不需要申请任何权限来使用localStoragesessionStorage

底层存储层和持久性对于应用程序是不透明的,因此不用担心安全性的问题。

该 API 的工作方式与在浏览器中一样:localStorage可用于在流程重新启动时永久存储多达 5MB 的数据,而 sessionStorage可用于在流程持续时间内存储少量数据。

下面是一个简单的例子:

// kv.ts

const key = Deno.args[0];

if (key === undefined) {
  // if user passes no args, display number of entries
  console.log(localStorage.length);
} else {
  const value = Deno.args[1];

  if (value === undefined) {
    // if no value is specified, return value of the key
    console.log(localStorage.getItem(key));
  } else {
    // if value is specifed, set the value
    localStorage.setItem(key, value);
  }
}
$ deno run --location https://example.com ./kv.ts
0
$ deno run --location https://example.com ./kv.ts foo bar
$ deno run --location https://example.com ./kv.ts foo
bar
$ deno run --location https://example.com ./kv.ts
1

支持远程导入映射 (maps)

Deno 1.8 中导入映射功能已稳定,到了 Deno 1.10,目前已启用远程导入映射功能。这意味着导入映射现在不必存储在本地文件系统上,它们也可以通过 HTTP 进行加载:

$ deno install --import-map=https://example.com/import_map.json -n example https://example.com/mod.ts

详细内容查看发布公告


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C++标准模板库编程实战

C++标准模板库编程实战

Ivor Horton / 郭小虎、程聪 / 2017-1

《C++标准模板库编程实战》介绍最新的C++14标准的API、库和扩展,以及如何将它们运用到C++14程序中。在书中,作者Ivor Horton 则阐述了什么是STL,以及如何将它们应用到程序中。我们将学习如何使用容器、迭代器,以及如何定义、创建和应用算法。此外,还将学习函数对象和适配器,以及它们的用法。 阅读完本书之后,你将能够了解如何扩展STL,如何定义自定义类型的C++组件,你还将能够......一起来看看 《C++标准模板库编程实战》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具