Skip to content

Commit

Permalink
fix: path.test
Browse files Browse the repository at this point in the history
  • Loading branch information
icai committed Nov 26, 2024
1 parent 705838e commit 4c54120
Showing 1 changed file with 26 additions and 36 deletions.
62 changes: 26 additions & 36 deletions test/path.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,44 @@
import { describe, it, expect, vi } from 'vitest';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import path from 'path';

describe('File path creation with path.join()', () => {
// test windows with data/config.ts
it('should correctly join the current working directory and the relative path, windows with data/config.ts', () => {
// Mock process.cwd() to simulate a directory
vi.stubGlobal('process', {
cwd: vi.fn(() => 'C:\\Users\\YourName\\your-project'),
});
beforeEach(() => {
vi.restoreAllMocks(); // Ensure no stale mocks interfere
});

const filePath = path.join(process.cwd(), 'data/config.ts');
const isWindows = process.platform === 'win32'; // Determine if the OS is Windows

// Check that the path is correctly joined
expect(filePath).toBe('C:\\Users\\YourName\\your-project\\data\\config.ts');
})
// test windows with /data/config.ts
it('should correctly join the current working directory and the relative path, windows with /data/config.ts', () => {
// Mock process.cwd() to simulate a directory
vi.stubGlobal('process', {
cwd: vi.fn(() => 'C:\\Users\\YourName\\your-project'),
});
// Test with relative paths
it('should correctly join the current working directory and the relative path (data/config.ts)', () => {
const cwd = isWindows
? 'C:\\Users\\YourName\\your-project'
: '/Users/YourName/your-project';
const expectedPath = isWindows
? 'C:\\Users\\YourName\\your-project\\data\\config.ts'
: '/Users/YourName/your-project/data/config.ts';

const filePath = path.join(process.cwd(), '/data/config.ts');
// Check that the path is correctly joined
expect(filePath).toBe('C:\\Users\\YourName\\your-project\\data\\config.ts');
})

// test unix with data/config.ts
it('should correctly join the current working directory and the relative path, unix with data/config.ts', () => {
// Mock process.cwd() to simulate a directory
vi.stubGlobal('process', {
cwd: vi.fn(() => '/Users/YourName/your-project'),
cwd: vi.fn(() => cwd),
});

const filePath = path.join(process.cwd(), 'data/config.ts');

// Check that the path is correctly joined
expect(filePath).toBe('/Users/YourName/your-project/data/config.ts');
expect(filePath).toBe(expectedPath);
});

// test unix with /data/config.ts
it('should correctly join the current working directory and the relative path, unix with /data/config.ts', () => {
// Mock process.cwd() to simulate a directory
// Test with absolute-like paths
it('should correctly join the current working directory and the absolute path (/data/config.ts)', () => {
const cwd = isWindows
? 'C:\\Users\\YourName\\your-project'
: '/Users/YourName/your-project';
const expectedPath = isWindows
? 'C:\\Users\\YourName\\your-project\\data\\config.ts'
: '/Users/YourName/your-project/data/config.ts';

vi.stubGlobal('process', {
cwd: vi.fn(() => '/Users/YourName/your-project'),
cwd: vi.fn(() => cwd),
});

const filePath = path.join(process.cwd(), '/data/config.ts');

// Check that the path is correctly joined
expect(filePath).toBe('/Users/YourName/your-project/data/config.ts');
expect(filePath).toBe(expectedPath);
});
});

0 comments on commit 4c54120

Please sign in to comment.