Skip to content

Commit 0bfe7c6

Browse files
authored
ci: enable eslint for tests (GoogleCloudPlatform#275)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent 4d8eb24 commit 0bfe7c6

File tree

6 files changed

+53
-58
lines changed

6 files changed

+53
-58
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
**/node_modules
22
build/
3-
test/

test/conformance/function.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const fs = require("fs");
2-
const fileName = "function_output.json";
1+
const fs = require('fs');
2+
const fileName = 'function_output.json';
33

44
function writeHttp(req, res) {
55
writeJson(req.body);
66
res.end(200);
77
}
88

99
function writeCloudEvent(cloudevent) {
10-
cloudevent.datacontenttype = "application/json"
10+
cloudevent.datacontenttype = 'application/json';
1111
writeJson(cloudevent);
1212
}
1313

1414
function writeLegacyEvent(data, context) {
15-
content = {
15+
const content = {
1616
data: data,
1717
context: {
1818
eventId: context.eventId,
@@ -25,7 +25,7 @@ function writeLegacyEvent(data, context) {
2525
}
2626

2727
function writeJson(content) {
28-
json = JSON.stringify(content);
28+
const json = JSON.stringify(content);
2929
fs.writeFileSync(fileName, json);
3030
}
3131

test/data/with_main/foo.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
/*eslint no-unused-vars: "off"*/
12
/**
23
* Test HTTP function to test function loading.
34
*
45
* @param {!Object} req request context.
56
* @param {!Object} res response context.
67
*/
7-
function testFunction (req, res) {
8-
return 'PASS'
9-
};
8+
function testFunction(req, res) {
9+
return 'PASS';
10+
}
1011

1112
module.exports = {
1213
testFunction,
13-
}
14+
};

test/data/without_main/function.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
/*eslint no-unused-vars: "off"*/
12
/**
23
* Test HTTP function to test function loading.
34
*
45
* @param {!Object} req request context.
56
* @param {!Object} res response context.
67
*/
7-
function testFunction (req, res) {
8-
return 'PASS'
9-
};
8+
function testFunction(req, res) {
9+
return 'PASS';
10+
}
1011

1112
module.exports = {
1213
testFunction,
13-
}
14+
};

test/invoker.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('request to HTTP function', () => {
6161

6262
testData.forEach(test => {
6363
it(`should return transformed body: ${test.name}`, async () => {
64-
var callCount = 0;
64+
let callCount = 0;
6565
const server = getServer(
6666
(req: express.Request, res: express.Response) => {
6767
++callCount;
@@ -71,7 +71,7 @@ describe('request to HTTP function', () => {
7171
);
7272
await supertest(server)
7373
.post(test.path)
74-
.send({ text: 'hello' })
74+
.send({text: 'hello'})
7575
.set('Content-Type', 'application/json')
7676
.expect(test.text)
7777
.expect(test.status);
@@ -96,7 +96,7 @@ describe('GCF event request to event function', () => {
9696
eventType: 'testEventType',
9797
resource: 'testResource',
9898
},
99-
data: { some: 'payload' },
99+
data: {some: 'payload'},
100100
},
101101
expectedResource: 'testResource',
102102
},
@@ -107,7 +107,7 @@ describe('GCF event request to event function', () => {
107107
timestamp: 'testTimestamp',
108108
eventType: 'testEventType',
109109
resource: 'testResource',
110-
data: { some: 'payload' },
110+
data: {some: 'payload'},
111111
},
112112
expectedResource: 'testResource',
113113
},
@@ -124,7 +124,7 @@ describe('GCF event request to event function', () => {
124124
type: 'testType',
125125
},
126126
},
127-
data: { some: 'payload' },
127+
data: {some: 'payload'},
128128
},
129129
expectedResource: {
130130
service: 'testService',
@@ -137,19 +137,16 @@ describe('GCF event request to event function', () => {
137137
it(`should receive data and context from ${test.name}`, async () => {
138138
let receivedData: {} | null = null;
139139
let receivedContext: functions.CloudFunctionsContext | null = null;
140-
const server = getServer(
141-
(data: {}, context: functions.Context) => {
142-
receivedData = data;
143-
receivedContext = context as functions.CloudFunctionsContext;
144-
},
145-
SignatureType.EVENT
146-
);
140+
const server = getServer((data: {}, context: functions.Context) => {
141+
receivedData = data;
142+
receivedContext = context as functions.CloudFunctionsContext;
143+
}, SignatureType.EVENT);
147144
await supertest(server)
148145
.post('/')
149146
.send(test.body)
150147
.set('Content-Type', 'application/json')
151148
.expect(204);
152-
assert.deepStrictEqual(receivedData, { some: 'payload' });
149+
assert.deepStrictEqual(receivedData, {some: 'payload'});
153150
assert.notStrictEqual(receivedContext, null);
154151
assert.strictEqual(receivedContext!.eventId, 'testEventId');
155152
assert.strictEqual(receivedContext!.timestamp, 'testTimestamp');
@@ -175,14 +172,14 @@ const TEST_CLOUD_EVENT = {
175172
describe('CloudEvents request to event function', () => {
176173
interface TestData {
177174
name: string;
178-
headers: { [key: string]: string };
175+
headers: {[key: string]: string};
179176
body: {};
180177
}
181178

182179
const testData: TestData[] = [
183180
{
184181
name: 'CloudEvents v1.0 structured content mode',
185-
headers: { 'Content-Type': 'application/cloudevents+json' },
182+
headers: {'Content-Type': 'application/cloudevents+json'},
186183
body: TEST_CLOUD_EVENT,
187184
},
188185
{
@@ -204,13 +201,10 @@ describe('CloudEvents request to event function', () => {
204201
it(`should receive data and context from ${test.name}`, async () => {
205202
let receivedData: {} | null = null;
206203
let receivedContext: functions.CloudEventsContext | null = null;
207-
const server = getServer(
208-
(data: {}, context: functions.Context) => {
209-
receivedData = data;
210-
receivedContext = context as functions.CloudEventsContext;
211-
},
212-
SignatureType.EVENT
213-
);
204+
const server = getServer((data: {}, context: functions.Context) => {
205+
receivedData = data;
206+
receivedContext = context as functions.CloudEventsContext;
207+
}, SignatureType.EVENT);
214208
await supertest(server)
215209
.post('/')
216210
.set(test.headers)
@@ -238,14 +232,14 @@ describe('CloudEvents request to event function', () => {
238232
describe('CloudEvents request to cloudevent function', () => {
239233
interface TestData {
240234
name: string;
241-
headers: { [key: string]: string };
235+
headers: {[key: string]: string};
242236
body: {};
243237
}
244238

245239
const testData: TestData[] = [
246240
{
247241
name: 'CloudEvents v1.0 structured content mode',
248-
headers: { 'Content-Type': 'application/cloudevents+json' },
242+
headers: {'Content-Type': 'application/cloudevents+json'},
249243
body: TEST_CLOUD_EVENT,
250244
},
251245
{
@@ -266,12 +260,9 @@ describe('CloudEvents request to cloudevent function', () => {
266260
testData.forEach(test => {
267261
it(`should receive data and context from ${test.name}`, async () => {
268262
let receivedCloudEvent: functions.CloudEventsContext | null = null;
269-
const server = getServer(
270-
(cloudevent: functions.CloudEventsContext) => {
271-
receivedCloudEvent = cloudevent as functions.CloudEventsContext;
272-
},
273-
SignatureType.CLOUDEVENT
274-
);
263+
const server = getServer((cloudevent: functions.CloudEventsContext) => {
264+
receivedCloudEvent = cloudevent as functions.CloudEventsContext;
265+
}, SignatureType.CLOUDEVENT);
275266
await supertest(server)
276267
.post('/')
277268
.set(test.headers)

test/pubsub_middleware.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import * as assert from 'assert';
1515
import * as sinon from 'sinon';
1616
import {Response, Request} from 'express';
17-
import {legacyPubSubEventMiddleware, MarshalledPubSubBody} from '../src/pubsub_middleware';
17+
import {
18+
legacyPubSubEventMiddleware,
19+
MarshalledPubSubBody,
20+
} from '../src/pubsub_middleware';
1821

1922
const PUB_SUB_TOPIC = 'projects/FOO/topics/BAR_TOPIC';
2023
const RAW_PUBSUB_BODY = {
@@ -23,29 +26,29 @@ const RAW_PUBSUB_BODY = {
2326
data: 'eyJmb28iOiJiYXIifQ==',
2427
messageId: '1',
2528
attributes: {
26-
test: '123'
27-
}
28-
}
29+
test: '123',
30+
},
31+
},
2932
};
3033

3134
const marshalledPubSubBody = (topic: string | null): MarshalledPubSubBody => ({
3235
data: {
3336
'@type': 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
3437
data: 'eyJmb28iOiJiYXIifQ==',
3538
attributes: {
36-
test: '123'
37-
}
39+
test: '123',
40+
},
3841
},
3942
context: {
4043
eventId: '1',
4144
eventType: 'google.pubsub.topic.publish',
4245
resource: {
4346
name: topic,
4447
service: 'pubsub.googleapis.com',
45-
type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage'
48+
type: 'type.googleapis.com/google.pubsub.v1.PubsubMessage',
4649
},
4750
timestamp: new Date().toISOString(),
48-
}
51+
},
4952
});
5053

5154
describe('legacyPubSubEventMiddleware', () => {
@@ -69,19 +72,19 @@ describe('legacyPubSubEventMiddleware', () => {
6972
name: 'raw pub/sub request',
7073
path: `/${PUB_SUB_TOPIC}?pubsub_trigger=true`,
7174
body: RAW_PUBSUB_BODY,
72-
expectedBody: () => marshalledPubSubBody(PUB_SUB_TOPIC)
75+
expectedBody: () => marshalledPubSubBody(PUB_SUB_TOPIC),
7376
},
7477
{
7578
name: 'raw pub/sub request without topic in URL path',
7679
path: '/myfunction',
7780
body: RAW_PUBSUB_BODY,
78-
expectedBody: () => marshalledPubSubBody(null)
81+
expectedBody: () => marshalledPubSubBody(null),
7982
},
8083
{
8184
name: 'non-pub/sub request',
8285
path: `/${PUB_SUB_TOPIC}?pubsub_trigger=true`,
83-
body: {foo: "bar"},
84-
expectedBody: () => ({foo: "bar"})
86+
body: {foo: 'bar'},
87+
expectedBody: () => ({foo: 'bar'}),
8588
},
8689
];
8790

@@ -90,7 +93,7 @@ describe('legacyPubSubEventMiddleware', () => {
9093
const clock = sinon.useFakeTimers();
9194
const next = sinon.spy();
9295
const request = {
93-
path: test.path,
96+
path: test.path,
9497
body: test.body,
9598
};
9699
legacyPubSubEventMiddleware(request as Request, {} as Response, next);
@@ -99,4 +102,4 @@ describe('legacyPubSubEventMiddleware', () => {
99102
clock.restore();
100103
});
101104
});
102-
});
105+
});

0 commit comments

Comments
 (0)