Skip to content

Commit

Permalink
Implement port option and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
StarLederer committed Apr 1, 2022
1 parent 5dd7289 commit eb7c31e
Show file tree
Hide file tree
Showing 11 changed files with 439 additions and 779 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
overrides: [
// Typescript files
{
files: ['*.ts'],
files: ['src/**/*.ts', '__tests__/**/*.ts'],
parserOptions: {
project: './tsconfig.json',
},
Expand Down
28 changes: 10 additions & 18 deletions __tests__/src/abstraction/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
// import * as fs from 'fs/promises';
// import { resolve } from 'path';

// import { UserOptions } from 'src/plugin/Options';
// import FsServer from '../../../src/plugin/server';
import abstraction from '../../../src/abstraction';
// import { resolveOptions } from '../../../src/plugin/Options';

const testPort = 7070;

let lastFetch: { url: RequestInfo, init?: RequestInit };
global.fetch = jest.fn((url: RequestInfo, init?: RequestInit) => {
lastFetch = { url, init };
return { status: 200, text() { return ''; }, json() { return '{}'; } };
}) as jest.Mock;

// const userOptions: UserOptions = {
// rootDir: '__tests__/assets',
// goAboveRoot: false,
// };
// const options = resolveOptions(userOptions);
jest.mock('@vite-plugin-fs-runtime');

describe('abstraction', () => {
it('should build correct readdir queries', async () => {
await abstraction.readdir('');
expect(lastFetch).toEqual({ url: 'http://localhost:7070/?command=readdir' });
expect(lastFetch).toEqual({ url: `http://localhost:${testPort}/?command=readdir` });
await abstraction.readdir('', true);
expect(lastFetch).toEqual({ url: 'http://localhost:7070/?command=readdir&withFileTypes=true' });
expect(lastFetch).toEqual({ url: `http://localhost:${testPort}/?command=readdir&withFileTypes=true` });
});
it('should build correct readFile queries', async () => {
await abstraction.readFile('');
expect(lastFetch).toEqual({ url: 'http://localhost:7070/?command=readFile' });
expect(lastFetch).toEqual({ url: `http://localhost:${testPort}/?command=readFile` });
});
it('should build correct stat queries', async () => {
await abstraction.stat('');
expect(lastFetch).toEqual({ url: 'http://localhost:7070/?command=stat' });
expect(lastFetch).toEqual({ url: `http://localhost:${testPort}/?command=stat` });
});
it('should build correct writeFile queries', async () => {
await abstraction.writeFile('file', '');
expect(lastFetch).toEqual({
url: 'http://localhost:7070/file',
url: `http://localhost:${testPort}/file`,
init: {
method: 'POST',
headers: {
Expand All @@ -49,12 +41,12 @@ describe('abstraction', () => {
it('should build correct rm queries', async () => {
await abstraction.rm('');
expect(lastFetch).toEqual({
url: 'http://localhost:7070/',
url: `http://localhost:${testPort}/`,
init: { method: 'DELETE' },
});
await abstraction.rm('', { recursive: true });
expect(lastFetch).toEqual({
url: 'http://localhost:7070/?recursive=true',
url: `http://localhost:${testPort}/?recursive=true`,
init: { method: 'DELETE' },
});
});
Expand Down
5 changes: 2 additions & 3 deletions __tests__/src/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ let server: FsServer;

const userOptions: UserOptions = {
rootDir: '__tests__/assets',
goAboveRoot: false,
};

const options = resolveOptions(userOptions);
Expand All @@ -20,9 +19,9 @@ function resolveWithRoot(...args: string[]) {
return resolve(options.rootDir, ...args);
}

beforeAll(() => {
beforeAll(async () => {
server = new FsServer(resolveOptions(options));
server.start(true);
await server.start(true);
});

afterAll((done) => {
Expand Down
5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
export default {
preset: 'ts-jest',
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
transform: {
'^.+\\.ts?$': 'ts-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/'],
};
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,20 @@
"@typescript-eslint/parser": "^4.33.0",
"eslint": "7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-airbnb-typescript": "^14.0.1",
"eslint-plugin-import": "^2.25.2",
"eslint-config-airbnb-typescript": "^14.0.2",
"eslint-plugin-import": "^2.25.4",
"jest": "^27.5.1",
"node-fetch": "2",
"node-fetch": "^2.6.7",
"rimraf": "^3.0.2",
"rollup": "^2.58.3",
"supertest": "^6.2.2",
"rollup": "^2.70.1",
"ts-jest": "^27.1.4",
"tsup": "^5.5.0",
"typescript": "^4.4.4",
"vite": "^2.8.6"
"tsup": "^5.12.2",
"typescript": "^4.6.3",
"vite": "^2.9.1"
},
"dependencies": {
"@koa/cors": "^3.2.0",
"@koa/cors": "^3.3.0",
"get-port": "5",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa-router": "^10.1.1"
Expand Down
Loading

0 comments on commit eb7c31e

Please sign in to comment.