Skip to content
This repository has been archived by the owner on Mar 5, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Use strong types
  • Loading branch information
janaagaard75 committed Jun 29, 2019
1 parent 3e63b53 commit a2f1929
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -47,3 +47,7 @@ Build the solution. This compiles the TypeScript files in the `src` folder into
Continuous deployment is done with [CircleCI](https://circleci.com/), and configured in `config.yml`.

The run scripts `test-save-results` and `test-e2e-save-results` run the same tests as their counterparts without the `-save-results` postfix, but stores the result in a `test-results` folder so that CircleCI can display a summary.

## Casting to `any` in Tests

The mock library Substitute has a known issue in that it [doesn't work in strict null checking mode](https://github.com/ffMathy/FluffySpoon.JavaScript.Testing.Faking#strict-mode). In order to avoid the compiler error the mocked interfaces are cast to `any` when calling the `returns` method. This makes the code when working with mocked interfaces a bit ugly, but I think this is a better solution than turning off `strickNullChecks`.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -19,6 +19,8 @@
"test-e2e-save-results": "cross-env JEST_JUNIT_OUTPUT=test-results/test-results-e2e.xml jest --ci --runInBand --reporters=default --reporters=jest-junit --testRegex \"\\.e2e-test\\.ts$\""
},
"devDependencies": {
"@azure/functions": "1.0.3",
"@fluffy-spoon/substitute": "1.88.0",
"@types/jest": "24.0.13",
"@types/node": "10.14.*",
"@types/node-fetch": "2.3.5",
Expand Down
21 changes: 11 additions & 10 deletions src/greet/index.test.ts
@@ -1,20 +1,21 @@
import { Context } from "@azure/functions";
import { HttpRequest } from "@azure/functions";
import { Substitute } from "@fluffy-spoon/substitute";
import { run as greet } from "./index";

describe("greet function", () => {
test("returns correct greeting", async () => {
// TODO: Add helpers for creating the request and the context.
const request = {
query: {
name: "Jan Aagaard"
}
};
const request = Substitute.for<HttpRequest>();
(request.query.returns as any)({
name: "Jan Aagaard"
});

const context = {
log: () => undefined,
const context = Substitute.for<Context>();
(context.req as any).returns({
req: request
};
});

const response = await greet(context as any, request as any);
const response = await greet(context, request);

expect(response.body).toBe("Hello Jan Aagaard");
});
Expand Down
5 changes: 4 additions & 1 deletion src/greet/index.ts
@@ -1,4 +1,7 @@
export async function run(context: any, req: any): Promise<any> {
import { Context } from "@azure/functions";
import { HttpRequest } from "@azure/functions";

export async function run(context: Context, req: HttpRequest): Promise<any> {
context.log("JavaScript HTTP trigger function processed a request.");

if (req.query.name || (req.body && req.body.name)) {
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -2,6 +2,11 @@
# yarn lockfile v1


"@azure/functions@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@azure/functions/-/functions-1.0.3.tgz#1b51d58f188903f59f39bd093fe6f7b8d88deb11"
integrity sha512-/D+sz6LgWT+A6RRW2zhwlwhKqqDSxL6HCF1Q1lN0iXolD2FfNFZpzrOxGyGYEEXp/5Dtjp12bcRTBhMH1cBi2Q==

"@babel/code-frame@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
Expand Down Expand Up @@ -139,6 +144,11 @@
exec-sh "^0.3.2"
minimist "^1.2.0"

"@fluffy-spoon/substitute@1.88.0":
version "1.88.0"
resolved "https://registry.yarnpkg.com/@fluffy-spoon/substitute/-/substitute-1.88.0.tgz#c6a4a4c7fd1898a49ec31778f8e5222da38bde93"
integrity sha512-BJYWBilA38NoKCbDve9xoomLtUNOTw2siZkog0iwN5aLObZKfWnoH+GGkZ1ZLwMj2YqzA49NGsSb60gbaPlaWw==

"@jest/console@^24.7.1":
version "24.7.1"
resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.7.1.tgz#32a9e42535a97aedfe037e725bd67e954b459545"
Expand Down

0 comments on commit a2f1929

Please sign in to comment.