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

Add basic server smoke test #91

Merged
merged 1 commit into from
Oct 4, 2023
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
132 changes: 132 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"devDependencies": {
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@rehooks/component-size": "^1.0.2",
"@serialport/bindings-cpp": "^12.0.0",
"@types/cors": "^2.8.4",
"@types/express": "^4.16.1",
"@types/jest": "^29.0.0",
"@types/node": "^14.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/supertest": "^2.0.12",
"@types/w3c-web-serial": "^1.0.2",
"@types/ws": "8.0.0 - 8.5",
"@types/yargs": "^17.0.0",
Expand All @@ -60,6 +62,7 @@
"react-dom": "^18.0.0",
"rimraf": "^5.0.0",
"semver": "^7.5.2",
"supertest": "^6.3.3",
"ts-jest": "^29.0.0",
"typescript": "~5.0 || ~5.2.0"
},
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/server-smoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { startServer, waitForEbb } from '../server';
import type { Server } from 'http';
import request from 'supertest';

jest.mock("../serialport-serialport")
jest.mock("../ebb")
jest.mock("../server", () => {
const original = jest.requireActual("../server");
return {
...original,
waitForEbb: jest.fn(()=> 'fake-ebb-path'),
};
});

describe('Server Smoke Test', () => {
let server: Server;

beforeAll(async () => {
server = await startServer(0);
});

afterAll( () => {
server.close();
});

test('POST /cancel should return 200 OK', async () => {
const response = await request(server).post('/cancel');
expect(response.status).toBe(200);
});

});
11 changes: 6 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { EBB } from "./ebb";
import { Device, PenMotion, Motion, Plan } from "./planning";
import { formatDuration } from "./util";
import { autoDetect } from '@serialport/bindings-cpp';
import * as self from './server' // for mocking
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a link to documentation for this? Its a new pattern to me, but otherwise this looks good!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its just a little weird to me, like wouldn't you get infinite recursive imports?

Copy link
Owner Author

@alexrudd2 alexrudd2 Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting the function was the only way I found that allowed jest to mock it. It doesn't appear to recurse (presumably after the first export jest "grabs" it and stops the recursion?)

I have no idea if it's a good pattern or not; it was trial-and-error.


export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb") {
export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb"): Promise<http.Server> {
const app = express();

app.use("/", express.static(path.join(__dirname, "..", "ui")));
Expand Down Expand Up @@ -219,7 +220,7 @@ export function startServer(port: number, device: string | null = null, enableCo
await plotter.postPlot();
}

return new Promise((resolve) => {
return new Promise<http.Server>((resolve) => {
server.listen(port, () => {
async function connect() {
for await (const d of ebbs(device)) {
Expand Down Expand Up @@ -256,8 +257,8 @@ async function listEBBs() {
return ports.filter(isEBB).map((p: { path: any; }) => p.path);
}

async function waitForEbb() {
// eslint-disable-next-line no-constant-condition
export async function waitForEbb() {
// eslint-disable-next-line no-constant-condition
while (true) {
const ebbs = await listEBBs();
if (ebbs.length) {
Expand All @@ -270,7 +271,7 @@ async function waitForEbb() {
async function* ebbs(path?: string) {
while (true) {
try {
const com = path || (await waitForEbb());
const com = path || (await self.waitForEbb()); // use self-import for mocking
console.log(`Found EBB at ${com}`);
const port = await tryOpen(com);
const closed = new Promise((resolve) => {
Expand Down