我正在尝试测试与Headers
类交互的函数。不知道我的测试在技术上是否属于 unit -test的类别,但这应该无关。
运行测试时,我看到了臭名昭著的ReferenceError: Headers is not defined
错误。我确定是因为测试是在非浏览器环境中执行的。
我的问题是: 如何在浏览器环境中执行测试,因为我根本不想模拟Headers
类?
$ yarn test
yarn run v1.12.3
$ jest
FAIL __tests__/request-init-spec.ts
requestInit
× should work (9ms)
● requestInit › should work
ReferenceError: Headers is not defined
13 | describe(requestInit.name, () => {
14 | it('should work', () => {
> 15 | const headers = new Headers();
| ^
16 | headers.append('H1', 'V1');
17 |
18 | const requestInit: RequestInit = {
at Object.it (__tests__/request-init-spec.ts:15:21)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 4.574s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.