Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(Major tests refactor): All tests are now moved to respective packages #6

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
projects: ["<rootDir>/packages/*"],
collectCoverage: true
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "npm run bootstrap && lerna run build",
"serve": "lerna run serve",
"start": "lerna run start",
"test": "npm run build && jest",
"test": "npm run bootstrap && jest",
"coverage": "npm run build && jest --collect-coverage",
"clean": "lerna clean -y && rm -rf -f ./packages/**/dist && rm -f ./packages/**/tsconfig.tsbuildinfo"
},
Expand Down
7 changes: 5 additions & 2 deletions packages/tunnelify-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"serve": "tsc --watch",
"build": "tsc -p tsconfig.build.json",
"serve": "tsc --watch -p tsconfig.build.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/mikesposito/tunnelify.git"
Expand Down
11 changes: 11 additions & 0 deletions packages/tunnelify-cli/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.json",

"compilerOptions": {
"outDir": "./dist"
},

"include": [
"src/**/*"
]
}
18 changes: 1 addition & 17 deletions packages/tunnelify-cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
{
"compilerOptions": {
"module": "commonjs",
"composite": true,
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true
},
"extends": "../../tsconfig.json"
}
6 changes: 6 additions & 0 deletions packages/tunnelify-local-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
moduleNameMapper: {
"@mikesposito/(.+)": "<rootDir>../$1/src"
}
};
12 changes: 9 additions & 3 deletions packages/tunnelify-local-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
"bugs": {
"url": "https://github.com/mikesposito/tunnelify/issues"
},
"files": [
"dist"
],
"homepage": "https://github.com/mikesposito/tunnelify#readme",
"scripts": {
"build": "tsc",
"serve": "tsc --watch",
"build": "tsc -p tsconfig.build.json",
"serve": "tsc --watch -p tsconfig.build.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michele Esposito<michele@esposito.codes>",
Expand All @@ -27,6 +30,9 @@
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.6.1",
"typescript": "^4.3.5"
"typescript": "^4.3.5",
"jest": "^27.0.6",
"portastic": "^1.0.1",
"socket.io-client": "^4.1.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
164 changes: 164 additions & 0 deletions packages/tunnelify-local-server/tests/local-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { TunnelifyCli } from "@mikesposito/tunnelify-cli";
import { TunnelifyLocalServer, ITunnelifyLocalServer } from "../src";
import path from 'path';
import fs from 'fs';
import request from 'supertest';
import {pickPort} from "../../../tests/helpers/suite";

describe("@mikesposito/tunnelify-local-server", () => {
describe("constructor()", () => {
it("Should initialize without errors", () => {
try {
const cli = new TunnelifyCli({
src: path.resolve(__dirname, "./__mocks__/test-instance"),
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
expect(localServer).toBeDefined();
} catch(e) {
throw e;
}
});
});

describe("mount()", () => {
it("Should throw error if path does not exists", async () => {
return new Promise<void>((resolve, reject) => {
try {
const cli = new TunnelifyCli({
src: path.resolve(__dirname, "./__mocks__/test-instancexxxxx"),
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(path.resolve(__dirname, "./__mocks__/test-instancexxxxx"));
fail("Should not have arrived here.");
reject();
} catch(e) {
resolve();
}
})
})
it("Should mount correct directory without errors", async () => {
try {
const cli = new TunnelifyCli({
src: path.resolve(__dirname, "./__mocks__/test-instance"),
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(path.resolve(__dirname, "./__mocks__/test-instance"));
expect(localServer.mountPoint).toBeDefined();
expect(localServer.mountPoint).toBe(path.resolve(__dirname, "./__mocks__/test-instance"));
} catch(e) {
throw e;
}
});
});

describe("listen()", () => {
it("Should listen without errors", async () => {
return new Promise<void>(async (resolve, reject) => {
try {
const mountPath = path.resolve(__dirname, "./__mocks__/test-instance");
const mountPort = await pickPort(22222);
const cli = new TunnelifyCli({
src: mountPath,
flags: {
verbose: false,
silent: true
}
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(mountPath);
await localServer.listen(mountPort);
expect(localServer.connection).toBeDefined();
localServer.stop();
resolve();
} catch(e) {
reject(e);
}
})
});
it("Should serve file correctly", () => {
return new Promise<void>(async (resolve, reject) => {
try {
const mountPath = path.resolve(__dirname, "./__mocks__/test-instance");
const mountPort = await pickPort(22222);
const cli = new TunnelifyCli({
src: mountPath,
flags: {
verbose: false,
silent: true
}
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(mountPath);
await localServer.listen(mountPort);
request(`http://localhost:${mountPort}/`)
.get("file.txt")
.then(response => {
localServer.stop();
expect(response.status).toBe(200);
expect(response.text).toBe("Hello, World!");
resolve();
})
.catch(e => reject(e));
} catch(e) {
reject(e);
}
});
});
});

describe("handleRemoteFileRequest()", () => {
it("Should handle Remote File Request", () => {
return new Promise<void>(async (resolve, reject) => {
try {
const mountPath = path.resolve(__dirname, "./__mocks__/test-instance");
const mountPort = await pickPort(22222);
const cli = new TunnelifyCli({
src: mountPath,
flags: {
verbose: false,
silent: true
}
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(mountPath);
await localServer.listen(mountPort);
localServer.handleRemoteFileRequest("GET", "/file.txt", (response) => {
localServer.stop();
expect(response).toHaveProperty("file");
expect(response).toHaveProperty("type");
expect((response.file as Buffer).toString()).toBe(fs.readFileSync(mountPath + "/file.txt", { encoding: "utf-8" }));
resolve();
})
} catch(e) {
reject(e);
}
});
})
it("Should return 404 if file does not exists", () => {
return new Promise<void>(async (resolve, reject) => {
try {
const mountPath = path.resolve(__dirname, "./__mocks__/test-instance");
const mountPort = await pickPort(22222);
const cli = new TunnelifyCli({
src: mountPath,
flags: {
verbose: false,
silent: true
}
}, false);
const localServer: ITunnelifyLocalServer = new TunnelifyLocalServer(cli);
localServer.mount(mountPath);
await localServer.listen(mountPort);
localServer.handleRemoteFileRequest("GET", "/file1.txt", (response) => {
localServer.stop();
expect(response).toHaveProperty("error");
expect(response.error).toBe(404);
resolve();
})
} catch(e) {
reject(e);
}
});
})
});
});
11 changes: 11 additions & 0 deletions packages/tunnelify-local-server/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.build.json",

"compilerOptions": {
"outDir": "./dist"
},

"include": [
"src/**/*"
]
}
24 changes: 1 addition & 23 deletions packages/tunnelify-local-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
{
"compilerOptions": {
"module": "commonjs",
"composite": true,
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"paths": {
"@mikesposito/tunnelify-cli": ["../tunnelify-cli/src"],
}
},
"references": [
{ "path": "../tunnelify-cli" }
]
"extends": "../../tsconfig.json"
}
6 changes: 6 additions & 0 deletions packages/tunnelify-provider/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: 'ts-jest',
moduleNameMapper: {
"@mikesposito/(.+)": "<rootDir>../$1/src"
}
};
12 changes: 10 additions & 2 deletions packages/tunnelify-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
"types": "dist/index.d.ts",
"scripts": {
"start": "node dist/index.js",
"build": "tsc",
"serve": "tsc --watch",
"build": "tsc -p tsconfig.build.json",
"serve": "tsc --watch -p tsconfig.build.json",
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"tunnelify-provider": "./dist/cli.js"
},
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/mikesposito/tunnelify.git"
Expand All @@ -26,6 +29,11 @@
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^16.6.1",
"jest": "^27.0.6",
"portastic": "^1.0.1",
"socket.io-client": "^4.1.3",
"supertest": "^6.1.6",
"ts-jest": "^27.0.5",
"typescript": "^4.3.5"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let storage = {};
export let storage = {};

const client = {
end: () => {},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World!
Loading