Skip to content

Commit f119b87

Browse files
committed
test: improve config tests and suppress deprecation warnings
- Add comprehensive tests for gist config loading - Handle environment mocking properly - Suppress punycode deprecation warnings - Update GitHub Actions workflow
1 parent 75a8793 commit f119b87

File tree

3 files changed

+61
-40
lines changed

3 files changed

+61
-40
lines changed

.github/workflows/tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ jobs:
2727
run: pnpm install
2828

2929
- name: Run tests
30+
env:
31+
NODE_OPTIONS: '--no-deprecation'
3032
run: pnpm test:once

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dev": "vite",
77
"build": "tsc && vite build",
88
"preview": "vite preview",
9-
"test:once": "vitest run",
9+
"test:once": "NODE_OPTIONS='--no-deprecation' vitest run",
1010
"test": "vitest",
1111
"coverage": "vitest run --coverage",
1212
"prepare": "husky install",

src/test/ConfigContext.spec.tsx

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
1+
import { vi, describe, it, expect, beforeEach, afterAll } from 'vitest';
12
import { render, screen, waitFor } from '@testing-library/react';
3+
4+
// Create a mutable version of ImportMetaEnv for testing
5+
type MutableImportMetaEnv = {
6+
-readonly [K in keyof ImportMetaEnv]: ImportMetaEnv[K];
7+
};
8+
9+
// Mock the module before importing
10+
vi.mock('../ConfigContext', async () => {
11+
12+
// Create base mock environment
13+
const mockEnv: MutableImportMetaEnv = {
14+
MODE: 'test',
15+
DEV: true,
16+
PROD: false,
17+
BASE_URL: '/',
18+
VITE_CONFIG_GIST_URL: undefined
19+
};
20+
21+
// Create a proxy to handle property assignments
22+
const envProxy = new Proxy<MutableImportMetaEnv>(mockEnv, {
23+
set(target: MutableImportMetaEnv, prop: string, value: unknown) {
24+
target[prop] = value;
25+
return true;
26+
}
27+
});
28+
29+
// Mock import.meta
30+
vi.stubGlobal('import', {
31+
meta: {
32+
env: envProxy
33+
}
34+
});
35+
36+
const actual = await vi.importActual<typeof import('../ConfigContext')>('../ConfigContext');
37+
return actual;
38+
});
39+
40+
// Now import the components that use import.meta.env
241
import { ConfigProvider, useConfig } from '../ConfigContext';
3-
import { vi, describe, it, expect, beforeEach } from 'vitest';
442

543
// Mock component to test the useConfig hook
644
const TestComponent = () => {
@@ -17,27 +55,24 @@ const TestComponent = () => {
1755

1856
describe('ConfigContext', () => {
1957
const mockGistUrl = 'https://api.github.com/gists/123';
58+
beforeAll(() => {
59+
// Mock console methods to suppress expected logs
60+
vi.spyOn(console, 'log').mockImplementation(() => { /* no-op */ });
61+
vi.spyOn(console, 'error').mockImplementation(() => { /* no-op */ });
62+
});
63+
2064
beforeEach(() => {
21-
// Reset mocks and spies
22-
vi.restoreAllMocks();
65+
// Reset mocks before each test
2366
vi.resetModules();
67+
vi.clearAllMocks();
2468

25-
// Mock fetch to return error by default
69+
// Reset fetch mock
2670
global.fetch = vi.fn().mockRejectedValue(new Error('Fetch not mocked'));
27-
28-
// Mock Vite's import.meta.env
29-
vi.stubGlobal('import.meta', {
30-
env: {
31-
VITE_CONFIG_GIST_URL: undefined,
32-
MODE: 'test',
33-
DEV: true
34-
}
35-
});
3671
});
3772

38-
afterEach(() => {
73+
afterAll(() => {
3974
vi.unstubAllGlobals();
40-
vi.clearAllMocks();
75+
vi.restoreAllMocks(); // Restore console methods
4176
});
4277

4378
it('loads default config when no gist URL is provided', async () => {
@@ -54,12 +89,8 @@ describe('ConfigContext', () => {
5489
});
5590

5691
it('loads and transforms gist config successfully', async () => {
57-
// Update env mock with gist URL
58-
vi.stubGlobal('import.meta', {
59-
env: {
60-
VITE_CONFIG_GIST_URL: mockGistUrl
61-
}
62-
});
92+
// Update env through the proxy
93+
(import.meta.env as MutableImportMetaEnv).VITE_CONFIG_GIST_URL = mockGistUrl;
6394

6495
// Mock successful gist response
6596
const mockGistData = {
@@ -104,12 +135,8 @@ describe('ConfigContext', () => {
104135
});
105136

106137
it('falls back to default config on fetch error', async () => {
107-
// Update env mock with gist URL
108-
vi.stubGlobal('import.meta', {
109-
env: {
110-
VITE_CONFIG_GIST_URL: mockGistUrl
111-
}
112-
});
138+
// Update env through the proxy
139+
(import.meta.env as MutableImportMetaEnv).VITE_CONFIG_GIST_URL = mockGistUrl;
113140

114141
global.fetch = vi.fn().mockImplementation(() =>
115142
Promise.reject(new Error('Network error'))
@@ -128,12 +155,8 @@ describe('ConfigContext', () => {
128155
});
129156

130157
it('handles malformed gist data gracefully', async () => {
131-
// Update env mock with gist URL
132-
vi.stubGlobal('import.meta', {
133-
env: {
134-
VITE_CONFIG_GIST_URL: mockGistUrl
135-
}
136-
});
158+
// Update env through the proxy
159+
(import.meta.env as MutableImportMetaEnv).VITE_CONFIG_GIST_URL = mockGistUrl;
137160

138161
global.fetch = vi.fn().mockImplementation(() =>
139162
Promise.resolve({
@@ -155,12 +178,8 @@ describe('ConfigContext', () => {
155178
});
156179

157180
it('transforms partial gist data correctly', async () => {
158-
// Update env mock with gist URL
159-
vi.stubGlobal('import.meta', {
160-
env: {
161-
VITE_CONFIG_GIST_URL: mockGistUrl
162-
}
163-
});
181+
// Update env through the proxy
182+
(import.meta.env as MutableImportMetaEnv).VITE_CONFIG_GIST_URL = mockGistUrl;
164183

165184
// Mock partial data with missing fields
166185
const mockPartialData = {

0 commit comments

Comments
 (0)