jbrumwell / mock-knex

A mock knex adapter for simulating a database during testing
MIT License
239 stars 71 forks source link

Cannot find module 'knex/package' from 'index.js' #70

Closed hassankhan closed 5 years ago

hassankhan commented 7 years ago

Hi there,

Thanks for the awesome package! Unfortunately I can't seem to get it to work. I'm using jest@^20.0.0, knex@^0.13.0 and mock-knex@^0.3.9 and typescript@^2.3.3:

Jest test case ```js import * as Knex from "knex"; import * as MockKnex from "mock-knex"; import RedshiftAdapter from "../../src/adapters/RedshiftAdapter"; import BaseQuery from "../../src/queries/BaseQuery"; class MockQuery extends BaseQuery { constructor(params) { super(params); } public execute = (client) => { return client("logs").select("*").limit(5); } } describe("RedshiftAdapter", () => { let adapter: RedshiftAdapter; let client: MockKnex; let tracker; beforeAll(() => { client = Knex({ acquireConnectionTimeout: 2000, client: "pg", connection: { host : "host", password : "password", port : 1000, user : "user", database : "database", ssl : true, }, debug: true, }); MockKnex.mock(client); tracker = MockKnex.getTracker(); adapter = new RedshiftAdapter(client); }); beforeEach(() => { tracker.install(); }); afterAll(() => { MockKnex.unmock(client); }); afterEach(() => { tracker.uninstall(); }); it("can make a request to Redshift", () => { tracker.on("query", (query) => { expect(query.method).toEqual("select"); query.response([ { action : "get", resource : "Apps", }, { action : "get", resource : "Users", }, { action : "get", resource : "AppUsers", }, ]); }); return adapter.execute(new MockQuery({})) .then((result) => { console.log(result); }); }); }); ```
jbrumwell commented 7 years ago

Could this be a typescript issue with loading json files? https://hackernoon.com/import-json-into-typescript-8d465beded79 I have confirmed that package.json is bundled in knex 13 and can't reproduce this error locally.

hassankhan commented 7 years ago

Thanks for the speedy reply 😄!

I've done as the link said, still no luck 😢 . I'm not quite sure where the problem could be. I was hoping someone else had already done this and might see the issue.

EDIT: It might be ts-jest, which is used to transpile TypeScript before running in Jest, I'll update if I find anything else.

jbrumwell commented 7 years ago

Could path mapping help? https://www.typescriptlang.org/docs/handbook/module-resolution.html

hassankhan commented 7 years ago

I found a way to get it to work (somewhat), I changed this line to the following:

import knexPackage from 'knex/package.json';

I'm going to look into why it thinks I'm using Knex v0.8 (I'm using v0.13). Also I had trouble building using the Makefile, I had to change this to BABEL = ./node_modules/babel-cli/bin/babel.js (perhaps you're using Babel 5?)

Jest run result ```bash /Users/hassankhan//node_modules/mock-knex/dist/platforms/knex/0.8/index.js:100 return cb(); ^ TypeError: cb is not a function at Client_PG.destroyRawConnection (/Users/hassankhan//node_modules/mock-knex/dist/platforms/knex/0.8/index.js:100:16) at Object.destroy (/Users/hassankhan//node_modules/knex/lib/client.js:252:18) at Pool.destroy (/Users/hassankhan//node_modules/generic-pool/lib/generic-pool.js:196:17) at Pool.destroyAllNow (/Users/hassankhan//node_modules/generic-pool/lib/generic-pool.js:552:10) at Immediate. (/Users/hassankhan//node_modules/knex/lib/client.js:327:21) at runCallback (timers.js:651:20) at tryOnImmediate (timers.js:624:5) at processImmediate [as _immediateCallback] (timers.js:596:5) ```
hassankhan commented 7 years ago

After digging around in the source, it seems like perhaps this is causing this new issue. I'm using client.destroy() as a promise, and mock-knex seems to only be able to handle callbacks for the same functionality.

EDIT: Changing this line to the following allows me to finally run tests successfully (although now mock-knex's tests seem to be failing 😖):

destroyRawConnection : (con, cb) => {
  const promise = Promise.resolve();

  if (cb && typeof cb === 'function') {
    promise.then(cb.bind(null, null), cb);
  }

  return promise;
},

Any thoughts, @jbrumwell?

hassankhan commented 7 years ago

Hey @jbrumwell, have you had a chance to look over the issue? Would it be okay to make a PR with this functionality?

deadratfink commented 6 years ago

Hi, I experience this as well when running my tests with jest 20.0.4 (so, setup is similar as above described by @hassankhan but without typescript). It could get fixed when I change the line var _package = require('knex/package'); of the transpiled mock-knex code in my _./nodemodules/mock-knex/dist/index.js to var _package = require('knex/package.json');. Of course this is not feasable for going on...so, any solving of this problem would be highly appreciated.

jbrumwell commented 6 years ago

@hassankhan Can you create a failing test for me using client.destroy()?

I will make the change requiring package.json in the next release sorry for the delay :)

nelsonwellswku commented 6 years ago

Any movement on this? I see an open PR that appears to fix the problem for me.

yvann commented 6 years ago

With "ts-jest" I just added "json" in "moduleFileExtensions" and it works now

# ./jest.config.js
module.exports = {
  globals: {
    'ts-jest': {
      tsConfigFile: 'tsconfig.json',
    },
  },
  moduleFileExtensions: ['ts', 'js', 'json'],
  transform: {
    '^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js',
  },
  testMatch: ['**/*.test.ts'],
  testEnvironment: 'node',
};
ShumeiZhang commented 5 years ago

this project npm install

jbrumwell commented 5 years ago

This should be resolved, https://github.com/colonyamerican/mock-knex/blob/master/src/index.js#L5