Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 9, 2024
1 parent 11dabaa commit 49e7059
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
43 changes: 10 additions & 33 deletions projects/cli/src/lib/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,23 @@
import { join } from 'node:path';
import { sortUserFile } from '@org/core';
import { sortCommandHandle } from './cli';
import { afterEach, beforeEach, type MockInstance, vi } from 'vitest';
import { vol } from 'memfs';
import { vi } from 'vitest';

vi.mock('fs', async () => {
const memfs: typeof import('memfs') = await vi.importActual('memfs');
return memfs.fs;
});
vi.mock('fs/promises', async () => {
const memfs: typeof import('memfs') = await vi.importActual('memfs');
return memfs.fs.promises;
vi.mock('@org/core', async () => {
const actual = await vi.importActual<typeof import('@org/core')>('@org/core');
return {
...actual,
sortUserFile: vi.fn(async () => {}),
};
});

describe('sortCommandHandle', () => {
const MEMFS_VOLUME = '/test';
let cwdSpy: MockInstance<[], string>;

beforeEach(() => {
cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue(MEMFS_VOLUME);
});

afterEach(() => {
cwdSpy.mockRestore();
});

it('should sort file of users', async () => {
const testPath = join(MEMFS_VOLUME, 'sort', 'users.json');
vol.fromJSON(
{
[testPath]: JSON.stringify([{ name: 'Michael' }, { name: 'Alice' }]),
},
MEMFS_VOLUME
);

const testPath = join('sort', 'users.json');
await expect(
sortCommandHandle({ filePath: testPath })
).resolves.not.toThrow();

const content = vol.readFileSync(testPath).toString();
expect(JSON.parse(content)).toEqual([
{ name: 'Alice' },
{ name: 'Michael' },
]);
expect(sortUserFile).toHaveBeenCalledWith(testPath);
});
});
12 changes: 12 additions & 0 deletions tooling/build-env/src/shared/setup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest';
import { getEnvironmentsRoot } from '@org/build-env';

describe('getEnvironmentsRoot', () => {
it('should return default env dir', () => {
expect(getEnvironmentsRoot()).toBe('tmp/environments');
});

it('should return given path', () => {
expect(getEnvironmentsRoot('env')).toBe('env');
});
});

0 comments on commit 49e7059

Please sign in to comment.