Skip to content
Open
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
1 change: 0 additions & 1 deletion examples/typescript-node/Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ integration-test:
RUN docker run --rm -d -p 8080:8080 typescript-node-example:latest && npm run test:integration
END


all:
BUILD +build
BUILD +docker
Expand Down
83 changes: 67 additions & 16 deletions examples/typescript-node/integration/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
import axios from 'axios';
import {sayHello} from "../src/server";

describe('sayHello', () => {
const call = async (who?: string) => {
const url = who ? `http://localhost:8080/hello?who=${who}` : 'http://localhost:8080/hello';
const response = await axios.get(url);
expect(response.status).toBe(200);
return response.data;
}
import axios from "axios";
import app from "../src/index";
import { Server } from "http";
import * as net from "net";

describe("sayHello", () => {
let server: Server | undefined;

it('should say Hello Earthly if nothing is passed', async () => {
expect(await call()).toBe('Hello Earthly');
const isPort8080Open = (): Promise<boolean> => {
return new Promise((resolve) => {
const socket = new net.Socket();
const onError = () => {
socket.destroy();
resolve(false);
};
socket.setTimeout(1000);
socket.once("error", onError);
socket.once("timeout", onError);
socket.connect(8080, "localhost", () => {
socket.end();
resolve(true);
});
});
};

it('should say Hello World if World is passed', async () => {
expect(await call('World')).toBe('Hello World');
beforeAll(async () => {
const open = await isPort8080Open();
if (open) {
console.log("Server already running on port 8080, skipping local startup");
return;
}

return new Promise<void>((resolve, reject) => {
server = app.listen(8080, () => {
resolve();
});
server.on("error", (err: any) => {
if (err.code === "EADDRINUSE") {
server = undefined;
resolve();
} else {
reject(err);
}
});
});
});
});

export {};
afterAll((done) => {
if (server) {
server.close(done);
} else {
done();
}
});

const call = async (who?: string) => {
const url = who
? `http://localhost:8080/hello?who=${who}`
: "http://localhost:8080/hello";
const response = await axios.get(url);
expect(response.status).toBe(200);
return response.data;
};

it("should say Hello Earthly if nothing is passed", async () => {
expect(await call()).toBe("Hello Earthly");
});

it("should say Hello World if World is passed", async () => {
expect(await call("World")).toBe("Hello World");
});
});
7 changes: 4 additions & 3 deletions examples/typescript-node/jest.config.integration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as config from './jest.config';
import config from "./jest.config.ts";

export default {
...config.default,
rootDir: 'integration'
...config,
rootDir: ".",
testMatch: ["<rootDir>/integration/**/*.spec.ts"],
};
Loading
Loading