Skip to content

Commit 7cf08bf

Browse files
authored
test: convert setup scripts to TypeScript (#1405)
1 parent 295f74f commit 7cf08bf

File tree

6 files changed

+258
-385
lines changed

6 files changed

+258
-385
lines changed

jest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"transform": {
3-
"\\.(js|jsx|ts|tsx)$": "ts-jest"
3+
"\\.(ts|tsx)$": "ts-jest"
44
},
55
"testEnvironment": "jsdom",
66
"testEnvironmentOptions": {
@@ -13,9 +13,9 @@
1313
"snapshotSerializers": [
1414
"enzyme-to-json/serializer"
1515
],
16-
"globalSetup": "<rootDir>/tests/globalSetup.js",
16+
"globalSetup": "<rootDir>/tests/globalSetup.ts",
1717
"setupFilesAfterEnv": [
18-
"<rootDir>/tests/setup.js"
18+
"<rootDir>/tests/setup.ts"
1919
],
2020
"moduleFileExtensions": [
2121
"js",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@testing-library/user-event": "^14.2.0",
8989
"@types/classnames": "^2.2.11",
9090
"@types/enzyme": "^3.10.8",
91+
"@types/enzyme-adapter-react-16": "^1.0.6",
9192
"@types/fs-extra": "^9.0.7",
9293
"@types/getos": "^3.0.1",
9394
"@types/jest": "^29.5.0",
@@ -109,7 +110,7 @@
109110
"css-loader": "^6.7.1",
110111
"electron": "25.2.0",
111112
"enzyme": "^3.11.0",
112-
"enzyme-adapter-react-16": "^1.15.6",
113+
"enzyme-adapter-react-16": "^1.15.7",
113114
"enzyme-to-json": "^3.6.1",
114115
"eslint": "^8.45.0",
115116
"eslint-config-prettier": "^8.8.0",

tests/globalSetup.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/globalSetup.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { maybeFetchContributors } from '../tools/contributors';
2+
import { populateReleases } from '../tools/fetch-releases';
3+
4+
export default async function globalSetup() {
5+
await Promise.all([maybeFetchContributors(true), populateReleases()]);
6+
}

tests/setup.js renamed to tests/setup.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { configure: enzymeConfigure } = require('enzyme');
2-
const Adapter = require('enzyme-adapter-react-16');
3-
const { createSerializer } = require('enzyme-to-json');
4-
const { configure: mobxConfigure } = require('mobx');
1+
import { configure as enzymeConfigure } from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
3+
import { createSerializer } from 'enzyme-to-json';
4+
import { configure as mobxConfigure } from 'mobx';
55

6-
const { ElectronFiddleMock } = require('./mocks/mocks');
6+
import { ElectronFiddleMock } from './mocks/mocks';
77

88
enzymeConfigure({ adapter: new Adapter() });
99

@@ -33,20 +33,22 @@ jest.mock('@sentry/electron/renderer', () => ({
3333
}));
3434

3535
class FakeBroadcastChannel extends EventTarget {
36-
constructor(channelName) {
36+
public name: string;
37+
38+
constructor(channelName: string) {
3739
super();
3840
this.name = channelName;
3941
}
4042

41-
postMessage(message) {
43+
postMessage(message: unknown) {
4244
this.dispatchEvent(new MessageEvent('message', { data: message }));
4345
}
4446
}
4547

46-
global.BroadcastChannel = class Singleton {
48+
(global.BroadcastChannel as any) = class Singleton {
4749
static channels = new Map();
4850

49-
constructor(channelName) {
51+
constructor(channelName: string) {
5052
if (!Singleton.channels.has(channelName)) {
5153
Singleton.channels.set(
5254
channelName,
@@ -57,28 +59,28 @@ global.BroadcastChannel = class Singleton {
5759
}
5860
};
5961

60-
expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }));
62+
expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }) as any);
6163

6264
// We want to detect jest sometimes
63-
global.__JEST__ = global.__JEST__ || {};
65+
(global as any).__JEST__ = (global as any).__JEST__ || {};
6466

6567
// Setup for main tests
6668
global.window = global.window || {};
6769
global.document = global.document || { body: {} };
6870
global.fetch = window.fetch = jest.fn();
6971

70-
delete window.localStorage;
71-
window.localStorage = {};
72+
delete (window as any).localStorage;
73+
(window.localStorage as any) = {};
7274

7375
window.navigator = window.navigator ?? {};
74-
window.navigator.clipboard = {};
76+
(window.navigator.clipboard as any) = {};
7577

7678
/**
7779
* Mock these properties twice so that they're available
7880
* both at the top-level of files and also within the
7981
* code called in individual tests.
8082
*/
81-
window.ElectronFiddle = new ElectronFiddleMock();
83+
(window.ElectronFiddle as any) = new ElectronFiddleMock();
8284
window.localStorage.setItem = jest.fn();
8385
window.localStorage.getItem = jest.fn();
8486
window.localStorage.removeItem = jest.fn();
@@ -87,13 +89,13 @@ window.navigator.clipboard.readText = jest.fn();
8789
window.navigator.clipboard.writeText = jest.fn();
8890

8991
beforeEach(() => {
90-
process.env.JEST = true;
91-
process.env.TEST = true;
92+
(process.env.JEST as any) = true;
93+
(process.env.TEST as any) = true;
9294
document.body.innerHTML = '<div id="app" />';
9395

94-
window.ElectronFiddle = new ElectronFiddleMock();
95-
window.localStorage.setItem.mockReset();
96-
window.localStorage.getItem.mockReset();
97-
window.localStorage.removeItem.mockReset();
98-
window.open.mockReset();
96+
(window.ElectronFiddle as any) = new ElectronFiddleMock();
97+
(window.localStorage.setItem as jest.Mock).mockReset();
98+
(window.localStorage.getItem as jest.Mock).mockReset();
99+
(window.localStorage.removeItem as jest.Mock).mockReset();
100+
(window.open as jest.Mock).mockReset();
99101
});

0 commit comments

Comments
 (0)