-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathvitest.browser.setup.mts
More file actions
58 lines (48 loc) · 1.51 KB
/
vitest.browser.setup.mts
File metadata and controls
58 lines (48 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import type { AwsCredentialIdentity } from "@aws-sdk/types";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
import { Browser } from "happy-dom";
import { beforeAll, expect } from "vitest";
import { testCredentials } from "./scripts/browser-testing/aws.testCredentials.browser";
type Writable<T extends object> = {
-readonly [K in keyof T]: T[K];
};
const sourceIdentity = testCredentials as unknown as Writable<AwsCredentialIdentity>;
/**
* This file sets up the testing environment for isomorphic e2e tests.
*
*/
declare global {
// eslint-disable-next-line no-var
var aws: {
testCredentials?: AwsCredentialIdentity | (() => Promise<AwsCredentialIdentity>);
};
}
const browser = new Browser();
browser.settings.fetch.disableSameOriginPolicy = true;
const originalCreate = FetchHttpHandler.create.bind(FetchHttpHandler);
FetchHttpHandler.create = function () {
// this tells happy-dom to include the authorization header in fetch.
return originalCreate({
...this,
credentials: "include",
});
};
if (typeof aws === "undefined") {
if (typeof global !== "undefined") {
// @ts-ignore
global.aws = {};
}
if (typeof window !== "undefined") {
// @ts-ignore
window.aws = {};
}
}
/**
* Since these are static credentials we have no way of refreshing them anyway.
* The test run must complete while the credentials are valid.
*/
delete sourceIdentity.expiration;
aws.testCredentials = sourceIdentity;
beforeAll(async () => {
expect(aws?.testCredentials).toBeDefined();
});