mocking Phaser with Jest - jestjs

I've been trying to set up jest as a test framework for a project I'm working on that uses Phaser, and I am getting stuck trying to mock out Phaser itself. I first ran into this issue with the missing canvas, which I was able to resolve from the link. But now I am getting another error "Cannot read property 'postion' of undefined".
jest.config.js:
module.exports = {
verbose: true,
roots: ['./src'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
moduleNameMapper: {
'\\.(css|less|scss)$': 'identity-obj-proxy'
},
setupFiles: ['jest-canvas-mock']
}
__mocks__/phaser.js:
const phaser = jest.genMockFromModule('phaser');
module.exports = phaser;
error message:
TypeError: Cannot read property 'position' of undefined
1 |
> 2 | const phaser = jest.genMockFromModule('phaser');
| ^
3 |
4 | module.exports = phaser;
at Image.get [as x] (node_modules/phaser/src/physics/matter-js/components/Transform.js:36:30)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at Array.forEach (<anonymous>)
at Object.<anonymous> (src/__mocks__/phaser.js:2:21)
at Object.<anonymous> (src/main.ts:3:1)
at Object.<anonymous> (src/main.spec.ts:3:1)
I'm looking at that Transform file and its blowing up on a getter because this.body is undefined:
get: function ()
{
return this.body.position.x;
},
Has anyone else had this problem? I'm hoping I just have some configuration wrong.

I wanted to thank you for sharing your configuration to run Jest with Phaser 3.
Regarding your issue, it appears that the Phaser.Physics.Matter.Commponents.Trasnform component is not really compatible with the way jest.genMockFromModule() works.
You can hack it simply by creating a default value for the body property of the Transform instance in phaser/src/physics/matter-js/components/Trasnform.js:
var Transform = {
body: Body.create({}),
// ...
Although I don't know how it might affect the production code.
If you don't use the Matter.js physics engine in your game, maybe you could create a custom Phaser 3 build instead.

Related

Error after upgrading Joi to latest version - Schema can only contain plain objects (name)

After upgrading Joi to the latest version #hapi/Joi(17.1.1) my server is not staring I am getting below error on startup. Seems there were some breaking changes in recent versions. Not able to get any clue yet, any help is appreciated.
Error: Schema can only contain plain objects (name)
at new module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/error.js:23:19)
at module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/assert.js:20:11)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:88:5)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/base.js:631:24)
at /Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:92
at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/common.js:173:16)
at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:32)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:90:25)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:117:24)
at Object.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/index.js:123:24)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/validation.js:49:22)
at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:197:43)
at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:122:14)
at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:498:23)
at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:491:22)
at /Users/xyz/project/projectxyz/src/app.js:73:14
at Array.forEach (<anonymous>)
at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
path: 'name'
} Server Init error Error: Schema can only contain plain objects (name)
at new module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/error.js:23:19)
at module.exports (/Users/xyz/project/projectxyz/node_modules/#hapi/hoek/lib/assert.js:20:11)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:88:5)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at internals.Base.$_compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/base.js:631:24)
at /Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:92
at Object.exports.tryWithPath (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/common.js:173:16)
at internals.Base.method [as keys] (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/types/keys.js:255:32)
at Object.internals.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:90:25)
at Object.exports.schema (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:17:26)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/compile.js:117:24)
at Object.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/joi/lib/index.js:123:24)
at Object.exports.compile (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/validation.js:49:22)
at module.exports.internals.Route._setupValidation (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:197:43)
at new module.exports.internals.Route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/route.js:122:14)
at internals.Server._addRoute (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:498:23)
at internals.Server.route (/Users/xyz/project/projectxyz/node_modules/#hapi/hapi/lib/server.js:491:22)
at /Users/xyz/project/projectxyz/src/app.js:73:14
at Array.forEach (<anonymous>)
at init (/Users/xyz/project/projectxyz/src/app.js:72:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:66:3) {
path: 'name'
}
To anyone who finds themselves in this dark place again, for me it was an issue when extending a Joi schema using the spread operator:
joi.object().keys({
...otherSchema,
position: joi.number()
})
This was causing the "Schema can only contain plain objects" error when I upgraded to Joi 17.5.0. Using the keys() syntax got around it:
otherSchema.keys({
position: joi.number()
})
Thanks all for your comments. I was able to resolve it after going through Joi's release notes.
The problem was due to the mix of Joi versions. In my codebase older version of Joi was used which was causing this. Below Github issue helped me realizing the issue.
https://github.com/hapijs/joi/issues/1913
i think u should do some thing like this to handle this error:
var ObjectJoi = Joi.object({
//some item that you want to add
})
var mySchema = new Schema(Joigoose.convert(ObjectJoi))
const myModel = mongoose.model('myModel', mySchema);
It's hard to tell without seeing the code.
But I faced the same issue when using Joi.extend().
const extendedType = Joi.extend(Joi => {
return {
type: 'myType', // <- PAY ATTENTION
base: Joi.array(),
messages: {
'stringArray:base': '...'
},
validate(value, helpers) {
...
},
coerce(value, helpers) {
...
}
}
});
The problem was that I used extendedType like below by mistake:
Joi.object({
someKey: extendedType,
...
});
instead of:
Joi.object({
someKey: extendedType.myType(),
...
});

How to remove TypeError: "x" is not a function?

I am trying to learn how to make a discord bot and pull data from this API called Ergast (http://ergast.com/mrd). I found this npm (https://github.com/estevE11/f1-stats) which uses NodeJS implementation to get a historical record of F1 data from Ergast API. Sorry for the bad wording I am still trying to learn the lingo.
I followed what was stated in the npm documentation for installing it and tried using the example to get data from the API. However when I run the code in index.js I get the error "TypeError: "x" is not a function". When I go into the node_modules "f1-stats" folder and run the code from main.js I do get the correct result.
index.js:
const client = new Discord.Client(); //This will be our client
const { prefix, token } = require('./config.json');//const PREFIX = '!';
const f1s = require('f1-stats');
//module.exports.f1s = f1s; //Still causes the TypeError
f1s("2008 drivers", (res) => {
console.log(res);
});
The error message I get in index.js:
f1s("2008 drivers", (res) => {
^
TypeError: f1s is not a function
at Object.<anonymous> (C:\Users\RyanPC\Documents\DiscordBot\index.js:8:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
node_modules/f1-stats/main.js:
const f1s = require("./f1-stats"); // "./" is used because module is located in the same folder as the Node.js file
f1s("2008 drivers", (res) => {
console.log(res);
});
when I run it in node_modules/f1-stats/main.js:
{ MRData:
{ xmlns: 'http://ergast.com/mrd/1.4',
series: 'f1',
url: 'http://ergast.com/api/f1/2008/drivers.json',
limit: '30',
offset: '0',
total: '22',
DriverTable: { season: '2008', Drivers: [Array] } } }
Because f1-stats doesn't export anything so when you import it. It is empty. The correct file you need to import is f1-stats/f1-stats.
const f1s = require('f1-stats/f1-stats');
try importing f1s properly , you are getting error because you have not imported the function properly , in other words...check what is being exported from what are you trying to import....hope it solves the problem.

vue/test-utils: Add localVue and stubs.transition to mount() but throw error like 'Cannot read property '_transitionClasses' of undefined'

"element-ui": "2.4.6"
"#vue/test-utils": "^1.0.0-beta.29"
I want to bind Element-ui to mount so at the beginning i wirite as:
import { mount, createLocalVue, shallowMount, config } from '#vue/test-utils'
import Element from 'element-ui'
const localVue = createLocalVue()
localVue.use(Element)
And my primary code:
const wrapper = mount(List, {
localVue
})
setTimeout(() => {
expect(wrapper.contains(OperationColumn)).toBe(true)
done()
})
And after running i got:
TypeError: Cannot read property '$el' of undefined
at VueComponent.mounted (F:\workspace\HaiShangEdit\node_modules\element-ui\lib\select.js:1280:54)
at invokeWithErrorHandling (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:1843:57)
at callHook (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:4157:7)
at Object.insert (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:3108:7)
at invokeInsertHook (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:6266:28)
at VueComponent.patch [as __patch__] (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:6483:5)
at VueComponent.Vue._update (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:3886:19)
at VueComponent.updateComponent (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:4007:10)
at Watcher.get (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:4405:25)
at new Watcher (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:4394:12)
at mountComponent (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:4014:3)
at VueComponent.Object.<anonymous>.Vue.$mount (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:8327:10)
at mount (F:\workspace\HaiShangEdit\node_modules\#vue\test-utils\dist\vue-test-utils.js:8649:21)
at Object.<anonymous> (F:\workspace\HaiShangEdit\test\pictureLibrary\list.spec.js:45:22)
at Object.asyncJestTest (F:\workspace\HaiShangEdit\node_modules\jest-jasmine2\build\jasmine_async.js:108:37)
at resolve (F:\workspace\HaiShangEdit\node_modules\jest-jasmine2\build\queue_runner.js:56:12)
at new Promise (<anonymous>)
at mapper (F:\workspace\HaiShangEdit\node_modules\jest-jasmine2\build\queue_runner.js:43:19)
at promise.then (F:\workspace\HaiShangEdit\node_modules\jest-jasmine2\build\queue_runner.js:87:41)
at process._tickCallback (internal/process/next_tick.js:68:7)
console.error node_modules/#vue/test-utils/dist/vue-test-utils.js:1421
[vue-test-utils]: update has been removed from vue-test-utils. All updates are now synchronous by default
And after google i find it was a bug from Element-ui so i change my part code like:
const wrapper2 = mount(List, {
localVue,
sync: true,
stubs: {
transition: false
}
})
But it still failed with more errors:
[Vue warn]: Error in callback for watcher "options": "TypeError: Cannot read property 'querySelectorAll' of undefined"
TypeError: Cannot read property 'querySelectorAll' of undefined
at VueComponent.options (F:\workspace\HaiShangEdit\node_modules\element-ui\lib\element-ui.common.js:8577:29)
TypeError: Cannot set property 'documentHandler' of undefined
at update (F:\workspace\HaiShangEdit\node_modules\element-ui\lib\utils\clickoutside.js:64:29)
[Vue warn]: Error in created hook: "TypeError: Cannot read property '_transitionClasses' of undefined"
TypeError: Cannot read property '_transitionClasses' of undefined
at Array.updateClass (F:\workspace\HaiShangEdit\node_modules\vue\dist\vue.runtime.common.dev.js:6733:28)
I'm quite confused about vue/test-utils cause i'm a new learner,so it would be grateful if anyone can help me solve this problem.By the way, should i learn jest before i start to write vue unit test with vue/test-utils?

Node.js with ExpressJS error: Cannot read property 'prototype' of undefined

Running node.js v0.10.2 and express v3.1.1 (latest at this time) and getting this error:
/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
^
TypeError: Cannot read property 'prototype' of undefined
at create (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10:26)
at /root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9503:18
at Object.<anonymous> (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9505:2)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/root/dmr-addresses/address/log.js:1:71)
line 1 of log.js is:
var $ = require('jquery');
I've tried running npm install jquery but it has not fixed the problem.
Check this:
Same error here...
I don't know what I'm doing, but I changed the node-jquery.js fourth-fifth row's and it's start working :)
old:
if(window == null ) {
window = require('jsdom').jsdom().createWindow();
new:
if(!window || !window.document) {
window = require('jsdom').createWindow();
window.document = require('jsdom').jsdom();
You don't actually have a prototype object in Node server code, it's all stored in the much nicer __proto__ object and you should be using Object.create/defineProperty.
What exactly are you trying to do? Run an ajax query with Node? If so, you should be using Nodes http.request
An example could be:
require('request').post({
"uri" : "http://example.com/",
"headers" : {
'content-type': 'application/json'
},
"body" : "hello=world"
},
function(e,r,b){
// e = errors, r = response and b = returned body
console.log(b,r.statusCode));
});
Looks like this is an issue with the jsdom module that node-jquery depends on. It appears that this is a known issue, and that it has been fixed, but not published to npm yet.
Check it out: https://github.com/coolaj86/node-jquery/issues/52

Creating temporary view in couchdb using node-couchdb-api

I am trying to connect couchdb using node-couchdb-api at nodejs level as mentioned in the following link http://dominicbarnes.us/node-couchdb-api/.My couchdb version is 1.1.1 and nodejs version is 0.6.10.
For creating temporary view as mentioned in api http://dominicbarnes.us/node-couchdb-api/api/database/tempView.html I have written the following code.
var couchdb = require("couchdb-api");
var server = couchdb.srv(localhost, 5984, false, false);
var db = server.db("test");
var map = function (doc) {
emit(null, 1);
};
var reduce = "_sum";
var query = { include_docs: true };
db.tempView(map, reduce, query, function (err, response) {
console.log(response);
});
But i am facing the following problem.
C:\Program Files\nodejs\node_modules\couchdb-api>node server.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot read property '0' of null
at C:\Program Files\nodejs\node_modules\couchdb-api\lib\util.js:24:39
at Array.map (native)
at Object.formatFunction (C:\Program Files\nodejs\node_modules\couchdb-api\lib\util.js:22:25)
at Object.tempView (C:\Program Files\nodejs\node_modules\couchdb-api\lib\database.js:285:28)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\couchdb-api\server.js:27:4)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
please suggest me to resolve the issue.
Thanks in advance.
sorry about the problem you were experiencing. I'm the creator of that module, and I've just pushed version 1.1.2 up to NPM which addresses your problem. (and includes a unit test to make sure it doesn't happen again)
Just update to the latest version via npm update couchdb-api and you should be set to go. Let me know if you have further issues.

Resources