Cannot import custom NodeJs module in Vue component - node.js

I am building a webservice with Node and Vue. I built backend Node modules which are all working as expected. But when I try to import those in my Vue SFC script, I am getting 'stream undefined' error.
My hunch is that I am mixing CommonJS and ES6 styles of exporting and importing and that is causing the issue because when I created a temporary module with just one function and no require or import statements and use module.exports to expose that function and then call import in Vue SFC, it is working. But when I use require statements in the module, I am getting the stream undefined error again. I tried multiple solutions I found online but I am not able to get it to work.
My (temporary) node module, temp.js. This doesn't work:
require('./auth.js');
function test(){
console.log('Test method');
}
module.exports = {test};
This works:
function test(){
console.log('Test method');
}
module.exports = {test};
My SFC script:
<script>
import * as temp from './temp.js';
export default {
name: 'Dashboard'
}
</script>

Related

How to mock electron when running jest with #kayahr/jest-electron-runner

Setup
I want to unit test my electron app with jest. For this I have the following setup:
I use jest and use #kayahr/jest-electron-runner to run it with electron instead of node. Additionally, since it is a typescript project, I use ts-jest.
My jest.config.js looks like this:
module.exports = {
collectCoverage: true,
coverageDirectory: 'coverage',
coverageProvider: 'v8',
preset: 'ts-jest',
runner: '#kayahr/jest-electron-runner/main',
testEnvironment: 'node',
};
The test is expected to run in the main process. I have reduced my code to the following example function:
import { app } from 'electron';
export function bar() {
console.log('in bar', app); //this is undefined when mocked, but I have a real module if not mocked
const baz = app.getAppPath();
return baz;
}
The test file:
import electron1 from 'electron';
import { bar } from '../src/main/foo';
console.log('in test', electron1); //this is undefined in the test file after import
// jest.mock('electron1'); -> this does just nothing, still undefined
const electron = require('electron');
console.log('in test after require', electron); //I have something here yay
jest.mock('electron'); //-> but if I comment this in -> it is {} but no mock at all
it('should mock app', () => {
bar();
expect(electron.app).toBeCalled();
});
What do I want to do?
I want to mock electron.app with jest to look whether it was called or not.
What is the problem?
Mocking electron does not work. In contrast to other modules like fs-extra where jest.mock() behaves as expected.
I don't understand the behavior happening here:
Importing "electron" via import in the file containing the tests (not the file to be tested!) does not work (other modules work well), but require("electron") does.
I do have the electron module if not mocked in bar(), but after mocking not
while jest.mock("fs-extra") works, after jest.mock("electron") electron is only an empty object, not a mock
I would really like to understand what I did wrong or what the problem is. Switching back to #jest-runner/electron does not seem to be an option, since it is not maintained anymore. Also I don't know if this is even the root of the problem.
Has anyone seen this behavior before and can give me a hint where to start searching?

Translate javascript modules

I was trying to run some NodeJS code using QickJS and NectarJS, and I stumble in an old question I have, how to import modules.
In special, qjs -m <source-js> loads ES6 modules, and expect import export statements,, instead of require, and writing module.exports, or exports.
Also I use an builtin std module (that cannot be resolved by node_module).
For the first example I changed the code manually, but I would like to have a script to do the transformations. I expected this to be doable with webpack, but I can't find how.
Example
main.js
const h = require('./hello.js');
h.sayHello();
hello.js
const h = require('./print.js');
function sayHello(){
h.print("Hello");
}
module.exports = {sayHello};
print.js
module.exports = {
print(s) {
console.log(s);
}
}
I can run this with the command node main.js, but qjs -m main.js, will fail with 'require' is not defined

ReferenceError: firebase_app__WEBPACK_IMPORTED_MODULE_0__ is not defined in userFunction (Autodesk's viewer)

My app.bundle.js created by webpack is effing up:
/* harmony import */ var firebase_app__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(10);
/* harmony import */ var firebase_firestore__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(16);
var firebaseConfig = {
//config options for database
}; // Initialize Firebase
var defaultProject = firebase_app__WEBPACK_IMPORTED_MODULE_0__.default.initializeApp(firebaseConfig);
var defaultFirestore = defaultProject.firestore();
console.log("Firebase globals: ", firebase_app__WEBPACK_IMPORTED_MODULE_0__.default);
function userFunction(pdb) {
console.log("Firebase globals inside function: ", firebase_app__WEBPACK_IMPORTED_MODULE_0__.default);
}
As you can see firebase_app_WEBPACK_IMPORTED_MODULE_0__ is defined at the top yet when loading webpage error ReferenceError: firebase_app__WEBPACK_IMPORTED_MODULE_0__ is not defined pops up out of nowhere.
Update:
I actually found the mistake, basically I'm using Autodesk's Forge Viewer Library which at some point calls my function userFunction, userFunction needs firebase to process a list in one pass, sadly this function is executed in a webworker (async) which means I can't easily reference a variable outside the function's scope.
You will have to add an import for each Firebase SDK you want to use.
import firebase from 'firebase/app'
import 'firebase/storage'
import 'firebase/analytics'
import ....
If you already have done so, you also need them to be in correct order in a react project
The correct order:
import './initializedFirebase'
import App from './App'
If you import App.jsx before initializeFirebase.js, then firebase.storage() is used before it gets initialized.
Also consider upgrading your version of Firebase which solves many issues.

Cypress: Cannot use cy.task() to load dataset to Mongo before tests

I'm trying to use cy.taks() to load certain datasets to mongo before a test is run. But I'm getting errors. I've got a module where I export 2 functions, one from dropping a collection, and the other to load an object to a collection. Here is my cypress/plugins/index.js:
module.exports = (on, config) => {
on("task", {
"defaults:db": () => {
const {dropCollection, createUser } = require("../../lib/connectDB");
dropCollection("users");
createUser(userData)
},
});
};
Here is my /lib/connecDB.js:
export function dropCollection(collection) {
return mongoose.connection.dropCollection(collection);
}
export async function createUserInDB(userData) {
await User.create(userData);
}
So when I run the test, I'm getting:
cy.task('defaults:db') failed with the following error:
Unexpected token 'export'
Tried as well importing these function outside the index.js export, but getting same result.
I'd say it is something about export/import. The functions are exported as ES6, and imported as ES5.
I've tried to import the function the ES6 like:
import { dropCollection, createUser } from '../lib/connectDB'
And then export the plugin function also as ES6, but then I get:
Error: The plugins file is missing or invalid.
Your `pluginsFile` is set to `C:\Users\someRoute\cypress\plugins\index.js`, but either the file is missing, it contains a syntax error, or threw an error when required. The `pluginsFile` must be a `.js`, `.ts`, or `.coffee` file.
I've also tried to import required modules outside the function like:
const globalDbUtils = require('../lib/connectDB')
And then use the functions as
globalDbUtils.dropCollection("collectionName")
globalDbUtils.createUser(userData)
But I'm getting last error. I've tried pretty much everything, I tried to import Mongoose models straight, mongo client etc...Also I tried to import just one function and return it (just copy/pasting official doc...) but cannot make it work. I researched for a couple of days getting nothing, I found there is a npm package that helps u doing this, but since cypress allows you to do this by using no more plugins, I'd like to do it with no more tools than cypress itself.
Anyone knows what I am doing wrong?
Thanks in advance!
You need to use require instead of import at the top of your file and when exporting at the bottom use
module.exports = { createUserInDB }
Instead of exporting as you are currently doing.

module import work with react in dev but not in build

I am trying to have the same module working with both Node.js and React. This works fine until I try to create a build application with React.
Here is what I am doing
var DummyTest = function DummyTest(){
this.hello=function(){
console.log("Hello Dummy Test");
}
}
module.exports = DummyTest;
In Node.js:
const DummyTest = require("./src/utils/dummy")
var test = new DummyTest();
test.hello();
In React:
import {DummyTest} from './utils/dummy';
var test = new DummyTest();
test.hello();
Perfect, works both on Web and Node.js.
But if I try to create a build environment:
Attempted import error: 'DummyTest' is not exported from './utils/dummy'.
export default can not be used because it must works also on Node.js .
Use
import * as DummyTest from './utils/dummy
...or depending on your bundler's configuration, just require it like you would in Node.

Resources