Skip to content

Commit 3995354

Browse files
authored
Upgrade to Jest 29 (#2068)
* Testing jest upgrade, using node environment export resolution with jsdom for jest 29 to tackle import errors, committing as is to test in CI * fix types * Fix types * fix(typesense): Get client type from created client to avoid double-installation conflict, yarn fix
1 parent 28baa9a commit 3995354

12 files changed

Lines changed: 1589 additions & 1505 deletions

functions/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
22
module.exports = {
33
preset: "ts-jest",
44
testEnvironment: "node",

functions/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
},
3737
"devDependencies": {
3838
"@types/fluent-ffmpeg": "^2.1.27",
39-
"@types/jest": "^27.4.0",
39+
"@types/jest": "^29.5.14",
4040
"@types/jsdom": "^21.1.7",
4141
"@types/luxon": "^2.0.9",
4242
"@types/object-hash": "^2.2.1",
4343
"copyfiles": "^2.4.1",
4444
"firebase-functions-test": "^0.3.3",
4545
"firebase-tools": "^13.18.0",
46-
"jest": "^27.5.1",
46+
"jest": "^29.7.0",
4747
"rimraf": "^3.0.2",
48-
"ts-jest": "^27.1.3",
48+
"ts-jest": "^29.2.5",
4949
"typescript": "4.5.5"
5050
},
5151
"private": true

functions/yarn.lock

Lines changed: 786 additions & 800 deletions
Large diffs are not rendered by default.

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const createJestConfig = nextJest({
1010
const config = {
1111
coverageProvider: "v8",
1212
testEnvironment: "jsdom",
13+
testEnvironmentOptions: {
14+
customExportConditions: ["node", "node-addons"]
15+
},
1316
moduleNameMapper: {
1417
"^components/(.*)$": "<rootDir>/components/$1"
1518
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
"@tsconfig/node16": "^1.0.2",
157157
"@types/diff": "^5.0.2",
158158
"@types/dompurify": "^2.3.3",
159-
"@types/jest": "^27.4.0",
159+
"@types/jest": "^29.5.14",
160160
"@types/js-yaml": "^4.0.5",
161161
"@types/lodash": "^4.14.178",
162162
"@types/luxon": "^2.0.9",
@@ -184,8 +184,8 @@
184184
"firebase-tools": "^13.18.0",
185185
"ini": "^1.3.5",
186186
"inquirer": "^6.5.1",
187-
"jest": "^27.5.1",
188-
"jest-environment-jsdom": "^27.5.1",
187+
"jest": "^29.7.0",
188+
"jest-environment-jsdom": "^29.7.0",
189189
"jest-summary-reporter": "^0.0.2",
190190
"js-yaml": "^4.1.0",
191191
"lorem-ipsum": "^2.0.4",

scripts/generate-stories.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const argv = yargs(hideBin(process.argv))
6363
default: paths.defaultStoriesConfig,
6464
describe:
6565
"Path to a multi-document yaml file with story configs. Each config is a map with name, folder, grouping, and figmaUrl fields."
66-
}).argv
66+
})
67+
.parseSync()
6768

6869
generateStories(argv.template, argv.stories)

scripts/typesense-admin.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { execSync } from "child_process"
22
import repl from "repl"
3-
import { Client } from "typesense"
4-
import yargs from "yargs"
3+
import yargs, { Arguments } from "yargs"
54
import { hideBin } from "yargs/helpers"
65
import { createClient } from "../functions/src/search/client"
76

87
declare global {
9-
var client: Client
8+
var client: ReturnType<typeof createClient>
109
}
1110

1211
const envs: Record<string, { url: string; key?: string; alias?: string }> = {
@@ -21,13 +20,13 @@ const envs: Record<string, { url: string; key?: string; alias?: string }> = {
2120
}
2221
}
2322

24-
type Args = { url?: string; key?: string; env?: string }
23+
type Args = Arguments<{ url?: string; key?: string; env?: string }>
2524
yargs(hideBin(process.argv))
2625
.scriptName("typesense-admin")
2726
.command(
2827
"console",
2928
"start a node repl with an initialized client",
30-
() => {},
29+
{},
3130
(args: Args) => {
3231
globalThis.client = resolveClient(args)
3332
repl.start({}).setupHistory("typesense-admin.history", () => {})
@@ -36,7 +35,7 @@ yargs(hideBin(process.argv))
3635
.command(
3736
"create-search-key",
3837
"create a new search key",
39-
() => {},
38+
{},
4039
async (args: Args) => {
4140
const client = resolveClient(args)
4241
const key = await client.keys().create({
@@ -47,20 +46,15 @@ yargs(hideBin(process.argv))
4746
console.log("Created", key.value)
4847
}
4948
)
50-
.command(
51-
"list-keys",
52-
"list keys",
53-
() => {},
54-
async (args: Args) => {
55-
const client = resolveClient(args)
56-
console.log(await client.keys().retrieve())
57-
}
58-
)
49+
.command("list-keys", "list keys", {}, async (args: Args) => {
50+
const client = resolveClient(args)
51+
console.log(await client.keys().retrieve())
52+
})
5953
.command(
6054
"delete-key <id>",
6155
"list keys",
62-
() => {},
63-
async (args: Args & { id: string }) => {
56+
{},
57+
async (args: Args & { id?: string }) => {
6458
const client = resolveClient(args)
6559
console.log(await client.keys(Number(args.id)).delete())
6660
}

tests/integrationEnvironment.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
1-
import { Config } from "@jest/types"
1+
import type {
2+
JestEnvironmentConfig,
3+
EnvironmentContext
4+
} from "@jest/environment"
25
import BrowserEnvironment from "jest-environment-jsdom"
36
import timers from "timers"
47

58
class IntegrationEnvironment extends BrowserEnvironment {
6-
constructor(config: Config.ProjectConfig) {
7-
super(
8-
Object.assign({}, config, {
9-
globals: Object.assign({}, config.globals, {
10-
// https://github.com/firebase/firebase-js-sdk/issues/3096#issuecomment-637584185
11-
Uint32Array: Uint32Array,
12-
Uint8Array: Uint8Array,
13-
ArrayBuffer: ArrayBuffer,
9+
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
10+
super(config, context)
11+
}
1412

15-
// These are required to run the admin sdk in a jsdom environment
16-
setImmediate: timers.setImmediate,
17-
setTimeout: timers.setTimeout,
18-
setInterval: timers.setInterval,
19-
clearImmediate: timers.clearImmediate,
20-
clearTimeout: timers.clearTimeout,
21-
clearInterval: timers.clearInterval,
13+
async setup() {
14+
await super.setup()
2215

23-
/** jsdom's Blob implementation does not work with firebase/storage.
24-
* firebase/storage *does* work with a fallback if Blob is not
25-
* available, so removing the global is a hack to get storage tests
26-
* working. We'll need a better solution when tests need to use Blobs.
27-
* */
28-
Blob: undefined
29-
})
30-
})
31-
)
32-
}
16+
// https://github.com/firebase/firebase-js-sdk/issues/3096#issuecomment-637584185
17+
this.global.Uint32Array = Uint32Array
18+
this.global.Uint8Array = Uint8Array
19+
this.global.ArrayBuffer = ArrayBuffer
3320

34-
async setup() {}
21+
// These are required to run the admin sdk in a jsdom environment
22+
this.global.setImmediate =
23+
timers.setImmediate as typeof globalThis.setImmediate
24+
this.global.setTimeout = timers.setTimeout as typeof globalThis.setTimeout
25+
this.global.setInterval =
26+
timers.setInterval as typeof globalThis.setInterval
27+
this.global.clearImmediate = timers.clearImmediate
28+
this.global.clearTimeout =
29+
timers.clearTimeout as typeof globalThis.clearTimeout
30+
this.global.clearInterval =
31+
timers.clearInterval as typeof globalThis.clearInterval
3532

36-
async teardown() {}
33+
/** jsdom's Blob implementation does not work with firebase/storage.
34+
* firebase/storage *does* work with a fallback if Blob is not
35+
* available, so removing the global is a hack to get storage tests
36+
* working. We'll need a better solution when tests need to use Blobs.
37+
* */
38+
;(this.global as any).Blob = undefined
39+
}
40+
41+
async teardown() {
42+
await super.teardown()
43+
}
3744
}
3845

3946
export default IntegrationEnvironment

tests/jest.integration.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ runAgainstEmulators()
77
const config: Config.InitialOptions = {
88
clearMocks: true,
99
testEnvironment: "./tests/integrationEnvironment.ts",
10+
testEnvironmentOptions: {
11+
customExportConditions: ["node", "node-addons"]
12+
},
1013
rootDir: "..",
1114
testPathIgnorePatterns: [
1215
"/node_modules/",

tests/jest.seed.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ runAgainstEmulators()
77
const config: Config.InitialOptions = {
88
clearMocks: true,
99
testEnvironment: "./tests/integrationEnvironment.ts",
10+
testEnvironmentOptions: {
11+
customExportConditions: ["node", "node-addons"]
12+
},
1013
rootDir: "..",
1114
roots: ["tests/seed"]
1215
}

0 commit comments

Comments
 (0)