Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Domi04151309 committed Jul 10, 2024
1 parent 8f77eca commit 8d812b3
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/modules/builder/builder.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { BUILD_OPTIONS, exists } from '../shared/test-utils.js';
import { describe, expect, it } from 'vitest';
import { Builder } from './builder.js';
import path from 'node:path';

describe('building', () => {
it('should empty the out directory', async () => {
const builder = new Builder(BUILD_OPTIONS);
await builder.emptyOutDirectory();

expect(await exists(BUILD_OPTIONS.outDirectory)).toBe(false);
});

it('should get folders and files', async () => {
const builder = new Builder(BUILD_OPTIONS);
const [folders, files] = await builder.getFoldersAndFiles();

expect(folders).toContain('src');
expect(files).toContain('src/index.js');
});

it('should create the folder structure', async () => {
const builder = new Builder(BUILD_OPTIONS);
await builder.createFolderStructure(['test']);

expect(
await exists(path.join(BUILD_OPTIONS.outDirectory, 'test'))
).toBe(true);
});

it('should copy files', async () => {
const builder = new Builder(BUILD_OPTIONS);
await builder.copyFiles(['./README.md', './src/index.js']);

expect(
await exists(path.join(BUILD_OPTIONS.outDirectory, 'README.md'))
).toBe(true);
expect(
await exists(path.join(BUILD_OPTIONS.outDirectory, 'src/index.js'))
).toBe(true);
});
});
17 changes: 17 additions & 0 deletions src/modules/bundler/bundler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest';
import { BUILD_OPTIONS } from '../shared/test-utils.js';
import { Bundler } from './bundler.js';

describe('bundling', () => {
it('should get entry points', async () => {
const bundler = new Bundler(BUILD_OPTIONS);
const entryPoints = await bundler.getEntryPoints(['./README.md']);

expect(entryPoints.length).toBe(0);
});

it('should bundle', async () => {
const bundler = new Bundler(BUILD_OPTIONS);
await bundler.bundle([]);
});
});
4 changes: 3 additions & 1 deletion src/modules/bundler/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ export function isLocal(filePath) {
* @returns {string}
*/
export function resolvePath(root, from, to) {
return to.startsWith('/') ? path.join(root, to) : path.relative(from, to);
if (to.startsWith('/')) return path.join(root, to);

return path.join(from.endsWith('/') ? from : path.dirname(from), to);
}
48 changes: 48 additions & 0 deletions src/modules/bundler/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from 'vitest';
import { isLocal, resolvePath } from './utils.js';

describe('local paths', () => {
it('should return true on absolute local path', () => {
expect(isLocal('/')).toBe(true);
});

it('should return true on relative local path', () => {
expect(isLocal('./')).toBe(true);
expect(isLocal('../')).toBe(true);
});

it('should return false on //', () => {
expect(isLocal('//')).toBe(false);
});

it('should return false on http://', () => {
expect(isLocal('http://')).toBe(false);
});

it('should return false on https://', () => {
expect(isLocal('https://')).toBe(false);
});

it('should return false on nonsense', () => {
expect(isLocal(0)).toBe(false);
});
});

describe('path resolving', () => {
const ROOT = 'root';

it('should resolve absolute path', () => {
expect(
resolvePath(ROOT, `${ROOT}/test1/test2`, '/test3')
).toBe(`${ROOT}/test3`);
});

it('should resolve relative path', () => {
expect(
resolvePath(ROOT, `${ROOT}/test1/test2`, './test3')
).toBe(`${ROOT}/test1/test3`);
expect(
resolvePath(ROOT, `${ROOT}/test1/test2/`, './test3')
).toBe(`${ROOT}/test1/test2/test3`);
});
});
19 changes: 19 additions & 0 deletions src/modules/cleaner/cleaner.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it, vi } from 'vitest';
import { readFile, unlink } from 'node:fs/promises';
import { BUILD_OPTIONS } from '../shared/test-utils.js';
import { Cleaner } from './cleaner.js';

vi.mock('node:fs/promises', () => ({
readFile: vi.fn(() => Promise.resolve(Buffer.from(''))),
unlink: vi.fn(() => Promise.resolve())
}));

describe('cleaning', () => {
it('should clean up', async () => {
const cleaner = new Cleaner(BUILD_OPTIONS);
await cleaner.cleanUp(['./README.md', './src/index.js']);

expect(readFile).toHaveBeenCalled();
expect(unlink).toHaveBeenCalled();
});
});
24 changes: 24 additions & 0 deletions src/modules/shared/test-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DEFAULT_BUILD_OPTIONS } from '../../index.js';
import { stat } from 'node:fs/promises';

/** @type {FullBuildOptions} */
export const BUILD_OPTIONS = {
...DEFAULT_BUILD_OPTIONS,
directories: ['src'],
ignoreList: ['.test.js'],
outDirectory: 'out',
rootDirectory: 'src'
};

/**
* @param {string} path
* @returns {Promise<boolean>}
*/
export async function exists(path) {
try {
await stat(path);
return true;
} catch {
return false;
}
}

0 comments on commit 8d812b3

Please sign in to comment.