Skip to content

Commit 7c07de3

Browse files
thomasballingerConvex, Inc.
authored and
Convex, Inc.
committedJul 26, 2024·
Custom test environment of happy-dom + ws (#28369)
The jsdom WebSocket implementation seems less reliable than the (less accurate imitation of the browser WebSocket implementation) ws library. Use a custom test environment that is happy-dom + ws for these tests. GitOrigin-RevId: 4579d0a45d7be4ab2e15c8abd17c32a1cca4a5bf
1 parent cd2a58c commit 7c07de3

15 files changed

+82
-26
lines changed
 

‎.eslintrc.cjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,12 @@ module.exports = {
7575
// vitest (manually enabled until we can upgrade eslint)
7676
"vitest/no-focused-tests": ["error", { fixable: false }],
7777
},
78-
ignorePatterns: ["node_modules", "dist", "*.js", "tmpDist*", "tmpPackage*"],
78+
ignorePatterns: [
79+
"node_modules",
80+
"dist",
81+
"*.js",
82+
"tmpDist*",
83+
"tmpPackage*",
84+
"custom-vitest-enviroment.ts",
85+
],
7986
};

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unpublished
4+
5+
- Drop support for Node.js v16, and with it drop the dependency on node-fetch.
6+
This removes the 'punycode' deprecation warning printed when running the CLI
7+
in more recent versions of Node.js.
8+
39
## 1.13.2
410

511
- Fix `npx convex import` regression in 1.13.1

‎custom-vitest-enviroment.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { Environment } from "vitest";
2+
import { builtinEnvironments, populateGlobal } from "vitest/environments";
3+
4+
import ws from "ws";
5+
const nodeWebSocket = ws as unknown as typeof WebSocket;
6+
7+
const happy = builtinEnvironments["happy-dom"];
8+
9+
export default <Environment>{
10+
name: "happy-dom-plus-ws",
11+
transformMode: happy.transformMode,
12+
// optional - only if you support "experimental-vm" pool
13+
async setupVM(options) {
14+
const { getVmContext: happyGetVmContext, teardown: happyTeardown } =
15+
await happy.setupVM!(options);
16+
return {
17+
getVmContext() {
18+
const context = happyGetVmContext();
19+
return context;
20+
},
21+
teardown() {
22+
return happyTeardown();
23+
// called after all tests with this env have been run
24+
},
25+
};
26+
},
27+
async setup(global, options) {
28+
const { teardown: happyTeardown } = await happy.setup(global, options);
29+
//populateGlobal(global, original, {});
30+
// Add the websocket here!
31+
global.myNewGlobalVariable = 8;
32+
global.WebSocket = nodeWebSocket;
33+
34+
// custom setup
35+
return {
36+
teardown(global) {
37+
const ret = happyTeardown(global);
38+
delete global.myNewGlobalVariable;
39+
return ret;
40+
// called after all tests with this env have been run
41+
},
42+
};
43+
},
44+
};

‎src/browser/sync/client.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44

55
import { test, expect } from "vitest";
66

77
import { BaseConvexClient } from "./client.js";
88
import { anyApi } from "../../server/api.js";
99

10-
test.skip("localQueryResult reflects optimistic results", async () => {
10+
test("localQueryResult reflects optimistic results", async () => {
1111
const client = new BaseConvexClient("http://127.0.0.1:8000", () => {
1212
// ignore updates.
1313
});
@@ -27,7 +27,7 @@ test.skip("localQueryResult reflects optimistic results", async () => {
2727
expect(client.localQueryResult("myUdf", {})).toBe(true);
2828
});
2929

30-
test.skip("Client warns when old clientConfig format is used", async () => {
30+
test("Client warns when old clientConfig format is used", async () => {
3131
expect(() => {
3232
new BaseConvexClient(
3333
{ address: "http://127.0.0.1:8000" } as any,

‎src/browser/sync/protocol.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44

55
import { test, expect } from "vitest";
66

77
import { Long } from "../long.js";
88
import { longToU64, u64ToLong } from "./protocol.js";
99

10-
test.skip("Long serialization", async () => {
10+
test("Long serialization", async () => {
1111
expect(Long.fromNumber(89234097497)).toEqual(
1212
u64ToLong(longToU64(Long.fromNumber(89234097497))),
1313
);

‎src/nextjs/nextjs.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { vi, expect, test, describe, beforeEach, afterEach } from "vitest";
55

@@ -13,15 +13,15 @@ import { preloadQuery, preloadedQueryResult } from "./index.js";
1313

1414
const address = "https://127.0.0.1:3001";
1515

16-
describe.skip("env setup", () => {
16+
describe("env setup", () => {
1717
test("requires NEXT_PUBLIC_CONVEX_URL", async () => {
1818
await expect(preloadQuery(anyApi.myQuery.default)).rejects.toThrow(
1919
"Environment variable NEXT_PUBLIC_CONVEX_URL is not set.",
2020
);
2121
});
2222
});
2323

24-
describe.skip("preloadQuery and usePreloadedQuery", () => {
24+
describe("preloadQuery and usePreloadedQuery", () => {
2525
beforeEach(() => {
2626
global.process.env.NEXT_PUBLIC_CONVEX_URL = address;
2727
global.fetch = vi.fn().mockResolvedValue({

‎src/react-auth0/ConvexProviderWithAuth0.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { test } from "vitest";
55
import React from "react";
66
import { ConvexProviderWithAuth0 } from "./ConvexProviderWithAuth0.js";
77
import { ConvexReactClient } from "../react/index.js";
88

9-
test.skip("Helpers are valid children", () => {
9+
test("Helpers are valid children", () => {
1010
const convex = new ConvexReactClient("https://localhost:3001");
1111

1212
const _ = (

‎src/react-clerk/ConvexProviderWithClerk.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { test } from "vitest";
55
import React from "react";
66
import { ConvexProviderWithClerk } from "./ConvexProviderWithClerk.js";
77
import { ConvexReactClient } from "../react/index.js";
88
import { useAuth } from "@clerk/clerk-react";
99

10-
test.skip("Helpers are valid children", () => {
10+
test("Helpers are valid children", () => {
1111
const convex = new ConvexReactClient("https://localhost:3001");
1212

1313
const _ = (

‎src/react/ConvexAuthState.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @jest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { expect, vi, test } from "vitest";
55
import { act, render, screen } from "@testing-library/react";

‎src/react/auth_helpers.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { test } from "vitest";
55
import React from "react";
66
import { Authenticated, AuthLoading, Unauthenticated } from "./auth_helpers.js";
77

8-
test.skip("Helpers are valid children", () => {
8+
test("Helpers are valid children", () => {
99
const _element = (
1010
<div>
1111
<Authenticated>Yay</Authenticated>
@@ -15,7 +15,7 @@ test.skip("Helpers are valid children", () => {
1515
);
1616
});
1717

18-
test.skip("Helpers can take many children", () => {
18+
test("Helpers can take many children", () => {
1919
const _element = (
2020
<div>
2121
<Authenticated>

‎src/react/auth_websocket.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
import { expect, vi, test, describe } from "vitest";
55
import jwtEncode from "jwt-encode";
@@ -17,7 +17,7 @@ const testReactClient = (address: string) =>
1717
});
1818

1919
// On Linux these can retry forever due to EADDRINUSE so run then sequentially.
20-
describe.skip.sequential("auth websocket tests", () => {
20+
describe.sequential("auth websocket tests", () => {
2121
// This is the path usually taken on page load after a user logged in,
2222
// with a constant token provider.
2323
test("Authenticate via valid static token", async () => {

‎src/react/client.test.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import React from "react";
1010
import { renderHook } from "@testing-library/react";
1111
import { anyApi, makeFunctionReference } from "../server/api.js";
1212

13-
console.log("!!!!!!!!!!!", ws.WebSocket);
14-
1513
const address = "https://127.0.0.1:3001";
1614

1715
const testConvexReactClient = () =>

‎src/react/use_paginated_query.test.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @vitest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44
/* eslint-disable @typescript-eslint/ban-types */
55
import { expect, vi, test, describe, beforeEach } from "vitest";
@@ -49,7 +49,7 @@ class ErrorBoundary extends React.Component<Props> {
4949
}
5050
}
5151

52-
test.skip.each([
52+
test.each([
5353
{
5454
options: undefined,
5555
expectedError:
@@ -396,7 +396,7 @@ describe.skip("usePaginatedQuery pages", () => {
396396
});
397397
});
398398

399-
describe.skip("PaginatedQueryArgs", () => {
399+
describe("PaginatedQueryArgs", () => {
400400
test("basic", () => {
401401
type MyQueryFunction = FunctionReference<
402402
"query",
@@ -410,7 +410,7 @@ describe.skip("PaginatedQueryArgs", () => {
410410
});
411411
});
412412

413-
describe.skip("PaginatedQueryItem", () => {
413+
describe("PaginatedQueryItem", () => {
414414
test("interface return type", () => {
415415
interface ReturnType {
416416
property: string;

‎src/react/use_queries.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @jest-environment jsdom
2+
* @vitest-environment custom-vitest-enviroment.ts
33
*/
44

55
import { RequestForQueries, useQueriesHelper } from "./use_queries.js";

‎vitest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export default defineConfig({
77
},
88
isolate: true,
99
watch: false,
10+
//environment: "node", // "node" is the default
1011
},
1112
});

0 commit comments

Comments
 (0)
Please sign in to comment.