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

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

内容简介: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

详细内容查看发布公告


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

查看所有标签

猜你喜欢:

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

谋局者

谋局者

何常在 / 北京联合出版公司 / 2017-1 / 39.80

★商战版《官场笔记》!全面超越《问鼎》《交手》!商战小说*大神何常在迄今为止至为满意之作! ★以马云、马化腾、李彦宏、雷军、刘强东、张朝阳等大佬为原型,写透高手们的大智慧、大手腕、大谋略! ★善谋者胜,善算者赢!内含大量阳谋诡计、商业运作、商业谈判、事件营销等可以读以致用的知识!是商界人士必看读物! ★全景再现互联网三大帝国七大诸侯从无到有从有到强从强到吞并一切的成长和并购史! ......一起来看看 《谋局者》 这本书的介绍吧!

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

在线压缩/解压 CSS 代码

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

在线 XML 格式化压缩工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具