The following works from the coffeescript REPL but it doesn't work if I put it in a file and run it. It says that "a is not defined".
Coffee = require 'coffee-script'
a = {b: 1}
console.log Coffee.eval('a.b')
Any ideas how to get this to work as a script? I couldn't find any documentation on .eval.
Looking around at the source code, it appears we can use a sandbox option to set the context. This works:
Coffee = require 'coffee-script'
a = {b: 1}
console.log Coffee.eval('a.b', {sandbox:{a}})
Related
I love testing-library, have used it a lot in a React project, and I'm trying to use it in an Angular project now - but I've always struggled with the enormous error output, including the HTML text of the render. Not only is this not usually helpful (I couldn't find an element, here's the HTML where it isn't); but it gets truncated, often before the interesting line if you're running in debug mode.
I simply added it as a library alongside the standard Angular Karma+Jasmine setup.
I'm sure you could say the components I'm testing are too large if the HTML output causes my console window to spool for ages, but I have a lot of integration tests in Protractor, and they are SO SLOW :(.
I would say the best solution would be to use the configure method and pass a custom function for getElementError which does what you want.
You can read about configuration here: https://testing-library.com/docs/dom-testing-library/api-configuration
An example of this might look like:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
You can then put this in any single test file or use Jest's setupFiles or setupFilesAfterEnv config options to have it run globally.
I am assuming you running jest with rtl in your project.
I personally wouldn't turn it off as it's there to help us, but everyone has a way so if you have your reasons, then fair enough.
1. If you want to disable errors for a specific test, you can mock the console.error.
it('disable error example', () => {
const errorObject = console.error; //store the state of the object
console.error = jest.fn(); // mock the object
// code
//assertion (expect)
console.error = errorObject; // assign it back so you can use it in the next test
});
2. If you want to silence it for all the test, you could use the jest --silent CLI option. Check the docs
The above might even disable the DOM printing that is done by rtl, I am not sure as I haven't tried this, but if you look at the docs I linked, it says
"Prevent tests from printing messages through the console."
Now you almost certainly have everything disabled except the DOM recommendations if the above doesn't work. On that case you might look into react-testing-library's source code and find out what is used for those print statements. Is it a console.log? is it a console.warn? When you got that, just mock it out like option 1 above.
UPDATE
After some digging, I found out that all testing-library DOM printing is built on prettyDOM();
While prettyDOM() can't be disabled you can limit the number of lines to 0, and that would just give you the error message and three dots ... below the message.
Here is an example printout, I messed around with:
TestingLibraryElementError: Unable to find an element with the text: Hello ther. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
...
All you need to do is to pass in an environment variable before executing your test suite, so for example with an npm script it would look like:
DEBUG_PRINT_LIMIT=0 npm run test
Here is the doc
UPDATE 2:
As per the OP's FR on github this can also be achieved without injecting in a global variable to limit the PrettyDOM line output (in case if it's used elsewhere). The getElementError config option need to be changed:
dom-testing-library/src/config.js
// called when getBy* queries fail. (message, container) => Error
getElementError(message, container) {
const error = new Error(
[message, prettyDOM(container)].filter(Boolean).join('\n\n'),
)
error.name = 'TestingLibraryElementError'
return error
},
The callstack can also be removed
You can change how the message is built by setting the DOM testing library message building function with config. In my Angular project I added this to test.js:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
This was answered here: https://github.com/testing-library/dom-testing-library/issues/773 by https://github.com/wyze.
How do we write document level coffeescript in jade-handlebars ?? In jade file I used following
template(name="hellow")
:coffeescript
alert('helloworld')
But it gives error. Help me out guys
ERROR
Error: Cannot find module 'coffee-script'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at module.exports.coffeescript (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/filters.js:93:14)
at Object.Compiler.visitFilter (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:419:28)
at Object.Compiler.visitNode (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:176:32)
at Object.Compiler.visit (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:161:10)
at Object.Compiler.visitBlock (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:253:12)
at Object.Compiler.visitNode (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:176:32)
at Object.Compiler.visit (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:161:10)
at Object.Compiler.visitTag (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:390:12)
at Object.Compiler.visitNode (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:176:32)
at Object.Compiler.visit (/home/rohan/.meteorite/packages/jade-handlebars/jrhone/meteor-jade-handlebars/e442fecedeac64b81a0865e1b195a630181a3ee4/jade/lib/compiler.js:161:10)
Thank You in advance!!!!!
Even if you could get this to compile, I'm not sure it will do anything because of the way meteor dynamically injects elements into the body. For example if you add:
script(type='text/javascript') alert('hello!')
to your jade, or the equivalent:
<script type="text/javascript">alert('hello!')</script>
to your html, it won't actually execute - at least it doesn't for me. I think this is because the body has already rendered before the script was injected onto the page.
That aside, I did get this to compile, but not using jade-handlebars. See my answer here.
I'm new to meteor and coffeescript. I'm using the file layout suggested in the Unofficial Meteor FAQ. In file collections/C.coffee, I have
C = new Meteor.Collection 'C'
console.log "C: #{C}"
In file server/main.coffee, I have
C.insert {test: 'test'}
When I start meteor, I see on the console:
C: [object Object]
ReferenceError: C is not defined
at app/server/main.coffee.js:5:1
at /home/xxx/yyy/.meteor/local/build/server/server.js:298:12
How do I make C available in files outside of collections/C.coffee?
Update: Adding # to C fixes the problem at the top level. However it still fails with:
Meteor.methods
test: (statement) ->
#C.insert {test: 'test'}
It fails with an error TypeError: Cannot call method 'insert' of undefined
To make C visible outside the file it was defined in use #, which compiles to this. or window. in js, which gives it the same effect as a global scope:
#C = new Meteor.Collection 'C'
I have the following ArrayController:
Lead.Controllers.Leads = Ember.ArrayController.extend
init: ->
content: Ember.A()
#view = Ember.View.create
controller: #
templateName: 'app/templates/leads/list'
#view.appendTo $('#leads')
addLead: (data) ->
lead = Lead.Lead.create()
lead.setProperties JSON.parse data
console.log lead.get 'company'
debugger
#pushObject lead
console.log #get('length')
The problem is after I call push object, the length is still 0. I really cannot see what I am doing wrong.
Can anyone see what I am doing wrong? The only thing I can think of is that the Content is set to an empty array via Ember.A().
I have no idea what else it could be.
I'm not quite sure where your problem is since I a) don't really know or use CoffeeScript and b) there's no jsFiddle or working example. But if I'm reading this correctly your trying to do the following: See this jsFiddle which works as expected. Hope that points you in the right direction.
It's a Coffee Script syntax error.
There are two solutions (depending on what you want to implement).
The second example will use the same array for every instance of the controller.
Also, I'd recommend calling #_super() when overriding the init method, otherwise you might get some unexpected results with certain classes.
Ember.ArrayController.extend
init: ->
#_super()
#set 'content', Ember.A()
# content
Ember.ArrayController.extend
content: Ember.A()
init: ->
#_super()
# content
I get the following error:
TypeError: Cannot read property 'name' of undefined
at new SchemaArray (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema/array.js:40:22)
at Function.interpretAsType (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema.js:201:12)
at Schema.path (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema.js:162:29)
at Schema.add (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema.js:110:12)
at Schema.add (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema.js:106:14)
at new Schema (/home/campaigns/scheduler/tests/node_modules/mongoose/lib/schema.js:38:10)
at Object. (/home/campaigns/scheduler/tests/testSchedulerModel.coffee:12:21)
at Object. (/home/campaigns/scheduler/tests/testSchedulerModel.coffee:177:4)
at Module._compile (module.js:402:26)
at Object.run (/usr/local/lib/node_modules/coffee-script/lib/coffee-script.js:57:25)
However, on line 12 of testSchedulerModel.coffee there is no reference to 'name'. Here is what line 12 looks like:
ObjectId = Schema.ObjectId
So how should I understand this error?
Thank you,
Igor
Sigh. Unfortunately, while you'd think testSchedulerModel.coffee:12 would mean "line 12 of testSchedulerModel.coffee, it actually means "line 12 of the JavaScript that testSchedulerModel.coffee is compiled to. The problem is that there's currently no way to trace errors back to the original CoffeeScript (at least not under Node).
So, you'll have to compile testSchedulerModel.coffee as JS and see what line 12 is there.
Better debugging tools for CoffeeScript are coming, but for now, it's probably best that you set up a Cakefile that compiles your code to JS before running it, to avoid such confusion.
By the way, there is an open issue regarding those .coffee filenames in the stack trace: issue 987.
Trevor: Kudos for the tip!
I solved my issue. It turns out that I cannot have objects inside of arrays when defining my schema in Mongoose. I was doing it like this:
AdUnitSchema = new mongoose.Schema
venue: [
{
id: ObjectId,
name: String
}
],
geotarget: [
{
id: ObjectId,
name: String
}
]
But in actuality, I had to create separate schemas, and reference them like this:
AdUnitSchema = new mongoose.Schema
venue:
[VenueSchema]
geotarget:
[GeotargetSchema]