"Failed to get configuration" using Wolkenkit client in Mocha test through Node - node.js

I am trying to interact with my Wolkenkit application through a Mocha test in Node.
Following the tutorial on client connections, when running the test, I get the following error:
Error: Failed to get configuration.
at ConfigurationWatcher.wentOffline (node_modules/wolkenkit-client/dist/ConfigurationWatcher.js:113:28)
at /home/aef/Projects/experiments/wolkenkit_bullet/node_modules/wolkenkit-client/dist/ConfigurationWatcher.js:101:16
at tryCatch (node_modules/es6-promise/dist/es6-promise.js:409:12)
at invokeCallback (node_modules/es6-promise/dist/es6-promise.js:424:13)
at publish (node_modules/es6-promise/dist/es6-promise.js:398:7)
at publishRejection (node_modules/es6-promise/dist/es6-promise.js:339:3)
at flush (node_modules/es6-promise/dist/es6-promise.js:128:5)
at processTicksAndRejections (internal/process/task_queues.js:79:11)
Any help on resolving this is highly appreciated.
I added the following dependencies in package.json:
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^7.0.1"
},
"dependencies": {
"wolkenkit": "^3.1.2",
"wolkenkit-client": "^3.1.0"
},
My test code looks like this:
'using strict';
const expect = require('chai').expect;
const wolkenkit = require('wolkenkit-client');
describe("wolkenkit app", () => {
it("first test", async () => {
const app = await wolkenkit.connect({host: 'local.wolkenkit.io', port: 3000});
});
})

While setting the environment variable within Node didn't work, the problem has been solved by setting NODE_TLS_REJECT_UNAUTHORIZED to 0 in the system environment like:
export NODE_TLS_REJECT_UNAUTHORIZED=0
Thanks to #mattwagl for pointing into the right direction.

This error is probably caused by your local self signed certificate. The client is not able to connect to the backend since it does not trust this certificate. You can bypass this check using the process.env.NODE_TLS_REJECT_UNAUTHORIZED flag like this…
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
suite('integration', () => {
let application;
suiteSetup(async () => {
application = await wolkenkit.connect({ host: 'local.wolkenkit.io', port: 3000 });
});
Another option would be to add the certificate to your trusted certificates.

Related

Shopify node api context initalize errors out

I'm trying to make an app following these directions:
https://github.com/Shopify/shopify-node-api/blob/main/docs/getting_started.md
I have all the code configred and it looks like this:
// src/index.ts
import http from 'http';
import url from 'url';
import querystring from 'querystring';
import Shopify, { ApiVersion } from '#shopify/shopify-api';
require('dotenv').config();
const { API_KEY, API_SECRET_KEY, SCOPES, SHOP, HOST } = process.env
Shopify.Context.initialize({
API_KEY,
API_SECRET_KEY,
SCOPES: [SCOPES],
HOST_NAME: HOST.replace(/https?:\/\//, ""),
HOST_SCHEME: HOST.split("://")[0],
IS_EMBEDDED_APP: {boolean},
API_VERSION: ApiVersion.{version} // all supported versions are available, as well as "unstable" and "unversioned"
});
// Storing the currently active shops in memory will force them to re-login when your server restarts. You should
// persist this object in your app.
const ACTIVE_SHOPIFY_SHOPS: { [key: string]: string | undefined } = {};
async function onRequest(
request: http.IncomingMessage,
response: http.ServerResponse,
): Promise<void> {
const {headers, url: req_url} = request;
const pathName: string | null = url.parse(req_url).pathname;
const queryString: string = String(url.parse(req_url).query);
const query: Record<string, any> = querystring.parse(queryString);
switch (pathName) {
default:
// This shop hasn't been seen yet, go through OAuth to create a session
if (ACTIVE_SHOPIFY_SHOPS[SHOP] === undefined) {
// not logged in, redirect to login
response.writeHead(302, {Location: `/login`});
response.end();
} else {
response.write('Hello world!');
// Load your app skeleton page with App Bridge, and do something amazing!
}
return;
} // end of default path
} // end of onRequest()
http.createServer(onRequest).listen(3000);
Package JSON looks like this:
{
"name": "shopify-checkout-apit",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"#shopify/shopify-api": "^3.1.0"
},
"devDependencies": {
"#types/node": "^17.0.40",
"dotenv": "^16.0.1",
"typescript": "^4.7.3"
},
"scripts": {
"build": "npx tsc",
"prestart": "yarn run build",
"start": "node dist/index.js"
}
}
When I go to run the app with yarn start I get a ton of errors
PS C:\Users\kawnah\shopify-checkout-apit> yarn start yarn run v1.22.18
$ yarn run build $ npx tsc src/index.ts:17:27 - error TS1003:
Identifier expected.
17 API_VERSION: ApiVersion.{version} // all supported versions are
available, as well as "unstable" and "unversioned"
~
src/index.ts:18:1 - error TS1005: ',' expected.
18 }); ~
Found 2 errors in the same file, starting at: src/index.ts:17
error Command failed with exit code 2. info Visit
https://yarnpkg.com/en/docs/cli/run for documentation about this
command. error Command failed with exit code 2. info Visit
https://yarnpkg.com/en/docs/cli/run for documentation about this
command. PS C:\Users\kawnah\shopify-checkout-apit>
I have no idea what any of this means.
Typescript Error TS1003 when attempting to access object properties using bracket notation
Why does this trigger an Identifier Expected error in Typescript?
I tried deleting node modules and reinstalling but it didn't work.
How do you fix this?
the config needs to look like this
Shopify.Context.initialize({
API_KEY,
API_SECRET_KEY,
SCOPES: [SCOPES],
HOST_NAME: HOST.replace(/https?:\/\//, ""),
HOST_SCHEME: HOST.split("://")[0],
IS_EMBEDDED_APP: true,
API_VERSION: ApiVersion.October21 // all supported versions are available, as well as "unstable" and "unversioned"
});

Unhandled error while running jest-puppeteer test

I am trying to set up testing for my puppeteer project. I was following a basic guide and the test passes but there is 2 console errors in the terminal.
The error doesn't show up when using https://google.com or https://youtube.com. So it looks like it could be a thing with the specific site?
console.error
Unhandled error
at process.uncaught (node_modules/jest-jasmine2/build/jasmine/Env.js:248:21)
at handler (node_modules/jest-environment-puppeteer/lib/PuppeteerEnvironment.js:17:11)
at map (node_modules/mitt/src/index.ts:74:75)
at Array.map (<anonymous>)
at Object.emit (node_modules/mitt/src/index.ts:74:56)
at Page.emit (node_modules/puppeteer/lib/EventEmitter.js:72:22)
console.error
at process.uncaught (node_modules/jest-jasmine2/build/jasmine/Env.js:249:21)
at handler (node_modules/jest-environment-puppeteer/lib/PuppeteerEnvironment.js:17:11)
at map (node_modules/mitt/src/index.ts:74:75)
at Array.map (<anonymous>)
at Object.emit (node_modules/mitt/src/index.ts:74:56)
at Page.emit (node_modules/puppeteer/lib/EventEmitter.js:72:22)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.613 s
Ran all test suites.
Here is my code
describe('NCAA Home', () => {
beforeAll(async () => {
await page.goto('http://stats.ncaa.org/rankings/change_sport_year_div');
});
it('should be titled "NCAA Statistics"', async () => {
await expect(page.title()).resolves.toMatch('NCAA Statistics');
});
});
Here is my jest.config.js
module.exports = {
preset: "jest-puppeteer",
testMatch: [
"**/test/**/*.test.js"
],
verbose: true
}
package.json
{
"name": "stackoverflow",
"version": "1.0.0",
"description": "",
"main": "index.js",
"jest": {
"preset": "jest-puppeteer"
},
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^26.1.0",
"jest-puppeteer": "^4.4.0"
},
"dependencies": {
"puppeteer": "^5.1.0"
}
}
All of the things I have come across have mentioned an issue with async/await but anything I have tried produces the same, if not, more errors. I have made a new project with these files and I am getting the same error
The error is from the website itself. Check the console of the website. Hence for a websites like google.com or youtube.com, it works without any errors.
I have created clean repo which reproduces issue.
https://github.com/sergtimosh/jest-puppeteer-issue-reproduction.git
clone repository
npm i
npm test test.spec.js
or
HEADLESS=false npm test test.spec.js
A workaround is to create incognito browser context in jest-environment.js.
Just uncomment two lines in this file and tests are passing with no issues. But problem is still here if you need to share browser context between test suites(files).
const PuppeteerEnvironment = require('jest-environment-puppeteer');
class JestEnvironment extends PuppeteerEnvironment {
async setup() {
await super.setup()
//to fix issue uncomment next two lines
// const incognitoContext = await this.global.browser.createIncognitoBrowserContext()
// this.global.page = await incognitoContext.newPage()
}
async teardown() {
await super.teardown()
}
}
module.exports = JestEnvironment;

Deploy Express server and test GraphQL API via CircleCI

I wish to test that my Express server launches properly and that my endpoint retrieves the correct information. I wish to do this in CI so that I can monitor when my application is no longer working, if I make any changes in the future.
It's a very simple application, my server looks like this:
server.js
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { GraphQLObjectType, GraphQLString, GraphQLSchema } from 'graphql';
import cors from 'cors';
const QueryRoot = new GraphQLObjectType({
name: 'Query',
fields: () => ({
hello: {
type: GraphQLString,
resolve: () => "Hello world!"
}
})
});
const schema = new GraphQLSchema({ query: QueryRoot });
const app = express();
app.use(cors()); // enable cors
app.use('/api', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(1337, () => {
console.log('Server running on port: 1337');
});
basicTest.test.js:
const chai = require('chai');
const expect = chai.expect;
const url = `http://localhost:1337`;
const request = require('supertest')(url);
describe('GraphQL', () => {
it('Response returns hello world', (done) => {
request.post('/api')
.send({ query: ' { hello }' })
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body.data.hello).to.contain('Hello world!');
done();
});
});
});
My .circleci/config.yml looks like this:
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
docker:
# specify the version you desire here
- image: circleci/node:10.16.0
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run:
npm start &
npm test
and package.json:
{
"name": "graphql-express-server",
"repository": {
"url": "myURL"
},
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon -r esm server.js",
"test": "mocha test/*.test.js"
},
"author": "AuthorName",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"esm": "^3.2.25",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^14.4.2",
"nodemon": "^1.19.1",
"supertest": "^4.0.2"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.0"
}
}
I tried adding & to the end of my npm start command but that just skips the entire process as far as I can see. The issue I'm getting is 'Error: ECONNREFUSED: Connection refused' when the test gets executed.
Do I need to host the server elsewhere? I'm unsure what I'm supposed to do. I've never really tested the back end in CI before.
EDIT:
My commands are as follows:
- run:
npm start &
- run:
sleep 5
- run:
npm test
And my output in CI is as follows:
--------------------------------
***npm start &***
#!/bin/bash -eo pipefail
npm start &
--------------------------------
***sleep 5***
#!/bin/bash -eo pipefail
sleep 5
--------------------------------
***npm test***
#!/bin/bash -eo pipefail
npm test
> graphql-express-server#1.0.0 test /home/circleci/repo
> mocha test/*.test.js
GraphQL
1) Response returns hello world
0 passing (14ms)
1 failing
1) GraphQL
Response returns hello world:
Error: ECONNREFUSED: Connection refused
at Test.assert (node_modules/supertest/lib/test.js:165:15)
at localAssert (node_modules/supertest/lib/test.js:131:12)
at /home/circleci/repo/node_modules/supertest/lib/test.js:128:5
at Test.Request.callback (node_modules/superagent/lib/node/index.js:728:3)
at ClientRequest.req.once.err (node_modules/superagent/lib/node/index.js:647:10)
at Socket.socketErrorListener (_http_client.js:392:9)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! Test failed. See above for more details.
Exited with code 1
--------------------------------
As you can see, the message Server running on port: 1337 does not show up in my npm start & step console section.
Any suggestions? Am I doing something wrong?
npm start & won't wait for server to start, are you sure your server is listening on the port by the time your test makes a request? I.e. do you have 'Server running on port: 1337' in your console on CI? If not then try running sleep 3 before starting tests

Firebase - TypeError: Path must be a string. Received undefined

I am just starting up with firebase.
i am not sure ins and out of the firebase and based on my vaguely understanding, I have configured my app this way.
In the main Index.js file, I am requiring
const path = require('path')
const firebaseConfig = require("./src/config/firebaseConfig.js")
const firebaseDb = require("./src/helperFunctions/firebase_db.js")
Here, firebaseConfig is the place where I am configuring my firebase
const firebaseConfigJSON = require("./functions-config.json")
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert(firebaseConfigJSON),
databaseURL: "https://functions-firebase-43a59.firebaseio.com"
})
const db = admin.firestore()
db.settings({ timestampsInSnapshots: true });
module.exports = {
db
}
and then using this imported Db in firebaseDb
//All the operations at firebase store would be done from here
const firebaseDb = require("./../config/firebaseConfig.js")
firebaseDb.db.collection('users').add({
name: "Rohit Bhatia",
age: "24"
})
.then((response) => {
console.log("this is response", response)
})
.catch((err) => {
console.log("This is error in firebase", err)
})
Since most of the code is singleton here, I was expecting everything to go smoothly until I received following error
This is error in firebase TypeError: Path must be a string. Received
undefined
at assertPath (path.js:28:11)
at Object.join (path.js:1236:7)
at getPath (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/dir-glob/index.js:6:41)
at globs.concat.map.x (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/dir-glob/index.js:47:59)
at Array.map ()
at module.exports.sync (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/dir-glob/index.js:47:33)
at globDirs (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/globby/index.js:58:9)
at getPattern (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/globby/index.js:61:64)
at globTasks.reduce (/Users/anilbhatia/Desktop/google-functions/functions/node_modules/globby/index.js:107:19)
at Array.reduce ()
Can someone please help me in figuring out what could I be doing wrong? or perhaps did I actually got the firebase?
My initial goal was to create a collection in my firebase via my express app before putting data from api routes.
Try running:
npm install firebase-admin#6.4.0
Also you can do:
npm install
npm run build (inside functions folder.)
Then firebase deploy.
Fixed it for me.
We were able to revert the dir-glob to 2.0.0 by adding:
"dir-glob": "2.0.0",
"globby": "8.0.0",
In the package.json dependencies.
You can do this with:
npm install dir-glob#2.0.0 --save
npm install globby#8.0.0 --save
We then deleted the node_modules and run: npm install and deployed to Firebase
Future googler's
Make sure in your firebase.json file you have the correct source path:
{
"functions": {
"predeploy": "npm run build",
"source": "."
}
}

TypeError: Cannot read property 'address' of undefined supertest

I need some help to resolve my problem with testing on nodejs codes. I'm using mocha and supertest. I'm confused with the implementation in supertest. I don't know to resolved it. I'm trying to automate downloading a file.
describe('GET /entry/:entryId/file/:id/download', function(){
it('should pass download function', function(done){
this.timeout(15000);
request(app.webServer)
.get('/entry/543CGsdadtrE/file/wDRDasdDASAS/download')
.set('Authorization', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGco')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
console.log(err, res);
done();
});
});
});
I received a similar error from mocha when testing an express app. Full text of error:
0 passing (185ms)
2 failing
1) loading express responds to /:
TypeError: app.address is not a function
at Test.serverAddress (test.js:55:18)
at new Test (test.js:36:12)
at Object.obj.(anonymous function) [as get] (index.js:25:14)
at Context.testSlash (test.js:12:14)
2) loading express 404 everything else:
TypeError: app.address is not a function
at Test.serverAddress (test.js:55:18)
at new Test (test.js:36:12)
at Object.obj.(anonymous function) [as get] (index.js:25:14)
at Context.testPath (test.js:17:14)
I fixed it by adding this to my express server.js, i.e. export the server object
module.exports = app
Typescript users, who are facing this error, check two things:
The express server should have module.exports = app (thanks to #Collin D)
Use import * as app from "./app"
instead of wrong import app from "./app"
I was facing same problem, above solution didn't work for me, some one in my shoes
kindly follow this guy's
exports in server.js should be
module.exports.app = app;
If you have multiple modules than use es6 feature
module.exports = {
app,
something-else,
and-so-on
}
my package.json for version cross ref..
{
"name": "expressjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha **/*.test.js",
"start": "node app.js",
"test-watch": "nodemon --exec npm test"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.4",
"hbs": "^4.0.1"
},
"devDependencies": {
"mocha": "^5.2.0",
"supertest": "^3.3.0"
}
}

Resources