mercredi 31 octobre 2018

jest typeError in pure javascript nodejs

I cannot make it work without an error and am requesting help. The issue is the following: Whenever I start jest with --watchAll it causes this error:

TypeError: Cannot read property 'Object.<anonymous>' of null
        at Runtime._execModule (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/jest-runtime/build/index.js:447:72)
        at Runtime.requireModule (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/jest-runtime/build/index.js:265:14)
        at Runtime.requireModuleOrMock (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/jest-runtime/build/index.js:337:19)
        at Object.factory (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/mathjs/lib/function/arithmetic/divide.js:6:27)
        at load (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/mathjs/lib/core/core.js:106:28)
        at resolver (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/mathjs/lib/core/function/import.js:220:24)
        at Object.get [as divide] (/home/timmy/react/yogist/yogist_chatbot_server/node_modules/mathjs/lib/utils/object.js:210:20)
        at getScoreMatrix (/home/timmy/react/yogist/yogist_chatbot_server/controllers/recommendation_engine.js:38:29)
        at /home/timmy/react/yogist/yogist_chatbot_server/controllers/chatbot.js:110:61
        at <anonymous>

I found out that it disappears when I either: Remove my

matrix = mathjs.divide(matrix, mathjs.norm(matrix, "fro"));

Line out of my code.

OR

add a setTimeout with roughly 200ms delay in the jest test

OR

run jest with out watchALL or watch flag.

it also appears when I run npx jest. (so without watch flag)

It only appears when testing with jest, code works seamlessly (it also works with this error, test succeeds).

This seems to be relevant: 5205 but I could not understand the solution properly. Can somebody explain again please?

here is the jest test code:

import request from "supertest";
import Server from "../index";
import path from "path";
import _ from "lodash";

const chai = require("chai");
const expect = chai.expect;

const API_BASE_PATH = "localhost:3000/";

describe("testing userActions", () => {
  beforeAll(done => {
    setTimeout(() => {
      done();
    }, 1);
  });
  afterAll(() => {
    Server.close();
  });
  describe("GET user/getSubscription", () => {
    it("should return 200 OK.", done => {
      return request(Server)
        .post(API_BASE_PATH + "user/getSubscription")
        .send({
          headers: {
            authorization: ""
          }
        })
        .end((err, res) => {
          console.log("this is the error:", res.data);
          if (!err && res.body) {
            console.log("values are:", res.body);
            done("");
          } else {
            done(err);
          }
        });
    });
  });
});

here is the relevant part of my package.json:

...    "dependencies": {
        "babel-cli": "^6.26.0",
        "babel-core": "^6.26.0",
        "babel-jest": "^23.0.6",
        "babel-loader": "^7.1.4",
        "babel-preset-es2015": "^6.24.1",
        "bcrypt-nodejs": "^0.0.3",
        "body-parser": "^1.18.2",
        "config": "^1.30.0",
        "cors": "^2.8.4",
        "express": "^4.16.3",
        "fs": "^0.0.1-security",
        "hash.js": "^1.1.5",
        "jquery-csv": "^0.8.9",
        "jwt-simple": "^0.5.1",
        "kuker-emitters": "^6.7.4",
        "lodash": "^4.17.10",
        "mathjs": "^5.2.3",
        "moment": "^2.22.1",
        "mongoose": "^5.0.10",
        "mongoose-moment": "^0.1.3",
        "mongoose-type-url": "^1.0.2",
        "morgan": "^1.9.0",
        "node-schedule": "^1.3.0",
        "nodemailer": "^4.6.4",
        "nodemon": "^1.17.2",
        "npx": "^10.2.0",
        "passport": "^0.4.0",
        "passport-jwt": "^4.0.0",
        "passport-local": "^1.0.0",
        "path": "^0.12.7",
        "random-token": "^0.0.8",
        "require": "^2.4.20",
        "socket.io": "^2.0.4",
        "socket.io-redis": "^5.2.0",
        "stent": "^5.1.0",
        "stripe": "^6.1.0",
        "uuid": "^3.2.1",
        "winston": "^3.0.0"
      },
      "devDependencies": {
        "chai": "^4.1.2",
        "jest": "^23.6.0",
        "jest-cli": "^22.0.4",
        "supertest": "^3.0.0",
        "webpack": "^4.23.1",
        "webpack-cli": "^3.1.2",
        "webpack-node-externals": "^1.7.2"
      }, ...

Aucun commentaire:

Enregistrer un commentaire