Mocha can't find modules in imported code - javascript

I have a Phoenix app and am trying to get Mocha wired up for javascript unit tests. I want to test module 'Two', that imports module 'One' but I can't figure out how to configure Mocha to find module one.
Here is the test code:
web/static/js/one.js
export var One = 1;
web/static/js/two.js
import {One} from "web/static/js/one";
export var Two = function () {return One + One;}
> test/js/two_test.js
import assert from 'assert';
import {Two} from "../../web/static/js/two";
describe('Two()', function() {
it('returns value 2', function () {
assert.equal(2, Two());
});
});
Here is the output when I run npm test
home:~/elixir/optitrue$ npm test
> # test /home/jon/elixir/optitrue
> mocha --compilers js:babel-register test/js/**/*.js
module.js:457
throw err;
^
Error: Cannot find module 'web/static/js/one'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (two.js:1:1)
at Module._compile (module.js:556:32)
at loader (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:148:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:158:7)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (two_test.js:2:1)
at Module._compile (module.js:556:32)
at loader (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:148:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/jon/elixir/optitrue/node_modules/babel-register/lib/node.js:158:7)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at /home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (/home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:217:14)
at Mocha.run (/home/jon/elixir/optitrue/node_modules/mocha/lib/mocha.js:485:10)
at Object.<anonymous> (/home/jon/elixir/optitrue/node_modules/mocha/bin/_mocha:403:18)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
brunch.config
exports.config = {
// See http://brunch.io/#documentation for docs.
files: {
javascripts: {
joinTo: "js/app.js"
},
stylesheets: {
joinTo: "css/app.css"
},
templates: {
joinTo: "js/app.js"
}
},
conventions: {
assets: /^(web\/static\/assets)/
},
// Phoenix paths configuration
paths: {
watched: [
"web/static",
"test/static"
],
// Where to compile files to
public: "priv/static"
},
// Configure your plugins
plugins: {
babel: {
// Do not use ES6 compiler in vendor code
ignore: [/web\/static\/vendor/]
}
},
modules: {
autoRequire: {
"js/app.js": ["web/static/js/app"]
}
},
npm: {
enabled: true,
whitelist: ["phoenix", "phoenix_html"]
}
};

The error is quite clear:
Error: Cannot find module 'web/static/js/one'
Meaning the path you provided doesn't work, try using a relative paths:
import {One} from "./one";

Related

Discord.js v12: Error: The class/prototype returned from the extender function must extend the existing structure class/prototype

I am trying to set up a event handler for my bot. After I add the code and start the bot it outputs the error:
Error:
Error: The class/prototype returned from the extender function must extend the existing structure class/prototype (received function ExtendedTextChannel extends TextChannel; expected extension of ExtendedTextChannel).
at Function.extend (/home/runner/ricebot/node_modules/discord.js/src/util/Structures.js:82:13)
at module.exports (/home/runner/ricebot/node_modules/discord-buttons/src/index.js:24:16)
at Object.<anonymous> (/home/runner/ricebot/index.js:35:27)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/runner/ricebot/events/ready.js:1:16)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at /home/runner/ricebot/index.js:119:18
Code:
readdirSync("./events/").forEach((file) => {
const events = readdirSync("./events/").filter((file) =>
file.endsWith(".js")
);
for(let file of events) {
let pull = require(`./events/${file}`);
if(pull.name) {
client.events.set(pull.name, pull);
} else {
continue;
}
}
});
The error log leads me to this line 35
require('discord-buttons')(client); and to this line 119 let pull = require(`./events/${file}`)
This really seems confusing to me. Thanks in advance.

gulp.pipe() and src() not a function

var gulp = require('gulp');
var browserify = require('browserify');
var reactify = require('reactify');
gulp.task('react', function(){
console.log("gulp is runnung");
});
gulp.src('src/App.js')
.pipe(browserify({
insertGlobals: false,
debug: true,
transform: [reactify]
}))
.pipe(gulp.dest('./dist/js'));
gulp.task("watch", function(){
gulp.watch(["src/**/*.js"," src/**/*.jsx"], ["react"]);
})
gulp.task("default", ["watch"]);
module.exports = gulp;
New to Gulp not sure what to do here
I installed every dependency
gulp,
reactify and browserify
.pipe(gulp.dest('./dist/js'));
^
TypeError: gulp.src(...).pipe(...).pipe is not a function
at Object.<anonymous> (C:\Users\geilkea\Desktop\my-project\src\gulpfile.js:16:2)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at [eval]:1:12
at Script.runInThisContext (node:vm:132:18)
at Object.runInThisContext (node:vm:309:38)
This is the error im getting do I have to require something else?

How to fix TypeError: document.getElementById is not a function when running unit tests over files that use leaflet.js objects?

I have the exact same problem that is in this post How can you unit test Leaflet JS maps?
I was unable to comment or add additional questions to this post so I apologize for the duplicate question.
GLOBAL.window = { screen: {} };
GLOBAL.document = {
documentElement: {
style: {}
},
getElementsByTagName: function () { return []; },
createElement: function () { return {}; }
};
GLOBAL.navigator = {
userAgent: 'nodejs',
platform: 'nodejs'
};
GLOBAL.L = require('leaflet');
const assert = require('assert');
const config = require('../js/config.js')
config.js has the following in it.
const openstreetmap = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© OpenStreetMap contributors'
});
const map = L.map('map', {
center: [39, -105],
zoom: 4,
defaultExtentControl: true,
zoomControl: false,
layers: [openstreetmap]
});
I type mocha and get the following error:
TypeError: document.getElementById is not a function
at get (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:2247:43)
at NewClass._initContainer (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4094:37)
at NewClass.initialize (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:3131:8)
at new NewClass (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:301:20)
at Object.createMap [as map] (C:\dev\apps\APP\node_modules\leaflet\dist\leaflet-src.js:4718:9)
at Object. (C:\dev\apps\APP\resources\js\config.js:12:15)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object. (C:\dev\apps\APP\resources\test\config_test.js:45:16)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
at C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:334:36
at Array.forEach ()
at Mocha.loadFiles (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:331:14)
at Mocha.run (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:811:10)
at Object.exports.singleRun (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:108:16)
at exports.runMocha (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run-helpers.js:142:13)
at Object.exports.handler.argv [as handler] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\run.js:292:3)
at Object.runCommand (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\lib\command.js:242:26)
at Object.parseArgs [as _parseArgs] (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:1104:24)
at Object.parse (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\node_modules\yargs\yargs.js:566:25)
at Object.exports.main (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\lib\cli\cli.js:68:6)
at Object. (C:\Users\usr\AppData\Roaming\npm\node_modules\mocha\bin\mocha:154:29)
at Module._compile (internal/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

vm.runInContext: Why can't this function access a global object that should be in the context?

Using Node v10.13.0, I don't understand why this code errors out instead of printing "Bill":
const vm = require('vm');
function printName() {
console.log(defaultName);
}
const sandbox = { defaultName: 'Bill', printName: printName };
vm.createContext(sandbox);
vm.runInContext('printName()', sandbox);
When I run it, I get this:
$ node ./eval-js.js
/Users/wiseman/src/typescript-eval-test/eval-js.js:4
console.log(defaultName);
^
ReferenceError: defaultName is not defined
at printName (/Users/wiseman/src/typescript-eval-test/eval-js.js:4:17)
at evalmachine.<anonymous>:1:1
at Script.runInContext (vm.js:107:20)
at Object.runInContext (vm.js:285:6)
at Object.<anonymous> (/Users/wiseman/src/typescript-eval-test/eval-js.js:9:4)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)

Electron,nodejs : App wont start, cannot find app in one of the modules.

I am trying out an electron app which should point to our website, so I am trying one of the tutorials.. Unfortunately, when I start the app, I get the following error :
Error log :
akshay#akshay-mint-desktop ~/electron_tutorial_one $ npm start
> electron_tutorial_one#1.0.0 start /home/akshay/electron_tutorial_one
> electron .
App threw an error during load
Error: Cannot find module 'app'
at Module._resolveFilename (module.js:455:15)
at Function.Module._resolveFilename (/home/akshay/electron_tutorial_one/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/akshay/electron_tutorial_one/index.js:2:11)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'app'
at Module._resolveFilename (module.js:455:15)
at Function.Module._resolveFilename (/home/akshay/electron_tutorial_one/node_modules/electron-prebuilt/dist/resources/electron.asar/common/reset-search-paths.js:35:12)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/akshay/electron_tutorial_one/index.js:2:11)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
^Cakshay#akshay-mint-desktop ~/electron_tutorial_one $
index.js :
// Require the libraries used in the app
var app = require('app'),
BrowserWindow = require('browser-window');
require('crash-reporter').start();
var mainWindow = null;
// Kill the app when all windows are closed
app.on('mainWindow-all-closed', function() {
if (process.platform != 'darwin')
app.quit();
});
app.on('ready', function() {
// Create the main window for the app
mainWindow = new BrowserWindow({
"min-width" : 800,
"min-height" : 600,
fullscreen : true,
resizable : true,
"use-content-size" : true
});
// Load in our content
mainWindow.loadUrl('file://' + __dirname + '/index.html');
// This is required for now due to https://github.com/atom/electron/issues/1117
mainWindow.openDevTools();
mainWindow.closeDevTools();
// Ensure that garbage collection occurs when the window is closed
mainWindow.on('closed', function() {
mainWindow = null;
});
});
package.json :
{
"name": "electron_tutorial_one",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"devDependencies": {
"electron-prebuilt": "^1.4.13"
}
}
Instead of
// Require the libraries used in the app
var app = require('app'), BrowserWindow = require('browser-window');
you should use
const { app, BrowserWindow } = require('electron')
You've used a really old example. Electron modules are collected under 'electron' in 0.35.0 more then two years ago ;)
From release notes:
Add electron module which includes all public APIs.
You'd better use tutorials from electron docs:
https://www.electronjs.org/docs/tutorial/quick-start
https://github.com/electron/electron-quick-start

Categories

Resources