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

来源: OSCHINA
编辑: 局长
2021-05-19

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

详细内容查看发布公告

展开阅读全文
3 收藏
分享
加载中
更多评论
0 评论
3 收藏
分享
返回顶部
顶部