I'm trying to use o.spec to test mycomponent but failed.
The error message of "dart test" is:
tests/MyComponent.js:1
(function (exports, require, module, __filename, __dirname) { import MyComponent from "./src/mycomponent"
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Glob.<anonymous> (node_modules/mithril/ospec/bin/ospec:37:37)
error Command failed with exit code 1.
setup by npm or yarn:
yarn add mithril#next webpack webpack-cli
edit package.json:
{
...
"name": "my-project",
"scripts": {
"start": "webpack src/index.js --output bin/app.js -d --watch",
"build": "webpack src/index.js --output bin/app.js -p",
"test": "ospec"
}
}
index.html:
<!DOCTYPE html>
<body>
<script src="bin/app.js"></script>
</body>
src/mycomponent.js:
//var m = require("mithril")
import m from "mithril"
//module.exports = {
export default {
view: function(vnode) {
return m("div",
m("p", "Hello World")
)
}
}
src/index.js:
import m from "mithril"
import MyComponent from "./mycomponent"
//var m = require("mithril")
//var MyComponent = require("./mycomponent")
m.mount(document.body, MyComponent)
tests/MyComponent.js
import MyComponent from "./src/mycomponent"
//var MyComponent = require("./src/mycomponent")
o.spec("MyComponent", function() {
o("returns a div", function() {
var vnode = MyComponent.view()
o(vnode.tag).equals("div")
o(vnode.children.length).equals(1)
o(vnode.children[0].tag).equals("p")
o(vnode.children[0].children).equals("Hello world")
})
})
You need to mock the environment using
global.window = require("mithril/test-utils/browserMock.js")();
global.document = window.document;
in your tests.
Check more at https://mithril.js.org/testing.html
Related
I am newbie on Jest and I have the following issue when I execute the command
npm test
I have the following files
functions.js
const functions = {
add: (num1,num2) => num1 + num2
};
module.exports = functions;
and the functions.test.js
const functions = require('./functions');
test(`Adds 2 + 2 to equal 4`, ()=>{
expect(functions.add(2,2)).toBe(4);
});
my package.json has the following context
{
"name": "jest-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^29.2.2"
}
}
I havent found a lot of resources on the internet regarding the error I just try to remove the dot (.) character from the node_modules/jest
and the output of the console (error) is below
> jest-tutorial#1.0.0 test
> jest
C:\Users\agiallelis\node_modules\jest-cli\build\run.js:129
if (error?.stack) {
^
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\agiallelis\node_modules\jest-cli\build\index.js:12:12)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
I am trying to create a cli tool to make a todo list. For some reason I can't figure out I'm unable to use either require or import when trying to import the Chalk package for highlighting terminal outputs
here is what I have for my index.js file
#! /usr/bin/env node
const { program } = require("commander");
const list = require("./commands/list.js");
program.command("list").description("List all the TODO tasks").action(list);
program.parse();
Here is my list.js file
#! /usr/bin/env node
const conf = new (require("conf"))();
const chalk = require("chalk");
function list() {
const todoList = conf.get("todo-list");
if (todoList && todoList.length) {
console.log(
chalk.blue.bold(
"Tasks in green are done. Tasks in yellow are still not done."
)
);
todoList.forEach((task, index) => {
if (task.done) {
console.log(chalk.greenBright(`${index}. ${task.text}`));
} else {
console.log(chalk.yellowBright(`${index}. ${task.text}`));
}
});
} else {
console.log(chalk.red.bold("You don't have any tasks yet."));
}
}
module.exports = list;
and my package.json file
{
"name": "near-clear-state",
"version": "1.0.0",
"description": "Tool to let NEAR users clear the state of their account ",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "commonjs",
"author": "Dorian",
"license": "ISC",
"dependencies": {
"chalk": "^5.0.0",
"commander": "^8.3.0",
"conf": "^10.1.1",
"near-api-js": "^0.44.2"
},
"bin": {
"near-clear-state": "./index.js"
}
}
when I try running anything from this cli tool I'm making I get this error if I use require
➜ near-clear-state near-clear-state --help
/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:4
const chalk = require("chalk");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/node_modules/chalk/source/index.js from /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js not supported.
Instead change the require of index.js in /Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:4:15)
at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/index.js:3:14) {
code: 'ERR_REQUIRE_ESM'
}
Or this error when i use import
/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/commands/list.js:3
import { Chalk } from "chalk";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1026:15)
at Module._compile (node:internal/modules/cjs/loader:1061:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/doriankinoocrutcher/Documents/NEAR/Developer/near-clear-state/index.js:3:14)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
Node.js v17.4.0
Please help me
Use the import syntax, and add "type": "module" into your package.json to allow for ES6 imports.
I am new to javascript and I am having import issues.
I have installed the jquery.csv lib as npm i jquery.csv but then node is not able to import it. (this was the procedure described in https://github.com/typeiii/jquery-csv)
Here is the structure of my project:
lucapuggini#lucas-MBP js_utils % ls
index.js index.js~ node_modules package-lock.json package.json
lucapuggini#lucas-MBP js_utils % ls node_modules
jquery-csv
lucapuggini#lucas-MBP js_utils % cat index.js
var csv = require('./jquery.csv.js');
console.log("Start index.js")
lucapuggini#lucas-MBP js_utils % cat package-lock.json
{
"name": "js_utils",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"jquery-csv": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/jquery-csv/-/jquery-csv-1.0.11.tgz",
"integrity": "sha512-KDPc3wFLTFO68p/4IqsODZCjBp+y9axDa/pr36pDKrWk6yyHf8Nk1FqAGXvaUb6H7J1zJSYszABIFj0a40QXRA=="
}
}
}
lucapuggini#lucas-MBP js_utils % node index.js
node:internal/modules/cjs/loader:922
throw err;
^
Error: Cannot find module './jquery.csv.js'
Require stack:
- /Users/lucapuggini/ProgrammingProjects/padel/trunk/js_utils/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:15)
at Function.Module._load (node:internal/modules/cjs/loader:763:27)
at Module.require (node:internal/modules/cjs/loader:991:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/Users/lucapuggini/ProgrammingProjects/padel/trunk/js_utils/index.js:2:11)
at Module._compile (node:internal/modules/cjs/loader:1102:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1131:10)
at Module.load (node:internal/modules/cjs/loader:967:32)
at Function.Module._load (node:internal/modules/cjs/loader:807:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/lucapuggini/ProgrammingProjects/padel/trunk/js_utils/index.js'
]
}
lucapuggini#lucas-MBP js_utils %
Why am I getting this error?
It seems their documentation is incorrect. You need to require the npm module like this:
const csv = require('jquery-csv');
I am getting "Unexpected token import" when trying to run my react app
error that I am getting
User:my-version username$ node build
User:my-version username$ node app.js
/Users/Hindreen/Documents/workspace/apps/test/app.js:1
(function (exports, require, module, __filename, __dirname) { import React
from 'react';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:588:28)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
User:test username$
app.js
import React from 'react';
import ReactDOM from 'react-dom';
class Application extends React.Component {
render() {
return (
<div className="container">
<h1>Hello From React</h1>
</div>
);
}
}
build.js
var fs = require("fs");
var browserify = require("browserify");
var babelify = require("babelify");
browserify({ debug: true })
.transform(babelify)
.require("./app.js", { entry: true })
.bundle()
.on("error", function (err) { console.log("Error: " + err.message); })
.pipe(fs.createWriteStream("bundle.js"));
.babelrc
{
"presets": ["env", "react"],
"plugins": ["transform-es2015-modules-amd"]
}
package.json devDependencies
{
"devDependencies":{
"babel-plugin-transform-es2015-modules-amd": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.24.1",
"babelify": "^7.3.0",
"browserify": "^13.3.0",
"react": "^15.0.2",
"react-dom": "^15.0.2"
}
}
I appreciate any help, Thanks in advance and please let me know if my question is not clear.
const React = require('react'),
ReactDOM = require('react-dom');
if you write in ES6-syntax => install and use babel
-> User:my-version username$ node app.js
Unless you want to run your code server side, this is not the appropriate way to bootstrapping your 'build' code.
Please refer to the following which mimics your setup: https://repl.it/repls/RemoteJovialSignature
(it doesn't actually run, it's only for viewing purposes).
Steps to run locally:
node build - builds the bundle.js
Open index.html in your browser
I just solved saving my script file text.js back to UTF from Unicode. Probably the single-quote (') in Unicode was not recognized.
It was failing in line
var mysql = require('mysql');
I am a newbie in JS and I have some troubles running the test server (like: node app.js ..or iojs app.js) with the new ECMA6 syntax (using import and export)... here is the sample code I am using trying to achive that and the error the server is returning :
<script>
System.config({
baseURL:"/",
transpiler: "babel",
babelOptions: {
"optional": [
"runtime"
]
},
paths: {
"npm:*": "node_modules/*",
"bower:*": "bower_components/*"
},
map: {
"babel": "npm:babel-core/browser.js",
"sammy": "bower:sammy/lib/sammy.js",
"jquery":"bower:jquery/dist/jquery.js",
"handlebars": "bower:handlebars/handlebars.js"
}
});
</script>
<script type="text/javascript">
System.import('app.js')
.then(function(module) {
module.init('#content');
});
</script>
and in app.js the following simple code :
import $ from 'jquery';
import sammy from 'sammy';
import homeController from './controllers/homeController.js';
export function init (element) {
var sammyApp = Sammy(element, function() {
this.get('#/', homeController.load);
});
sammyApp.run('#/');
}
then o recive the following error screen when trying to execute the test server:
D:\TELERIK\JavaScript-Apps\exam-prepar-evlogi>iojs app.js
D:\TELERIK\JavaScript-Apps\exam-prepar-evlogi\app.js:1
(function (exports, require, module, __filename, __dirname) { import $ from 'j
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:448:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:471:10)
at startup (node.js:117:18)
at node.js:948:3
Any advice on where my fault is would be great.. Thank you all in advance