Skip to content

Commit 4ecd09c

Browse files
authored
test(*): Introduce Jest testing
Introduce Jest testing into the project. Added tests to firestore-send-email
1 parent 3bbb18b commit 4ecd09c

File tree

40 files changed

+4009
-830
lines changed

40 files changed

+4009
-830
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ mods-test-data/key.json
99
build
1010
*.iml
1111
local.properties
12+
coverage
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`extension functions configuration detected from environment variables 1`] = `
4+
Object {
5+
"location": "europe-west2",
6+
"mailchimpApiKey": "123456-789",
7+
"mailchimpAudienceId": "123456789",
8+
"mailchimpContactStatus": "pending",
9+
}
10+
`;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
2+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation();
3+
4+
import functionsConfig from "../functions/src/config";
5+
import { obfuscatedConfig } from "../functions/src/logs";
6+
import * as exportedFunctions from "../functions/src";
7+
8+
describe("extension", () => {
9+
beforeEach(() => {});
10+
11+
test("functions configuration detected from environment variables", async () => {
12+
expect(functionsConfig).toMatchSnapshot();
13+
});
14+
15+
test("functions configuration is logged on initialize", async () => {
16+
expect(consoleLogSpy).toBeCalledWith(
17+
"Initializing extension with configuration",
18+
obfuscatedConfig
19+
);
20+
});
21+
22+
test("functions are exported", async () => {
23+
expect(exportedFunctions.addUserToList).toBeInstanceOf(Function);
24+
expect(exportedFunctions.removeUserFromList).toBeInstanceOf(Function);
25+
});
26+
});

auth-mailchimp-sync/functions/lib/logs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
Object.defineProperty(exports, "__esModule", { value: true });
1818
const config_1 = require("./config");
19-
const obfuscatedConfig = Object.assign({}, config_1.default, { mailchimpApiKey: "********" });
19+
exports.obfuscatedConfig = Object.assign({}, config_1.default, { mailchimpApiKey: "<omitted>" });
2020
exports.complete = () => {
2121
console.log("Completed execution of extension");
2222
};
@@ -27,7 +27,7 @@ exports.errorRemoveUser = (err) => {
2727
console.error("Error when removing user from Mailchimp audience", err);
2828
};
2929
exports.init = () => {
30-
console.log("Initializing extension with configuration", obfuscatedConfig);
30+
console.log("Initializing extension with configuration", exports.obfuscatedConfig);
3131
};
3232
exports.initError = (err) => {
3333
console.error("Error when initializing extension", err);
@@ -36,7 +36,7 @@ exports.mailchimpNotInitialized = () => {
3636
console.error("Mailchimp was not initialized correctly, check for errors in the logs");
3737
};
3838
exports.start = () => {
39-
console.log("Started execution of extension with configuration", obfuscatedConfig);
39+
console.log("Started execution of extension with configuration", exports.obfuscatedConfig);
4040
};
4141
exports.userAdded = (userId, audienceId, mailchimpId, status) => {
4242
console.log(`Added user: ${userId} with status '${status}' to Mailchimp audience: ${audienceId} with Mailchimp ID: ${mailchimpId}`);

auth-mailchimp-sync/functions/src/logs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import config from "./config";
1818

19-
const obfuscatedConfig = {
19+
export const obfuscatedConfig = {
2020
...config,
21-
mailchimpApiKey: "********",
21+
mailchimpApiKey: "<omitted>",
2222
};
2323

2424
export const complete = () => {

auth-mailchimp-sync/jest.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const packageJson = require("./package.json");
2+
3+
module.exports = {
4+
name: packageJson.name,
5+
displayName: packageJson.name,
6+
rootDir: "./",
7+
preset: "ts-jest",
8+
globalSetup: "./jest.setup.js",
9+
globalTeardown: "./jest.teardown.js"
10+
};
11+
12+

auth-mailchimp-sync/jest.setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = async function() {
2+
process.env = Object.assign(process.env, {
3+
LOCATION: "europe-west2",
4+
MAILCHIMP_API_KEY: "123456-789",
5+
MAILCHIMP_AUDIENCE_ID: "123456789",
6+
MAILCHIMP_CONTACT_STATUS: "pending",
7+
});
8+
};

auth-mailchimp-sync/jest.teardown.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = async function() {
2+
delete process.env.LOCATION;
3+
delete process.env.MAILCHIMP_API_KEY;
4+
delete process.env.MAILCHIMP_AUDIENCE_ID;
5+
delete process.env.MAILCHIMP_CONTACT_STATUS;
6+
};

auth-mailchimp-sync/tsconfig.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
{
2+
"extends": "../tsconfig.json",
23
"compilerOptions": {
3-
"lib": ["es6"],
4-
"module": "commonjs",
5-
"noImplicitReturns": true,
6-
"outDir": "functions/lib",
7-
"sourceMap": false,
8-
"target": "es6"
4+
"outDir": "functions/lib"
95
},
10-
"compileOnSave": true,
11-
"include": ["functions/src"]
6+
"include": [
7+
"functions/src"
8+
]
129
}

delete-user-data/jest.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const packageJson = require('./package.json');
2+
3+
module.exports = {
4+
name: packageJson.name,
5+
displayName: packageJson.name,
6+
rootDir: './',
7+
preset: 'ts-jest',
8+
};

0 commit comments

Comments
 (0)