When I want to add requestHooks (for example) to my test and fixture I basically don't know where to do it.
I am using this repo https://github.com/rquellh/testcafe-cucumber
I find a solution. however, it is not stable: sometimes it throws an error: '[object DOMException]:\n No stack trace available'. Maybe someone knows why?
The code ( after creat mock and logger object as in testCafe doc):
When('I log in as free user', async () => {
await testController.addRequestHooks(mock)
await testController.addRequestHooks(logger)
await testController.wait(2000)
await testController
.click(selector)
.typeText(selector,string, {replace : true})
.typeText(selector,string, {replace: true})
.click(selector);
});
UPDATE: now it works with wait() function, but maybe there is some more elegant answer for that?
testController is available in Given, When, Then steps.
So, you can use the test controller standard methods: addRequestHooks and removeRequestHooks.
I've modified the example from the https://github.com/rquellh/testcafe-cucumber repository to demonstrate the RequestLogger usage.
const {Given, When, Then} = require('cucumber');
const Role = require('testcafe').Role;
const RequestLogger = require('testcafe').RequestLogger;
const githubPage = require('../support/pages/github-page');
const logger = new RequestLogger('https://github.com');
Given(/^I open the GitHub page$/, async function() {
await testController.addRequestHooks(logger);
await testController.navigateTo(githubPage.github.url());
});
...
Then(/^Logger should contain captured request information$/, async function() {
await testController.expect(logger.contains(record => record.response.statusCode === 200)).ok();
});
...
My solution (gherkin-testcafe: ^2.2.0):
The definition:
Feature: Check server names
Scenario Outline: Fetch pages
Given there is the <url>
When I check the response status
Then the http <prameter> equals the <value>
Examples:
| url | prameter | value |
| https://www.seznam.cz | server | nginx |
| https://www.google.com | server | gws |
And the implementation:
const {Given, When, Then} = require('cucumber');
const {RequestLogger} = require('testcafe');
let logger;
Given(/there is the (.+)/, async (t, [url]) => {
logger = RequestLogger(url, {
logResponseHeaders: true,
});
await t.addRequestHooks(logger);
await t.navigateTo(url);
});
When(/I check the response status/, async t => {
await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();
});
Then(/the http (.+) equals the (.+)/, async (t, [name, value]) => {
await t.expect(logger.contains(record => record.response.headers.server === value)).ok();
});
Related
I have been working on a firebase project in which I created a cloud function that creates documents in firestore. This is the function -
export const createExpenseCategory = functions
.region("europe-west1")
.https.onCall(async (data, context) => { // data is a string
if (!context.auth?.uid) { // check that requesting user is authenticated
throw new functions.https.HttpsError(
"unauthenticated",
"Not Authenticated"
);
}
const res = await admin
.firestore()
.collection("/categories/")
.where("uid", "==", context.auth.uid)
.get();
const categoryExists = res.docs.find((doc) => doc.data().name === data); // check that there are not duplicates.
// doc looks like this -
// {
// "name": "Food",
// "uid": "some_long_uid"
// }
if (categoryExists) {
throw new functions.https.HttpsError(
"already-exists",
`Category ${data} already exists`
);
}
return admin
.firestore()
.collection("/categories/")
.add({ name: data, uid: context.auth.uid });
});
As you can see, at the beginning of the function I check whether the user that sent the request is authenticated with the context parameter. Everything works fine when I play around with it in my web app, but I have been trying to figure out a way to create a unittest for this function. My problem is that I can't really figure out how to create an authenticated request to make sure that my function doesn't fail every time. I tried to look online for any documentation but couldn't seem to find any.
Thanks in advance!
You can unit test your functions using the firebase-functions-test SDK. The guide mentions you can mock the data within the eventContext or context parameter passed to your function. This works for mocking the uid field of the auth object:
// Left out authType as it's only for RTDB
wrapped(data, {
auth: {
uid: 'jckS2Q0'
}
});
The guide uses mocha for testing, but you can use other testing frameworks. I made a simple test to see if it would work and I could send the mock uid to the function, which worked as expected:
index.js
exports.authTest = functions.https.onCall( async (data, context) => {
if(!context.auth.uid){
throw new functions.https.HttpsError('unauthenticated', 'Missing Authentication');
}
const q = await admin.firestore().collection('users').where('uid', '==', context.auth.uid).get();
const userDoc = q.docs.find(doc => doc.data().uid == context.auth.uid);
return admin.firestore().collection('users').doc(userDoc.id).update({name: data.name});
});
index.test.js
const test = require('firebase-functions-test')({
projectId: PROJECT_ID
}, SERVICE_ACCTKEY); //Path to service account file
const admin = require('firebase-admin');
describe('Cloud Functions Test', () => {
let myFunction;
before(() => {
myFunction = require('../index.js');
});
describe('AuthTest', () => {
it('Should update user name in UID document', () => {
const wrapped = test.wrap(myFunction.authTest);
const data = {
name: 'FooBar'
}
const context = {
auth: {
uid: "jckS2Q0" //Mocked uid value
}
}
return wrapped(data, context).then(async () => {
//Asserts that the document is updated with expected value, fetches it after update
const q = await admin.firestore().collection('users').where('uid', '==', context.auth.uid).get();
const userDoc = q.docs.find(doc => doc.data().uid == context.auth.uid);
assert.equal(userDoc.data().name, 'FooBar');
});
});
});
});
Let me know if this was useful.
I have an graphql server connected to my Firebase RTD and deployed on heroku.
When I run my server in heroku the request to reservations resolver takes forever and eventually the Playground yells Unexpected token < in JSON at position 0
I suspect this is a timeout from Firebase, but how would I go about debugging this? (Heroku logs nothing about the error).
You can try the server for yourself: https://filex-database.herokuapp.com
The specific query that's causing me trouble is:
query {
reservations {
code
name
}
}
const db = require("../datasources/db");
const masterlist = require("../datasources/masterlist.js");
const getById = (key: string, id: string) =>
db[key].filter((item) => item.id === id)[0];
const firebaseQuery = (context: { firebaseClient }, endpoint: string) => {
const finalEndpoint =
endpoint.charAt(0) === "/" ? endpoint : "/".concat(endpoint);
const baseUrl = "/workshops";
return context.firebaseClient
.database()
.ref(`${baseUrl}${finalEndpoint}`)
.once("value")
.then((snapshot) => snapshot.val());
};
const Query = {
workshops: () => db.workshops,
workshop: (_, args) => getById("workshops", args.id),
options: () => db.options,
option: (_, args) => getById("options", args.id),
// this resolver is causing me trouble
reservations: async (_, __, context) => {
const data = await firebaseQuery(context, "/applicants");
return Object.values(data);
},
reservation: async (_, args, context) => {
const data = await firebaseQuery(context, `/applicants/${args.id}`);
return data;
},
};
module.exports = { Query };
EDIT: I made another simple server using the same technology and only delivering that resolver and it also timesout (everything works fine locally though)
http://apollo-testing-gonzo.herokuapp.com
I've written the below code to create a task whenever a new item is added to a collection in the firestore.
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
const { CloudTasksClient } = require('#google-cloud/tasks')
exports.moveActivityFromPlanToRecord = () =>
functions
.region('europe-west1')
.firestore.document('Users/{userId}/Activities/{activityId}')
.onCreate(async snapshot => {
const moveTime = snapshot.data()! as MoveTime
if (!moveTime || !moveTime.dueTime) {
console.log("DueTime is empty or null: \n" + moveTime)
return
}
// Get the project ID from the FIREBASE_CONFIG env var
const project = JSON.parse(process.env.FIREBASE_CONFIG!).projectId
const location = 'europe-west1'
const queue = 'activityDateEventChecker'
//queuePath is going to be a string that uniquely identifes the task
const tasksClient = new CloudTasksClient()
const queuePath: string =
tasksClient.queuePath(project, location, queue)
// URL to my callback function and the contents of the payload to deliver
const url = `https://${location}-${project}.cloudfunctions.net/activityDateEventCheckerCallback`
const docPath = snapshot.ref.path
const dueTime = moveTime.dueTime
const payload: MoveTaskPayload = { docPath, dueTime }
console.log(payload)
// build up the configuration for the Cloud Task
const task = {
httpRequest: {
httpMethod: 'POST',
url: url,
body: Buffer.from(JSON.stringify(payload)).toString('base64'),
headers: {
'Content-Type': 'application/json',
},
},
scheduleTime: {
seconds: moveTime.dueTime / 1000
}
}
// enqueue the task in the queue
return tasksClient.createTask({ parent: queuePath, task: task })
})
interface MoveTime extends admin.firestore.DocumentData {
dueTime?: number
}
interface MoveTaskPayload {
docPath: string,
dueTime: number
}
When the function is triggered (when a new "activity" is added to the collection), it throws the following error:
Error: 3 INVALID_ARGUMENT: Request contains an invalid argument
What could be the problem here?
BTW, should the last line in the method return the task, or await it?
EDIT: The exact same code is now working without me changing anything! I just deployed it with the Termux app just for fun, and after that it started working!
My guess is your current issue stems from:
`exports.moveActivityFromPlanToRecord = () => ...
If you remove the () => part then when the moveActivityFromPlanToRecord function is called, the expected parameters will need to match what onCreate() expects.
That shouldn't solve everything for you because onCreate() takes two parameters, function onCreate(snapshot: DataSnapshot, context: EventContext): PromiseLike<any> | any (took that from the docs rather than source code because I'm on mobile). Which means that there is no way for the onCreate() function to receive parameters in your current implementation.
What could be the problem here? \ BTW, should the last line in the method return the task, or await it?
Almost certain that you need to return a Promise or String. That's a basic understanding you should read about for all background Google Cloud functions.
There may be other issues, but that should help resolve what your asking for.
I am trying to write something to check that "About Us" exist on the following page: https://www.aggrowth.com/en-us/about-us and I am just hitting a wall. It shouldn't be difficult, but I have spent too much time on this.
We are using Gherking-testcafe: https://www.npmjs.com/package/gherkin-testcafe
NPM: 6.9.0
TestCafe: 1.0.1
Gherking-Testcafe: 2.0.0
I tried (All below was tested isolation, aka all of the different t.expect was run by themselves):
const h1AboutUs = await Selector('h1');
await t.expect(h1AboutUs.innerText).eql('About Us');
await t.expect(h1AboutUs.innerText).contains('About Us');
await t.expect(h1AboutUs.value).eql('About Us');
await t.expect(Selector('html').textContent).contains('About Us');
and tried removing the await:
const h1AboutUs = Selector('h1');
await t.expect(h1AboutUs.innerText).eql('About Us');
await t.expect(h1AboutUs.innerText).contains('About Us');
await t.expect(h1AboutUs.value).eql('About Us');
await t.expect(Selector('html').textContent).contains('About Us');
It works if I do:
This is the test I have:
When("I see the page load", async t => {
const h1AboutUs = await Selector('h1');
await t.expect(h1AboutUs.visible).eql(true);
await t.hover(h1AboutUs);
await t.expect(h1AboutUs.value).contains('about');
console.log(h1AboutUs.value);
});
My testCafe runner:
const createTestCafe = require('gherkin-testcafe');
const fs = require('fs');
const reportPath = './frontend/src/tests/test-reports'
let testcafe = null;
function readTestCafeConfig() {
configData = fs.readFileSync('.testcaferc.json', 'utf8');
const js = JSON.parse(configData);
return getJSONValues(js, 'src');
};
function getJSONValues(obj, key) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (i === key) {
objects.push(obj[i]);
}
}
return objects;
}
createTestCafe('localhost', 1337, 1338)
.then(tc => {
testcafe = tc;
const runner = testcafe.createRunner();
const src = readTestCafeConfig();
return runner
.src([src])
.browsers('chrome')
.reporter(['spec', {
name: 'json',
output: `${reportPath}/report/report.json`
},
{
name: 'xunit',
output: `${reportPath}/report/report.xml`
}])
// .video(`${reportPath}/videos`, {
// singleFile: true,
// failedOnly: true,
// pathPattern: '${USERAGENT}/${FILE_INDEX}.mp4'
// })
.tags('#aboutUs')
.run();
})
.then(failedCount => {
console.log('Tests failed: ' + failedCount);
testcafe.close();
});
The error I get in the console is:
1) Selector cannot implicitly resolve the test run in context of which it should be executed. If you need to call Selector from the Node.js API callback, pass the test controller manually via
Selector's `.with({ boundTestRun: t })` method first. Note that you cannot execute Selector outside the test code.
Browser: Chrome 74.0.3729 / Mac OS X 10.14.4
12 |});
13 |
14 |When("I see the page load", async t => {
15 | const h1AboutUs = await Selector('h1');
16 |
> 17 | await t.expect(h1AboutUs.visible).eql(true);
18 | await t.hover(h1AboutUs);
19 | await t.expect(h1AboutUs.value).contains('about');
20 | console.log(h1AboutUs.value);
21 |});
22 |
I expect not to see this error msg
You need to implement Selector binding to TestCafe's test controller for such tests. Please have a look at the following example:
const { Given, Then, Before } = require('cucumber');
const { Selector: NativeSelector } = require('testcafe');
const Selector = (input, t) => {
return NativeSelector(input).with({ boundTestRun: t });
};
Before('#aboutHook', async () => {
console.log('Running AGI test.');
});
Given("I am open AGI page", async t => {
await t.navigateTo('https://www.aggrowth.com/en-us/about-us');
});
Then("I should see check about us", async t => {
const h1AboutUs = Selector('h1', t);
//or const h1AboutUs = await Selector('h1', t); if you need
await t
.expect(h1AboutUs.visible).eql(true)
.hover(h1AboutUs);
});
You can get more examples in the gherkin-testcafe repository.
Note also that the h1 element doesn't have a property value.
You can learn more about TestCafe Selectors and their properties in TestCafe Docs.
I would like to call an asynchronous function outside the lambda handler with by the following code:
var client;
(async () => {
var result = await initSecrets("MyWebApi");
var secret = JSON.parse(result.Payload);
client= new MyWebApiClient(secret.API_KEY, secret.API_SECRET);
});
async function initSecrets(secretName) {
var input = {
"secretName" : secretName
};
var result = await lambda.invoke({
FunctionName: 'getSecrets',
InvocationType: "RequestResponse",
Payload: JSON.stringify(input)
}).promise();
return result;
}
exports.handler = async function (event, context) {
var myReq = await client('Request');
console.log(myReq);
};
The 'client' does not get initialized. The same code works perfectly if executed within the handler.
initSecrets contains a lambda invocation of getSecrets() which calls the AWS SecretsManager
Has anyone an idea how asynchronous functions can be properly called for initialization purpose outside the handler?
Thank you very much for your support.
I ran into a similar issue trying to get next-js to work with aws-serverless-express.
I fixed it by doing the below (using typescript so just ignore the :any type bits)
const appModule = require('./App');
let server: any = undefined;
appModule.then((expressApp: any) => {
server = createServer(expressApp, null, binaryMimeTypes);
});
function waitForServer(event: any, context: any){
setImmediate(() => {
if(!server){
waitForServer(event, context);
}else{
proxy(server, event, context);
}
});
}
exports.handler = (event: any, context: any) => {
if(server){
proxy(server, event, context);
}else{
waitForServer(event, context);
}
}
So for your code maybe something like
var client = undefined;
initSecrets("MyWebApi").then(result => {
var secret = JSON.parse(result.Payload);
client= new MyWebApiClient(secret.API_KEY, secret.API_SECRET)
})
function waitForClient(){
setImmediate(() => {
if(!client ){
waitForClient();
}else{
client('Request')
}
});
}
exports.handler = async function (event, context) {
if(client){
client('Request')
}else{
waitForClient(event, context);
}
};
client is being called before it has initialised; the client var is being "exported" (and called) before the async function would have completed. When you are calling await client() the client would still be undefined.
edit, try something like this
var client = async which => {
var result = await initSecrets("MyWebApi");
var secret = JSON.parse(result.Payload);
let api = new MyWebApiClient(secret.API_KEY, secret.API_SECRET);
return api(which) // assuming api class is returning a promise
}
async function initSecrets(secretName) {
var input = {
"secretName" : secretName
};
var result = await lambda.invoke({
FunctionName: 'getSecrets',
InvocationType: "RequestResponse",
Payload: JSON.stringify(input)
}).promise();
return result;
}
exports.handler = async function (event, context) {
var myReq = await client('Request');
console.log(myReq);
};
This can be also be solved with async/await give Node v8+
You can load your configuration in a module like so...
const fetch = require('node-fetch');
module.exports = async () => {
const config = await fetch('https://cdn.jsdelivr.net/gh/GEOLYTIX/public/z2.json');
return await config.json();
}
Then declare a _config outside the handler by require / executing the config module. Your handler must be an async function. _config will be a promise at first which you must await to resolve into the configuration object.
const _config = require('./config')();
module.exports = async (req, res) => {
const config = await _config;
res.send(config);
}
Ideally you want your initialization code to run during the initialization phase and not the invocation phase of the lambda to minimize cold start times. Synchronous code at module level runs at initialization time and AWS recently added top level await support in node14 and newer lambdas: https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/ . Using this you can make the init phase wait for your async initialization code by using top level await like so:
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
console.log("start init");
await sleep(1000);
console.log("end init");
export const handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
This works great if you are using ES modules. If for some reason you are stuck using commonjs (e.g. because your tooling like jest or ts-node doesn't yet fully support ES modules) then you can make your commonjs module look like an es module by making it export a Promise that waits on your initialization rather than exporting an object. Like so:
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const main = async () => {
console.log("start init");
await sleep(1000);
console.log("end init");
const handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
};
return { handler };
};
# note we aren't exporting main here, but rather the result
# of calling main() which is a promise resolving to {handler}:
module.exports = main();