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

Commit a2f1929

Browse files
committed
Use strong types
1 parent 3e63b53 commit a2f1929

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ Build the solution. This compiles the TypeScript files in the `src` folder into
4747
Continuous deployment is done with [CircleCI](https://circleci.com/), and configured in `config.yml`.
4848

4949
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.
50+
51+
## Casting to `any` in Tests
52+
53+
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`.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"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$\""
2020
},
2121
"devDependencies": {
22+
"@azure/functions": "1.0.3",
23+
"@fluffy-spoon/substitute": "1.88.0",
2224
"@types/jest": "24.0.13",
2325
"@types/node": "10.14.*",
2426
"@types/node-fetch": "2.3.5",

src/greet/index.test.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
import { Context } from "@azure/functions";
2+
import { HttpRequest } from "@azure/functions";
3+
import { Substitute } from "@fluffy-spoon/substitute";
14
import { run as greet } from "./index";
25

36
describe("greet function", () => {
47
test("returns correct greeting", async () => {
5-
// TODO: Add helpers for creating the request and the context.
6-
const request = {
7-
query: {
8-
name: "Jan Aagaard"
9-
}
10-
};
8+
const request = Substitute.for<HttpRequest>();
9+
(request.query.returns as any)({
10+
name: "Jan Aagaard"
11+
});
1112

12-
const context = {
13-
log: () => undefined,
13+
const context = Substitute.for<Context>();
14+
(context.req as any).returns({
1415
req: request
15-
};
16+
});
1617

17-
const response = await greet(context as any, request as any);
18+
const response = await greet(context, request);
1819

1920
expect(response.body).toBe("Hello Jan Aagaard");
2021
});

src/greet/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export async function run(context: any, req: any): Promise<any> {
1+
import { Context } from "@azure/functions";
2+
import { HttpRequest } from "@azure/functions";
3+
4+
export async function run(context: Context, req: HttpRequest): Promise<any> {
25
context.log("JavaScript HTTP trigger function processed a request.");
36

47
if (req.query.name || (req.body && req.body.name)) {

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# yarn lockfile v1
33

44

5+
"@azure/functions@1.0.3":
6+
version "1.0.3"
7+
resolved "https://registry.yarnpkg.com/@azure/functions/-/functions-1.0.3.tgz#1b51d58f188903f59f39bd093fe6f7b8d88deb11"
8+
integrity sha512-/D+sz6LgWT+A6RRW2zhwlwhKqqDSxL6HCF1Q1lN0iXolD2FfNFZpzrOxGyGYEEXp/5Dtjp12bcRTBhMH1cBi2Q==
9+
510
"@babel/code-frame@^7.0.0":
611
version "7.0.0"
712
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.0.0.tgz#06e2ab19bdb535385559aabb5ba59729482800f8"
@@ -139,6 +144,11 @@
139144
exec-sh "^0.3.2"
140145
minimist "^1.2.0"
141146

147+
"@fluffy-spoon/substitute@1.88.0":
148+
version "1.88.0"
149+
resolved "https://registry.yarnpkg.com/@fluffy-spoon/substitute/-/substitute-1.88.0.tgz#c6a4a4c7fd1898a49ec31778f8e5222da38bde93"
150+
integrity sha512-BJYWBilA38NoKCbDve9xoomLtUNOTw2siZkog0iwN5aLObZKfWnoH+GGkZ1ZLwMj2YqzA49NGsSb60gbaPlaWw==
151+
142152
"@jest/console@^24.7.1":
143153
version "24.7.1"
144154
resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.7.1.tgz#32a9e42535a97aedfe037e725bd67e954b459545"

0 commit comments

Comments
 (0)